Skip to content

Manage non expected json payloads #31

@ssanabriacec

Description

@ssanabriacec

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions