{"action":"created","issue":{"url":"https://api.github.com/repos/SergioBenitez/Rocket/issues/72","repository_url":"https://api.github.com/repos/SergioBenitez/Rocket","labels_url":"https://api.github.com/repos/SergioBenitez/Rocket/issues/72/labels{/name}","comments_url":"https://api.github.com/repos/SergioBenitez/Rocket/issues/72/comments","events_url":"https://api.github.com/repos/SergioBenitez/Rocket/issues/72/events","html_url":"https://github.com/SergioBenitez/Rocket/issues/72","id":197908903,"number":72,"title":"How to handle error responses properly","user":{"login":"ChrisBuchholz","id":70427,"avatar_url":"https://avatars.githubusercontent.com/u/70427?v=3","gravatar_id":"","url":"https://api.github.com/users/ChrisBuchholz","html_url":"https://github.com/ChrisBuchholz","followers_url":"https://api.github.com/users/ChrisBuchholz/followers","following_url":"https://api.github.com/users/ChrisBuchholz/following{/other_user}","gists_url":"https://api.github.com/users/ChrisBuchholz/gists{/gist_id}","starred_url":"https://api.github.com/users/ChrisBuchholz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ChrisBuchholz/subscriptions","organizations_url":"https://api.github.com/users/ChrisBuchholz/orgs","repos_url":"https://api.github.com/users/ChrisBuchholz/repos","events_url":"https://api.github.com/users/ChrisBuchholz/events{/privacy}","received_events_url":"https://api.github.com/users/ChrisBuchholz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-12-28T20:06:03Z","updated_at":"2016-12-28T20:22:29Z","closed_at":null,"body":"After playing around with rocket, I've found myself writing code like the following (this is just an example to show what I mean):\r\n\r\n```rust\r\n#[get(\"/\")]\r\nfn get() -> Result<status::Custom<JSON<Structure>>, Error> {\r\n    if 1 == 1 {\r\n        let structure = get_a_structure();\r\n        Ok(status::Custom(Status::Ok, JSON(structure)))\r\n    } else if 1 == 2 {\r\n        Err(Error::NotFound)\r\n    } else if 1 == 3 {\r\n        Err(Error::Unauthorized)\r\n    }\r\n}\r\n```\r\nWhere depending on different scenarios, I'm either returning a `Ok` or `Err` where the error could be a number of enum variants.\r\n\r\nWithout any error handlers set up in rocket, both errors just responds with a 500, but I'm imagining that one could set a handler up like:\r\n\r\n```rust\r\n#[error(_)]\r\nfn not_found(e: Error) -> JSON<Error> {\r\n    // return appropriate json depending on what e is\r\n}\r\n```\r\nThat is, mounting an error handler that will catch all errors that are not handled by say a `#[error(404)]` handler, which can then act upon the `Error` that were returned by the route and give a proper response.\r\n\r\nIt doesn't seem like this is possible yet. I also don't know if there is an even better way to approach this problem, so my question is:\r\n\r\n- Am I doing errors wrong in my routes?\r\n- If not, how would I go about responding with a proper response when an `Err` is returned from a route, since it doesn't seem like the error is available to the error handler."},"comment":{"url":"https://api.github.com/repos/SergioBenitez/Rocket/issues/comments/269535059","html_url":"https://github.com/SergioBenitez/Rocket/issues/72#issuecomment-269535059","issue_url":"https://api.github.com/repos/SergioBenitez/Rocket/issues/72","id":269535059,"user":{"login":"cbrewster","id":9086315,"avatar_url":"https://avatars.githubusercontent.com/u/9086315?v=3","gravatar_id":"","url":"https://api.github.com/users/cbrewster","html_url":"https://github.com/cbrewster","followers_url":"https://api.github.com/users/cbrewster/followers","following_url":"https://api.github.com/users/cbrewster/following{/other_user}","gists_url":"https://api.github.com/users/cbrewster/gists{/gist_id}","starred_url":"https://api.github.com/users/cbrewster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cbrewster/subscriptions","organizations_url":"https://api.github.com/users/cbrewster/orgs","repos_url":"https://api.github.com/users/cbrewster/repos","events_url":"https://api.github.com/users/cbrewster/events{/privacy}","received_events_url":"https://api.github.com/users/cbrewster/received_events","type":"User","site_admin":false},"created_at":"2016-12-28T20:22:29Z","updated_at":"2016-12-28T20:22:29Z","body":"Here is an example for when I was messing with Diesel:\r\n```\r\nuse diesel::result::Error as DieselError;\r\nuse rocket::http::Status;\r\nuse rocket::response::{Response, Responder};\r\nuse rocket_contrib::JSON;\r\nuse std::error::Error;\r\n\r\n#[derive(Debug)]\r\npub enum ApiError {\r\n    DieselError(DieselError)\r\n}\r\n\r\n#[derive(Serialize)]\r\npub struct ErrorResponse {\r\n    pub status: &'static str,\r\n    pub message: String,\r\n}\r\n\r\npub type ApiResult<T> = Result<JSON<T>, ApiError>;\r\n\r\nimpl<'r> Responder<'r> for ApiError {\r\n    fn respond(self) -> Result<Response<'r>, Status> {\r\n        let message = match self {\r\n            ApiError::DieselError(error) => {\r\n                if error == DieselError::NotFound {\r\n                    return Err(Status::NotFound);\r\n                }\r\n                String::from(error.description())\r\n            }\r\n        };\r\n        JSON(ErrorResponse {\r\n            status: \"error\",\r\n            message: message,\r\n        }).respond()\r\n    }\r\n}\r\n\r\nimpl From<DieselError> for ApiError {\r\n    fn from(error: DieselError) -> ApiError {\r\n        ApiError::DieselError(error)\r\n    }\r\n}\r\n```"}}
{"actor":{"display_login":"cbrewster"}}