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

Add CORS headers to error responses #546

Merged
merged 1 commit into from
Jul 15, 2020
Merged

Add CORS headers to error responses #546

merged 1 commit into from
Jul 15, 2020

Conversation

rkusa
Copy link
Contributor

@rkusa rkusa commented May 26, 2020

Browsers are currently unable to read responses the originate from tide::Errors when the CORS middleware is used. This PR makes sure to add CORS headers to error responses as well.

I guess we want to implement parts of the PR differently (I'll highlight the line I am talking about via a code comment), but I still wanted to open a PR to start the discussion and maybe at least provide a unit test another implementation could reuse.

Refs #525

src/utils.rs Outdated
res.set_body(err.to_string());
}
res
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the implementation part I am not sure about. I have the feeling that having this as a utility method is not the best solution, so I am definitely open to other ideas of how to streamline the error responses here?

@davidpdrsn
Copy link

Not sure if it’s ideal that this middleware would “swallow” all errors. What if you have another middleware that logs errors or otherwise does stuff to errors?

That’s why I suggested instead being able to add headers directly to the error itself.

@rkusa
Copy link
Contributor Author

rkusa commented May 26, 2020

Not sure if it’s ideal that this middleware would “swallow” all errors. What if you have another middleware that logs errors or otherwise does stuff to errors?
That’s why I suggested instead being able to add headers directly to the error itself.

@davidpdrsn Thanks for bringing up that valid concern 👍! IMHO, you'd just have to add the middlewares in the order they should do stuff to errors. I think, headers shouldn't be the concern of a tide::Error - I rather see it within the responsibility of a middleware to transform an error into a response for any use-case where the error response should be customized (adding headers, adding another body, adding a JSON body, customize which status code includes the message as a body ...).

@davidpdrsn
Copy link

Hm alright. Maybe it would be nice to also update the docs to make it clear that it’s important to add this middleware last, otherwise your other middlewares won’t see errors.

src/utils.rs Outdated
// errors default to 500 by default, so sending the error
// body is opt-in at the call site.
if !res.status().is_server_error() {
res.set_body(err.to_string());
Copy link
Member

@jbr jbr May 31, 2020

Choose a reason for hiding this comment

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

I don't think all apps would want the same error response here. I'm not sure what the right solution is, but perhaps we need a way of adding an Option<tide::Error> to a Response or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, however, I've only extracted the current error handling into that utility method, to make it re-usable. So this PR is not changing the current error handling. As far as I remember from Discord, the current approach for custom error handling would be a custom middleware that intercepts errors and converts them into a response (basically similar to this one #525 (comment)).

Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 3, 2020
Related to all of the following discussions, it would be useful for http_types's errors to optionally carry data similar to a response, particulaly Headers and/or a Body.

This allows for better handling of situations where things like middleware want to act only partialy on an error.

Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 3, 2020
Related to all of the following discussions, it would be useful for http_types's errors to optionally carry data similar to a response, particularly Headers and/or a Body.

This allows for better handling of situations where things like middleware want to act only partially on an error.

Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
@Fishrock123
Copy link
Member

Btw I opened http-rs/http-types#169 which should hopefully make this kind of code nicer and more robust.

Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 5, 2020
This allows for robust creation of Response-s directly from Error-s, with error
capture for future reference, and retreival via `error() -> Option<&Error>`.

Refs: http-rs#169
Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 5, 2020
This allows for robust creation of Response-s directly from Error-s, with error
capture for future reference, and retrieval via `error() -> Option<&Error>`.

Refs: http-rs#169
Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 5, 2020
This allows for robust creation of Response-s directly from Error-s, with error
capture for future reference, and retrieval via `error() -> Option<&Error>`.

Refs: http-rs#169
Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 5, 2020
This allows for robust creation of Response-s directly from Error-s, with error
capture for future reference, and retrieval via `error() -> Option<&Error>`.

Refs: http-rs#169
Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 5, 2020
This allows for robust creation of Response-s directly from Error-s, with error
capture for future reference, and retrieval via `error() -> Option<&Error>`.

Refs: http-rs#169
Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 5, 2020
This allows for robust creation of Response-s directly from Error-s, with error
capture for future reference, and retrieval via `error() -> Option<&Error>`.

PR-URL: http-rs#174
Refs: http-rs#169
Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 16, 2020
This allows for robust creation of Response-s directly from Error-s, with error
capture for future reference, and retrieval via `error() -> Option<&Error>`.

PR-URL: http-rs#174
Refs: http-rs#169
Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
Fishrock123 added a commit to Fishrock123/http-types that referenced this pull request Jun 16, 2020
This allows for robust creation of Response-s directly from Error-s, with error
capture for future reference, and retrieval via `error() -> Option<&Error>`.

PR-URL: http-rs#174
Refs: http-rs#169
Refs: http-rs/tide#546
Refs: http-rs/tide#532
Refs: http-rs/tide#452
@rkusa
Copy link
Contributor Author

rkusa commented Jul 15, 2020

It looks like this got fixed with recent changes like http-rs/http-types#174
I've reduced the PR to the unit-test, in case we want to add the test to prevent future regressions?

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.

This is great; thanks heaps!

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.

5 participants