Return less information for JSON parser errors - asp.net-core

Using asp.net core 3.1 for a web api, my problem is that when a request comes in with some invalid parameters, the framework produces an error message that I don't want returned in the response.
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|968ad6dd-4488d8bc10ec5760.",
"errors": {
"$.value": [
"The JSON value could not be converted to System.String. Path: $.value | LineNumber: 1 | BytePositionInLine: 15."
]
}
}
Is it possible to change this error response to something else? Ideally, I'd want to reduce the verbosity and not return the error message ("The JSON value could not be converted to System.String").
app.UseExceptionHandler does not work. ExceptionMiddleware does not work because await _next(httpContext); does not throw an exception.
Thank you in advance.

It seems that one possible way is to play with this: https://github.com/dotnet/aspnetcore/blob/5ff9ed68d1cf6c89d72d27a69b00ed0ecd34daed/src/Mvc/Mvc.Core/src/Infrastructure/ModelStateInvalidFilter.cs
Either set _apiBehaviorOptions.SuppressModelStateInvalidFilter or create a custom InvalidModelStateResponseFactory. Haven't tried it yet, but seems like a potential solution.

Related

HTTP 500 on Podio GET item

When fetching GET https://api.podio.com/item/1259778758 (other items work fine), I get the following error:
HTTP 500
with response body:
{
"error_parameters": {},
"error_detail": null,
"error_propagate": false,
"request": {
"url": "http://api.podio.com/item/1259778758",
"query_string": "",
"method": "GET"
},
"error_description": "An unexpected error occured during execution",
"error": "unexpected"
}
I assume this is due to some field value, but cannot figure this out. Can you please check an fix this?
Additional notes:
It is not possible to open this item in the browser as well
The filter operation in the API fails as well when this item is in the result.
It sounds like this should be escalated with Podio technical support.

set default error messages for mutations in Hasura

I am using the Hasura console. If I get an error, I see a message like this:
{
"errors": [
{
"extensions": {
"path": "$.selectionSet.insert_one.args.object",
"code": "constraint-violation"
},
"message": "Uniqueness violation. duplicate key value violates unique constraint \"vehicle_number_plate_key\""
}
]
}
Is there any way that I can use the console to set a custom error message? I couldn't find any such option on the console.
Not sure if there is a way to customize the error messages that are generated by Hasura.
However, if this is to be consumed by a single frontend client, you can just let the frontend client define its own error messages.
This is also practical to support multi-language error messages if the need arises.

Getting GoToWebinar registrant

I'm trying to get registrant info from GoToWebinarApi (api link).
First I'm making a request to get all registrants and getting the following response:
Response body
[
{
"firstName": "Test",
"lastName": "t",
"registrationDate": "2020-06-04T08:46:44Z",
"timeZone": "Europe/Helsinki",
"joinUrl": "https://global.gotowebinar.com/join/7649495216334202379/502991121",
"registrantKey": 6700838782913279000,
"email": "tes#gmail.com",
"status": "APPROVED"
}
]
After that I'm trying to use this registrant key to get registrant info in another query but persistently getting following response:
https://api.getgo.com/G2W/rest/v2/organizers/{{organizer_key}}/webinars/{{webinar_key}}/registrants/{{registrant_key}}
{
"errorCode": "NotFound",
"description": "Registrant with specified key does not exist",
"incident": "3599905930306030349"
}
I don't know what's wrong, maybe it's a bug? It has a lot of stuff like this.
Have you tried downloading Postman and importing their collection? You can just fill in the fields and see if there's any issues. Removing the possibilities you made a mistake in your code.
https://documenter.getpostman.com/view/7571707/SVzw4fq2?version=latest#97d099de-e2b2-413c-96aa-d441d2db80c9
EDIT: Issue/Problem:
I've found the issue. Basically the numbers are too big for Javascript. See: JSON Response Long is Rounded or Corrupted and Large numbers erroneously rounded in JavaScript

How do I get model binding not to stop after first failure

I am trying to implement well described errors in my WebApi if a request fails. When trying to bind a json body to a DTO class I get very nice readable errors for the first property where the model binding fails, but then it seems to stop. I want to return back information of all the fields where model binding failed.
My DTO class looks like the following:
public class RequestDTO
{
public int FirstValue { get; set; }
public int SecondValue { get; set; }
}
When passing in the following JSON
{
"firstValue": "y",
"secondValue": "x"
}
I get the following response
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|6ba7ef4a-442494fd16304f12.",
"errors": {
"$.firstValue": [
"The JSON value could not be converted to System.Int32. Path: $.firstValue | LineNumber: 1 | BytePositionInLine: 18."
]
}
}
What I am trying to achieve is to get a response like
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|6ba7ef4a-442494fd16304f12.",
"errors": {
"$.firstValue": [
"The JSON value could not be converted to System.Int32. Path: $.firstValue | LineNumber: 1 | BytePositionInLine: 18."
],
"$.secondValue": [
"The JSON value could not be converted to System.Int32. Path: $.secondValue | LineNumber: 1 | BytePositionInLine: 19."
]
}
}
But I just can't figure out how to do that. I hope that my question was clear enough, otherwise please post a comment and I will update.
Since version 3.0 ASP.NET Core uses System.Text.Json for handling JSON. It stops deserialization on the first error and there doesn't seem to be a setting for disabling it.
If you need more deserialization errors you can use old Newtosoft.Json. Just add Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package and add change the following line in ConfigureServices
services.AddControllers();
to
services.AddControllers().AddNewtonsoftJson();
However, System.Text.Json has got better perforamnce and it's reccomended to use it.

JSON RPC 2.0 standard responses

I am currently looking into implementing some JSON RPC 2.0 responses in my code. However, I'm a little unclear on what are the standard practices of using it:
1) When the user sends a request with invalid parameters, should I just return verbatim default error message
{"jsonrpc": "2.0", "error": {"code": -32602, "message": " Invalid params"}, "id": "1"}
Or can the message be more specific, like:
{"jsonrpc": "2.0", "error": {"code": -32602, "message": " Invalid params: invalid username"}, "id": "1"}
Or should such custom messages have their own error code?
2) If the user say, requests data from the database and the response is "data is not present", as in we encountered no errors but still didn't return anything, should that be returned as a JSON RPC error, or should it be more of response indicating data wasn't found? In other words, is the convention in JSON RPC to use errors as normal return conditions like in Google Go, or is it more akin to "something really messed up" panics?
According to the specification (http://www.jsonrpc.org/specification#error_object) you must use the optional property data for your additional information about the error, so, in your case the response must be:
{"jsonrpc": "2.0", "error": {"code": -32602, "message": " Invalid params", "data":"invalid username"}, "id": "1"}
You can create your own personal error codes in the range -32000 to -32099, but I would do it only when necessary, that is, unless your client application should behave in this case ("invalid username") different than in any other -32602 case.
That is up to you. It is a design question with a broader scope than JSON-RCP. You can find some opinions at this post: when-to-throw-an-exception