amiv-eth / amivapi

The REST API behind most of AMIV's web services.
http://api.amiv.ethz.ch/docs
GNU Affero General Public License v3.0
31 stars 6 forks source link

Provide better error messages for invalid JSON #211

Open temparus opened 6 years ago

temparus commented 6 years ago

The API should accept a JSON-Schema for the field event.additional_fields.

While trying to set the following JSON-Schema, an unexpected error occurs.

{
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "additionalProperties": False,
      "properties": {
        "firstName": {
            "type": "string",
            "description":"Vorname"
        },
        "lastName": {
            "type": "string",
            "description":"Nachname"
        },
        "abo": {
          "type":"string",
          "description":"Abo",
          "enum": ["nix", "Halbtax", "Gleis7", "GA"]
        }
      },
      "required":["firstName", "lastName"]
}

Expected behavior No error is expected.

Actual behavior The API returns the following error: {"additional_fields":"Must be json, parsing failed with exception: No JSON object could be decoded"}

NotSpecial commented 6 years ago

This is because your data is invalid JSON. Check with this online linter.

Concretely, the False has to be false.

Maybe the API can provide better error messages? Or should the frontend rather do the JSON linting?

temparus commented 6 years ago

Oh so true. Now it works. That means that it is wrong in the documentation. I'll open another issue for that.

temparus commented 6 years ago

I don't think that this is necessary. As there are online validators for JSON, this can be checked by the developer themselves. I was biased by the wrong documentation and didn't thought that it could be wrong.