lemmy
lemmy copied to clipboard
Fix #3366: Wrap plain-text error responses from the API in JSON
Summary
- All
LemmyError
s should be returned as JSON.- The previous behaviour was that if there was no explicit message, the message of the inner errorr was returned as
text-plain
. The same message is now wrapped in a JSON.
- The previous behaviour was that if there was no explicit message, the message of the inner errorr was returned as
- Add a default handler for all 400-599 HTTP status responses that are not
LemmyError
s.- This covers exceptions thrown by actix itself. The default handler for the framework would return text-plain messages.
Testing done
-
Valid call.
~ ❯ curl "http://localhost:8536/api/v3/comment/list?post_id=1&sort=New&type_=Local" -i at 16:48:09
HTTP/1.1 200 OK
content-length: 15
vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
content-type: application/json
access-control-expose-headers: content-type
date: Sun, 09 Jul 2023 11:18:17 GMT
-
Bad parameter. Error thrown by actix.
{"comments":[]}% ~ ❯ curl "http://localhost:8536/api/v3/comment/list?post_id=1&sort=New&type_=Test" -i at 16:48:17
HTTP/1.1 400 Bad Request
content-length: 105
content-type: application/json
date: Sun, 09 Jul 2023 11:18:25 GMT
{"error":"Query deserialize error: unknown variant `Test`, expected one of `All`, `Local`, `Subscribed`"}%
-
Brought down database. Error thrown by Lemmy.
~ ❯ curl "http://localhost:8536/api/v3/comment/list?post_id=1&sort=New&type_=Local" -i took 4s at 16:48:29
HTTP/1.1 400 Bad Request
content-length: 116
vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
access-control-expose-headers: content-type
content-type: application/json
date: Sun, 09 Jul 2023 11:25:18 GMT
{"error":"Error occurred while creating a new object: error connecting to server: Connection refused (os error 61)"}%
Looks good, thanks! However clippy is failing, most likely because of using unwrap. You can run ./scripts/fix-clippy.sh
to check and replace them with expect or similar.
Seems like a little bit of bloat to add the whole middleware, but I spose its fine.
Thanks for the reviews. I've had to update some code because of the changes in https://github.com/LemmyNet/lemmy/pull/3487. I've updated the summary as well.