-
Notifications
You must be signed in to change notification settings - Fork 76
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
could use customisation of error presentation #39
Comments
Great point. @davepacheco and I were discussing either a ignore = true option to the endpoint annotation or having two annotations: one for APIs and one for artifacts. I could imagine we might want some different options for each. Do you have an opinion? |
See also #40. I kind of like the idea of separate |
This is needed for oxidecomputer/omicron#3333, which would do the mentioned dropshot/dropshot/src/error.rs Lines 217 to 267 in c719b41
|
To my JavaScript-pilled brain, this looks like an optional Dropshot config param that's a function with a signature like: |
I discussed a few approaches offline with @jclulow and @ahl:
The choices are basically either (1) or (2)+(3). Neither (2) nor (3) alone is enough to deal with this. (1) sounds simple but has a pretty big downside, which is that you lose a lot of information about the underlying problem. Say you're in the middle of a request handler and you have a very specific problem with a lot of information about it. With (2), you can return your own @ahl, @jclulow, and I all prefer (2) + (3). There are no immediate plans to do it, but if someone wants to address this, that looks like the way to go. (2)+(3) also completely solves #41. Consumers would be completely in control of the returned error responses so they could use the same codes, their own codes, or whatever. Edit: this approach would also solve #801, too. |
I think I might take a crack at implementing the proposed solutions (2) and (3) described in #39 (comment), since this has come up a few times recently and it would be nice to have. One thing I don't see discussed in @davepacheco's comment is how HTTP status codes for errors would be determined. The approach that comes to my mind immediately is to also require that error types implement a trait that returns an |
This is starting to look like it will also involve a reasonably big change to introduce more structured error types to dropshot's internals, in support of point (3) in the above comment from Dave:
I was hoping to be able to make those changes separately from allowing handlers to return errors other than |
One design question that's come up while working on this: for the purposes of OpenAPI schema generation, do we think it's reasonable to assume/require that all #[endpoint(...)]
async fn foo(...) -> Result<Foo, FooError> {
// ...
} (where responses:
"4xx":
content:
application/json:
schema:
$ref: "#/components/schemas/FooError"
"5xx":
content:
application/json:
schema:
$ref: "#/components/schemas/FooError"
default:
content:
application/json:
schema:
$ref: "#/components/schemas/Foo" This allows generated client code to determine whether to expect the Otherwise, if we want to allow responses:
default:
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/Foo"
- $ref: "#/components/schemas/FooError" in which case, the client will just have to try to parse the response as both. Or, we could generate a Unfortunately, there isn't really a nice way to enforce that error types that implement a trait that returns |
Could we create our own type, |
Yes. While--yes--this is reflective of opinionation rather than constraint, Dropshot is intended to be opinionated! Saying that
I think there may be some merit to that. This approach may--for example--be a relevant approach to address #693. |
@davepacheco suggested that, as a middle ground, we could also have dropshot return a 500 if a user-defined error type returns a status that isn't an error. That's less violent than panicking, but still allows users to return |
Enforce that `HttpResponseError`s status codes are 4xx or 5xx using an `ErrorStatusCode` type which may only be those statuses. See: #39 (comment) While we're making breaking API changes, let's also have `HttpError::for_status` take a validated client-error-only type, rather than panicking surprisingly. This way, it's obvious to the user that the argument to this has to be a 4xx. Fixes #693
One imagines many dropshot servers will be straight REST APIs, where clients are just libraries or commands and uniform JSON error messages make sense. Some, however, may also serve other artefacts; e.g., a web server that had an API component, but also an interactive HTML component that a browser may access with some pages and images and such.
Presently dropshot will produce a stock uniform JSON error response for, say, endpoints that do not map to a handler. If the client sends an
Accept
header that suggests they understand or prefer a HTML response, it'd be good to be able to choose somehow to render a reasonable 404 page or event a 302 redirect to the right place for browsers.The text was updated successfully, but these errors were encountered: