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

the problem of exceptionMessage To JSON #26

Closed
larssonsun opened this issue Feb 8, 2020 · 5 comments
Closed

the problem of exceptionMessage To JSON #26

larssonsun opened this issue Feb 8, 2020 · 5 comments
Labels

Comments

@larssonsun
Copy link

@larssonsun larssonsun commented Feb 8, 2020

when i use autowrapper , i will get a json result just like this:

{
"statusCode": 422,
"isError": true,
"responseException": {
"exceptionMessage": "{"Username":["username is needed."]}"
}
}

if not use autowrapper, it is :

{
"username": [
"username is needed."
]
}

and this:

{
"statusCode": 400,
"isError": true,
"responseException": {
"exceptionMessage": "{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1\",\"title\":\"Bad Request","status":400,"traceId":"|12b5128e-441a91a7c1e0b449."}"
}
}

if not use autowrapper (ProblemDetail entity):

{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "Bad Request",
"status": 400,
"traceId": "|2fc8416d-4f4fdb099ade0681."
}

how can i fixed the value of "exceptionMessage" to JSON ?
Or will autowrapper adapt the verification results returned by "ProblemDetial" and FluentValidation in the future?

@proudmonkey

This comment has been minimized.

Copy link
Owner

@proudmonkey proudmonkey commented Feb 9, 2020

Hi @larssonsun,

That's an expected behavior since ExceptionMessage property is of type string. JSON object will be treated as plain text. The AutoWrapper ApiException object has 4 overload constructor methods that you can use to customize your error response.:

ApiException(string message, int statusCode = 500, string errorCode = "", string refLink = "")
ApiException(IEnumerable<ValidationError> errors, int statusCode = 400)
ApiException(System.Exception ex, int statusCode = 500)
ApiException(object custom, int statusCode = 400)

For your ProblemDetail entity, you can use the following overload:

ApiException(object custom, int statusCode = 400)

For example:

throw new ApiException(problemDetail);

For details, see: http://vmsdurano.com/asp-net-core-with-autowrapper-customizing-the-default-response-output/

For Model Validation Errors, AutoWrapper supports it by default by using the following overload:

ApiException(IEnumerable<ValidationError> errors, int statusCode = 400)

For example:

if (ModelState.IsValid)
{
  // do something
}
else
{
        throw new ApiException(ModelState.AllErrors());
}

AutoWrapper is used by ApiBoilerPlate which uses FluentValidation. AutoWrapper support both Data Annotation and FluentValidation.

@larssonsun

This comment has been minimized.

Copy link
Author

@larssonsun larssonsun commented Feb 9, 2020

Thank you for your reply.

so autowrapper will not automatically convert these two instances, right? Yes, I can use a custom ApiException to solve this problem. I'm just looking for a smarter way, haha.

  1. ProblemDetail is a machine-readable format for specifying errors in HTTP API responses based on
    https://tools.ietf.org/html/rfc7807 since .netcore 2.1.
  2. Also shaped like:
{
  "property": ["errorMessage", ..]
}

Is the default return method of FluentValidation.

Maybe AutoWrapper will automatically convert these two instances in the future?

Thanks again in the end.

@proudmonkey

This comment has been minimized.

Copy link
Owner

@proudmonkey proudmonkey commented Feb 9, 2020

Yes, i'll consider adding support for ProblemDetails format in next release. :)

@larssonsun

This comment has been minimized.

Copy link
Author

@larssonsun larssonsun commented Feb 10, 2020

Very looking forward.
Now, I use the following method to solve it:

responseBody = responseBody.Replace("\"\\\"", "\"").Replace("\\\"\"", "\"")
                            .Replace("\"{", "{").Replace("}\"", "}")
                            .Replace("\\\"", "\"");

hha..

@proudmonkey

This comment has been minimized.

Copy link
Owner

@proudmonkey proudmonkey commented Feb 26, 2020

@larssonsun

Just released a new version that comes with fixes in regards to these issues. You can find it here: AutoWrapper Now Supports Problem Details For Your ASP.NET Core APIs

Thank you so much for all your suggestions on improving this middleware! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.