Skip to content

How to handle errors differently based on the URL? #675

Answered by NexRX
danechitoaie asked this question in Q&A
Discussion options

You must be logged in to vote

You could use middleware to handle it outside your Endpoint like so

#[poem::async_trait]
impl<E: Endpoint> Endpoint for HandleEndpointErrorsMiddleware<E> {
    type Output = Response;

    async fn call(&self, req: Request) -> poem::Result<Self::Output> {
        trace!("Entered cache controlling middleware");
        let is_not_idempotent = req.method().is_safe();
        let mut res = self.ep.get_response(req).await;

        if res.status().is_client_error() || res.status().is_server_error() >= 300 { // or just res.status().as_u16() >= 300
            if req.original_uri().path().starts_with("/api/"){
                // Give Json errors
            } else {
                //Give HTML …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by danechitoaie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants