Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surface the source() fn in HandlerError #360

Closed
wants to merge 8 commits into from

Conversation

whitfin
Copy link
Contributor

@whitfin whitfin commented Oct 7, 2019

This might fix #359.

Basically we just surface the source() function from the Error trait correctly, by delegating to the internal error.

@colinbankier
Copy link
Collaborator

Thanks @whitfin. Seems fine - but travis seems unhappy.

@whitfin
Copy link
Contributor Author

whitfin commented Oct 8, 2019

@colinbankier the Travis unhappiness is due to #358 and the fact it has moved into beta :p

@fpgaminer
Copy link

I don't believe this fixes #359

Here's my minimal test:

use gotham::handler::{HandlerError, IntoHandlerError};
use gotham::state::State;


fn main() {
	pretty_env_logger::init();
	gotham::start("127.0.0.1:7879", || Ok(say_hello))
}

fn say_hello(state: State) -> (State, Result<String, HandlerError>) {
	let err = std::io::Error::new(std::io::ErrorKind::Other, "oh no!");
	(state, Err(err.into_handler_error()))
}

To be run with: RUST_LOG=debug cargo run

Using this patch the log shows:

 DEBUG gotham::handler::error      > [8b96448c-5a73-4c05-9ece-e26f95c5aa41] HandlerError generating 500 Internal Server Error response: (none)

Still shows (none) where the error should be; the same behavior as 0.4.0.

I'm no expert on this code base or implementing std:error::Error but I don't believe this line is right:

self.cause.source()

self.cause is the "source" of the HandlerError. So calling self.cause.source() actually returns the source of the source (which in my example usage is indeed None, hence the similar behavior).

My stab at it would be:

impl Error for HandlerError {
    fn description(&self) -> &str {
        "handler failed to process request"
    }

    fn cause(&self) -> Option<&dyn Error> {
        self.source()
    }

    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&*self.cause)
    }
}

Which appears to work correctly:

 DEBUG gotham::handler::error      > [4474b028-f506-4d12-a3ed-c8236f648d5d] HandlerError generating 500 Internal Server Error response: oh no!

Unless my assumptions are wrong, and the intention of source is to return the source of the source?

@whitfin
Copy link
Contributor Author

whitfin commented Oct 21, 2019

@fpgaminer you might be right, but then I'm a little confused what the point is to having both of those exist in the trait. What if self.cause has a source that is not None? Should we ignore it?

Or should it be something along the lines of the following (written in a rush)?

self.cause.source().or_else(|| Some(&*self.cause))

@fpgaminer
Copy link

fpgaminer commented Oct 21, 2019

My understanding is that Error::source() and Error::cause() are supposed to be synonymous; exactly the same method. Error::source() only exists because the std devs had to patch a bug (users couldn't downcast the Error returned by cause() because it didn't specify a 'static lifetime). Beyond that, the intention of the method didn't change. The naming of source versus cause is not significant.

So as a user I would expect for all implementations of Error, some_error.source() == some_error.cause().

@nyarly
Copy link
Contributor

nyarly commented May 20, 2020

Nudge, @fpgaminer and @whitfin - what's the status here?

@msrd0
Copy link
Member

msrd0 commented Aug 26, 2020

I think @fpgaminer is right - Error::cause() is marked deprecated in favour of Error::source(), so if we implement both, they should return the same result. Also I don't understand why this PR is making changes to the travis config.

@msrd0
Copy link
Member

msrd0 commented Aug 28, 2020

I believe this has been fixed in #438.

@msrd0 msrd0 closed this Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HandlerError::into_response doesn't log cause
6 participants