fusion44 / blitz_api

A management backend for the RaspiBlitz project written in Python / FastAPI
MIT License
19 stars 18 forks source link

Show errors in a consistent way #123

Open cstenglein opened 2 years ago

cstenglein commented 2 years ago

Currently, the error comes in weird formats, depending on the endpoint.

Do not send an error array. but only one error at a time.

fusion44 commented 2 years ago

Can you give some examples? What is a not weird format?

Not sure I can do anything about the error array response... that's a FastAPI thing. Will check.

cstenglein commented 2 years ago

There are currently 3 ways a error is displayed: just in detail, as detail.msg, and detail[X].msg (X is an integer).

As a user of blitz_api, I would like to have consistent error messages, either in detail or in detail.msg. Multiple errors are not really necessary IMO, since you could just show the "next" error.

Scenario 1

When I call system/login with a wrong password, I get the following response:

{
  "detail": [
    {
      "loc": [
        "body",
        "password"
      ],
      "msg": "ensure this value has at least 8 characters",
      "type": "value_error.any_str.min_length",
      "ctx": {
        "limit_value": 8
      }
    }
  ]
}

=> So I access detail.msg.

When I get an unauthenticated error, e.g. by calling system/change-password, I get the following response:

{
  "detail": "Not authenticated"
}

=> So I need to access just detail.

Scenario 2

When I call system/change-password and I enter the wrong old_password:

{
  "detail": "old password format invalid"
}

=> So I need to access just detail. again

When I call /lightning/new-address with the request body

{
  "type": "p2w2"
}

I get the details as an array:

{
  "detail": [
    {
      "loc": [
        "body",
        "type"
      ],
      "msg": "value is not a valid enumeration member; permitted: 'p2wkh', 'np2wkh'",
      "type": "type_error.enum",
      "ctx": {
        "enum_values": [
          "p2wkh",
          "np2wkh"
        ]
      }
    }
  ]
}

=> So there I need to access detail[0].msg

fusion44 commented 2 years ago

That there are multiple errors returned is a Fastapi thing. If you call an endpoint with multiple wrong query args then you'll get an error array with an error message for each. We can't change this without forking FastAPI.

What we can do is always return an array where the first element is the actual error message. Need to look into this more.

fusion44 commented 7 months ago

Issues related to this:

fusion44 commented 6 months ago

TIL: https://www.rfc-editor.org/rfc/rfc9457.html

This standard describes error handling for apis.

Here's more some information: https://blog.frankel.ch/problem-details-http-apis/

Might be a good idea to implement this standard.