lemmy icon indicating copy to clipboard operation
lemmy copied to clipboard

Fix #3366: Wrap plain-text error responses from the API in JSON

Open PawanHegde opened this issue 1 year ago • 2 comments

Summary

  • All LemmyErrors 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.
  • Add a default handler for all 400-599 HTTP status responses that are not LemmyErrors.
    • 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)"}%   

PawanHegde avatar Jul 09 '23 11:07 PawanHegde

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.

Nutomic avatar Jul 10 '23 13:07 Nutomic

Seems like a little bit of bloat to add the whole middleware, but I spose its fine.

dessalines avatar Jul 10 '23 15:07 dessalines

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.

PawanHegde avatar Jul 10 '23 19:07 PawanHegde