Skip to content

Add error::ResultDynErrExt #216

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

Merged
merged 1 commit into from
May 20, 2019
Merged

Add error::ResultDynErrExt #216

merged 1 commit into from
May 20, 2019

Conversation

updogliu
Copy link
Contributor

@updogliu updogliu commented May 13, 2019

Add error::ResultDynErrExt, which provide the same set of methods in error::ResultErr for Result<T, Box<dyn Error>>.

Motivation and Context

Trying to resolve issue #208. With this PR, a e: Result<T, Box<dyn Error>> can call e.client_err(), e.server_err() conveniently.

This introduces duplicate codes with error::ResultErr. But I did not figure out a way to avoid that.

How Has This Been Tested?

Not tested - mostly just copy and paste error::ResultErr, which does not have a test case for now.
I have no problem adding a unit test if necessary.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@updogliu updogliu force-pushed the master branch 2 times, most recently from 08937a1 to a981059 Compare May 15, 2019 09:34
@updogliu
Copy link
Contributor Author

Seeking for code review

@yoshuawuyts
Copy link
Member

@updogliu thanks for this PR! -- I think I'm missing a bit of context in how this would be used. Could you perhaps provide a (small) example?

@updogliu
Copy link
Contributor Author

updogliu commented May 17, 2019

The added trait ResultDynErrExt<T> has the same purpose to the existing ResultExt. ResultExt works for Result<T, E: std::error::Error + Send + Sync + 'static>, while ResultDynErrExt<T> works for Result<T, Box<dyn std::error::Error + Send + Sync>>.

Specifically, when a step of processing the request returns a Result<T, Box<dyn std::error::Error + Send + Sync>> (for example, to cover various kinds of errors), it is convenient to categorize the error in the result, if any, as a client error or a server error. This PR enables doing that via result.client_err() or result.server_err().

# Example:
async fn my_endpoint(mut cx: Context<State>) -> EndpointResult<u8> {
    let result: Result<u8, Box<dyn std::error::Error + Send + Sync>> = process(cx);
    result.client_err()?
}

@yoshuawuyts
Copy link
Member

@updogliu thanks for the explanation; what you're saying makes sense.

#220 has landed which has reorganized the project structure. Would you mind rebasing?

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after the rebase!

@updogliu
Copy link
Contributor Author

Thanks. Rebased.

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thanks!

@yoshuawuyts yoshuawuyts merged commit 2878aba into http-rs:master May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants