Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message (rfc7807) #13680

Merged
merged 12 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
error: localVarHTTPResponse.Status,
}
{{#responses}}
{{#dataType}}
{{#dataType}}
gcatanese marked this conversation as resolved.
Show resolved Hide resolved
{{^is1xx}}
{{^is2xx}}
{{#range}}
Expand All @@ -386,6 +386,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
}
newErr.model = v
newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
gcatanese marked this conversation as resolved.
Show resolved Hide resolved
{{^-last}}
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
{{/-last}}
Expand Down
20 changes: 20 additions & 0 deletions modules/openapi-generator/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,23 @@ func (e GenericOpenAPIError) Body() []byte {
func (e GenericOpenAPIError) Model() interface{} {
return e.model
}

// format error message using title and detail when model implements rfc7807
func formatErrorMessage(status string, v interface{}) string {

str := ""
metaValue := reflect.ValueOf(v).Elem()

field := metaValue.FieldByName("Title")
if field != (reflect.Value{}) {
str = fmt.Sprintf("%s", field.Interface())
}

field = metaValue.FieldByName("Detail")
if field != (reflect.Value{}) {
str = fmt.Sprintf("%s (%s)", str, field.Interface())
}

// status title (detail)
return fmt.Sprintf("%s %s", status, str)
}
Copy link
Member

@wing328 wing328 Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this code block uses 4-space instead of tabs for indention, can you update it to use tabs instead?

Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,22 @@ public void verifyTestFile() throws IOException {
"func Test_openapi_PetApiService(t *testing.T) {");
}

@Test
public void verifyFormatErrorMessageInUse() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go")
.setInputSpec("src/test/resources/3_0/go/petstore-with-problem-details.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);

TestUtils.assertFileExists(Paths.get(output + "/api_pet.go"));
TestUtils.assertFileContains(Paths.get(output + "/api_pet.go"),
"newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
openapi: 3.0.0
info:
description: >-
This spec is mainly for testing Petstore server and contains fake endpoints,
models. Please do not use this for any other purpose. Special characters: "
\
version: 1.0.0
title: OpenAPI Petstore
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
tags:
- name: pet
description: Everything about your Pets
paths:
/foo:
get:
tags:
- pet
responses:
"200":
description: response
content:
application/json:
schema:
$ref: '#/components/schemas/Foo'
"404":
description: not found
content:
application/json:
schema:
$ref: '#/components/schemas/RestServiceError'
"422":
description: validation error
content:
application/json:
schema:
$ref: '#/components/schemas/RestServiceError'
components:
schemas:
Foo:
type: string
default: foo
# RFC7807 Problem Detail
RestServiceError:
properties:
type:
description: " A URI reference that identifies the problem type"
type: string
title:
description: "A short, human-readable summary of the problem type."
type: string
status:
description: "The HTTP Status Code"
type: integer
detail:
description: "A human-readable explanation specific to this occurrence of the problem."
type: string
instance:
description: "A unique URI that identifies the specific occurrence of the problem."
type: string
20 changes: 20 additions & 0 deletions samples/client/petstore/go/go-petstore/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions samples/openapi3/client/petstore/go/go-petstore/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.