I have a simple web service that I'm writing with Rocket, whenever data
comes as a 200 response, it contains a vector of String
s. When an error comes, I want to have custom errors. And the structure I want to impose on those responses should be like in here https://stackoverflow.com/a/23708903/4402306 , specifically:
for successful responses:
{
"data": {
"topics": ["topic1", "topic2", ...]
}
}
and for errors:
{
"error": {
"status_code": 404,
"message": "not found (or any other custom error)"
}
}
For the errors I have the confusion of using a catcher
in rocket vs implementing my own error structures like an enum (with rocket derivations):
#[derive(Serialize, Deserialize, Responder)]
#[response(content_type = "json")]
pub enum APIError{
CustomError1(String, #[response(ignore)] String),
CustomError2(String, #[response(ignore)] String),
...
}
So what should I use to achieve my goal?
I also not sure how to represent the successful case (because with rocket
, the first field should implement the Responder
trait, but I have a Vec<String>
there)
#[derive(Serialize, Deserialize, Responder)]
#[response(content_type = "json")]
pub struct Data {
topics: Vec<String>,
}
And finally, I think I also need to combine Data
and APIError
in a single Response
struct - but then it's not clear to me how to control when to return what.
Assistance will be highly appreciated.