-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[API] Rework response and error handling #2139
Conversation
209eea4
to
09b2729
Compare
09b2729
to
d9a170e
Compare
7fa4e92
to
ca5c73b
Compare
36599e3
to
64c8c08
Compare
64c8c08
to
ef0a578
Compare
api/src/context.rs
Outdated
// TODO: Add error codes to these errors. | ||
pub fn get_latest_ledger_info_poem<E: InternalError>(&self) -> Result<LedgerInfo, E> { | ||
if let Some(oldest_version) = self.db.get_first_txn_version().map_err(E::internal)? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did the error codes go away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oop they don't need to, I can add them back.
anyhow::format_err!("Given limit value ({}) must not be zero", limit,).to_string(), | ||
)))); | ||
return Err(E::bad_request_str(&format!( | ||
"Given limit value ({}) must not be zero", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make sure to come back later and ensure these error messages are consistent
api/src/poem_backend/response.rs
Outdated
generate_success_response!(BasicResponse, (200, Ok)); | ||
|
||
// Generate traits defining a "from" function for each of these status types. | ||
// The error response then impls these traits for each status type they mention. | ||
generate_error_traits!(Internal, BadRequest, NotFound); | ||
|
||
// Generate an error response that only has options for 400 and 500. | ||
generate_error_response!(BasicError, (400, BadRequest), (500, Internal)); | ||
|
||
// This type just simplifies using BasicResponse and BasicError together. | ||
pub type BasicResult<T> = poem::Result<BasicResponse<T>, BasicError>; | ||
|
||
// As above but with 404. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've got a ton of macros, this isn't the easiest to read, but let's not index too much on these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed I'd like to come back to this, I'd like to find a non-macro approach here. Once you look past the macro complexity though the rest of the code becomes very nice and typed.
ef0a578
to
f3b8f20
Compare
f3b8f20
to
b6576de
Compare
✅ Forge test success on
|
Description
This PR reworks the response and error handling.
The good:
The bad:
The ugly:
Many endpoint handlers cannot imply the error type used in their results, meaning there are a lot of turbofish (. Figured it out, much nicer.::<>
) uses. Down the line we can investigate how to hint to rustc what is happening, since I feel it should be possible for it to figure out what's going onThe macros are pretty much a way to concentrate complexity that was previously all over the API codebase into just one place. At any rate I don't think there are many other good ways to make such strong checks happen at compile-time.
Test Plan
Same as for #1906. The main test is to run the server see what spec it generates:
Output: https://gist.github.com/banool/c9bc0659d4b155d7dd9584480378b8fc.
This change is