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

Manage non expected json payloads #31

Open
ssanabriacec opened this issue Nov 16, 2020 · 1 comment
Open

Manage non expected json payloads #31

ssanabriacec opened this issue Nov 16, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@ssanabriacec
Copy link
Contributor

ssanabriacec commented Nov 16, 2020

Problem description:

EventManager endpoint expects the following structure for a payload:

{
    "Name": "event_name",
    "Payload": [
        {
            "key": "value",
        }
    ],
    "Timestamp": "2020-10-27 13:12:49",
    "ExtraParams": {
        "key": "value",
    }
}

But if the payload differs like this one:

{
    "key": "value"
}

it then crashes on InvokeAsync method because the properties are expected there.

Error message:

System.ArgumentNullException: Value cannot be null. (Parameter 'value')
   at Newtonsoft.Json.Linq.JToken.EnsureValue(JToken value)
   at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
   at EventManager.Middleware.EventManagerMiddleware.InvokeAsync(HttpContext httpContext)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

HEADERS
=======
Accept: */*
Accept-Encoding: gzip, deflate, br
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 868
Content-Type: application/json
Host: localhost:40861
User-Agent: PostmanRuntime/7.26.8
Postman-Token: a109ae8c-af76-4e17-9e83-c988a6dc32b3

Another issue:

Also, malformed json payload (missing the last curly brace) throws the following error:

Newtonsoft.Json.JsonReaderException: Unexpected end of content while loading JObject. Path 'key', line 42, position 2.
   at Newtonsoft.Json.Linq.JContainer.ReadTokenFrom(JsonReader reader, JsonLoadSettings options)
   at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JObject.Parse(String json)
   at EventManager.Middleware.EventManagerMiddleware.InvokeAsync(HttpContext httpContext)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

HEADERS
=======
Accept: */*
Accept-Encoding: gzip, deflate, br
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 867
Content-Type: application/json
Host: localhost:40861
User-Agent: PostmanRuntime/7.26.8
Postman-Token: 7a9b0216-4e88-46d3-9d43-378b5b3df382

Possible Solutions:

Dispatch/ throw an exception (BadRequest or similar) that can be catched on upper calls, one for the properties, and another one for the malformed json

@ssanabriacec ssanabriacec added the bug Something isn't working label Nov 16, 2020
@tupini07
Copy link
Contributor

Thanks for creating this issue! I would suggest doing as you say but creating a custom exception (eg, MalformedEventDispatch), so that it is clearer what is happening.

Another thing we can do is have the EventManagerMiddleware return a BadRequest status code whenever someone sends a request to the event reception endpoint using a malformed event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants