GREsau / okapi

OpenAPI (AKA Swagger) document generation for Rust projects
MIT License
606 stars 110 forks source link

How to use the error file in the custom schema? #77

Closed jimmybrenes closed 2 years ago

jimmybrenes commented 2 years ago

Hello,

I would like to know how to use the error.rs of the custom schema, specifically the function of the implementations

impl std::fmt::Display for Error impl<'r> Responder<'r, 'static> for Error *impl From<rocket::serde::json::Error<'_>> for Error**

Also, I want to know if it is possible to choose which errors each endpoint returns, because as I have it now, the errors are shown for all the endpoints.

Thanks!

ralpha commented 2 years ago

In this file (you most likely are referring to) https://github.com/GREsau/okapi/blob/master/examples/custom_schema/src/error.rs you can find how to implement an Error. This is just 1 struct. In the docs it will be stated that this only returns 4 different responses. (400, 404, 422 and 500)

If you want your own error just copy that code and change the errors you don't/do need. (remove/add some) If you want 2 endpoints to have different responses you can do that by creating to structs. Lets say ServerError and FileError. We can just add the errors we want to each endpoint by changing the error type it will return.

Also take a look at these links: Related issue: #34 An older project (that still uses older version of Okapi): APIErrorNoContent returns 204, 400, 404 and 500. APIErrorNotModified returns 304, 400, 404 and 500.

Hope this helps you, if you have more specific questions, just ask.