Skip to content

Commit 1de28c8

Browse files
authored
Improve error message (rfc7807) (#13680)
* Add func formatErrorMessage * Add unit test * Commit generated code * Fix indentation * Using tabs * Set error before model * Commit generated code * Fix tabs * Commit generated code * Fix tabs * Fix tabs * Commit generated code
1 parent fa4f7e0 commit 1de28c8

9 files changed

Lines changed: 169 additions & 5 deletions

File tree

modules/openapi-generator/src/main/resources/go/api.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
385385
newErr.error = err.Error()
386386
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
387387
}
388-
newErr.model = v
388+
newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
389+
newErr.model = v
389390
{{^-last}}
390391
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
391392
{{/-last}}

modules/openapi-generator/src/main/resources/go/client.mustache

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,23 @@ func (e GenericOpenAPIError) Body() []byte {
616616
func (e GenericOpenAPIError) Model() interface{} {
617617
return e.model
618618
}
619+
620+
// format error message using title and detail when model implements rfc7807
621+
func formatErrorMessage(status string, v interface{}) string {
622+
623+
str := ""
624+
metaValue := reflect.ValueOf(v).Elem()
625+
626+
field := metaValue.FieldByName("Title")
627+
if field != (reflect.Value{}) {
628+
str = fmt.Sprintf("%s", field.Interface())
629+
}
630+
631+
field = metaValue.FieldByName("Detail")
632+
if field != (reflect.Value{}) {
633+
str = fmt.Sprintf("%s (%s)", str, field.Interface())
634+
}
635+
636+
// status title (detail)
637+
return fmt.Sprintf("%s %s", status, str)
638+
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,22 @@ public void verifyTestFile() throws IOException {
222222
"func Test_openapi_PetApiService(t *testing.T) {");
223223
}
224224

225+
@Test
226+
public void verifyFormatErrorMessageInUse() throws IOException {
227+
File output = Files.createTempDirectory("test").toFile();
228+
output.deleteOnExit();
229+
230+
final CodegenConfigurator configurator = new CodegenConfigurator()
231+
.setGeneratorName("go")
232+
.setInputSpec("src/test/resources/3_0/go/petstore-with-problem-details.yaml")
233+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
234+
235+
DefaultGenerator generator = new DefaultGenerator();
236+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
237+
files.forEach(File::deleteOnExit);
238+
239+
TestUtils.assertFileExists(Paths.get(output + "/api_pet.go"));
240+
TestUtils.assertFileContains(Paths.get(output + "/api_pet.go"),
241+
"newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)");
242+
}
225243
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
openapi: 3.0.0
2+
info:
3+
description: >-
4+
This spec is mainly for testing Petstore server and contains fake endpoints,
5+
models. Please do not use this for any other purpose. Special characters: "
6+
\
7+
version: 1.0.0
8+
title: OpenAPI Petstore
9+
license:
10+
name: Apache-2.0
11+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
12+
tags:
13+
- name: pet
14+
description: Everything about your Pets
15+
paths:
16+
/foo:
17+
get:
18+
tags:
19+
- pet
20+
responses:
21+
"200":
22+
description: response
23+
content:
24+
application/json:
25+
schema:
26+
$ref: '#/components/schemas/Foo'
27+
"404":
28+
description: not found
29+
content:
30+
application/json:
31+
schema:
32+
$ref: '#/components/schemas/RestServiceError'
33+
"422":
34+
description: validation error
35+
content:
36+
application/json:
37+
schema:
38+
$ref: '#/components/schemas/RestServiceError'
39+
components:
40+
schemas:
41+
Foo:
42+
type: string
43+
default: foo
44+
# RFC7807 Problem Detail
45+
RestServiceError:
46+
properties:
47+
type:
48+
description: " A URI reference that identifies the problem type"
49+
type: string
50+
title:
51+
description: "A short, human-readable summary of the problem type."
52+
type: string
53+
status:
54+
description: "The HTTP Status Code"
55+
type: integer
56+
detail:
57+
description: "A human-readable explanation specific to this occurrence of the problem."
58+
type: string
59+
instance:
60+
description: "A unique URI that identifies the specific occurrence of the problem."
61+
type: string

samples/client/petstore/go/go-petstore/client.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/api_default.go

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/api_fake.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/client.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)