blitz_api icon indicating copy to clipboard operation
blitz_api copied to clipboard

Show errors in a consistent way

Open cstenglein opened this issue 3 years ago • 9 comments
trafficstars

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

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

cstenglein avatar Jun 19 '22 14:06 cstenglein

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.

fusion44 avatar Jun 20 '22 17:06 fusion44

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

cstenglein avatar Jul 03 '22 08:07 cstenglein

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 avatar Sep 14 '22 10:09 fusion44

Issues related to this:

  • https://github.com/fusion44/blitz_api/issues/148
  • https://github.com/fusion44/blitz_api/issues/205
  • https://github.com/fusion44/blitz_api/issues/220
  • https://github.com/fusion44/blitz_api/issues/245
  • https://github.com/fusion44/blitz_api/issues/247

fusion44 avatar Mar 10 '24 12:03 fusion44

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.

fusion44 avatar Apr 08 '24 17:04 fusion44