-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API] Rework response and error handling
- Loading branch information
Showing
13 changed files
with
546 additions
and
391 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,34 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::convert::TryFrom; | ||
|
||
use poem::web::Accept; | ||
use poem_openapi::payload::Json; | ||
|
||
use super::{AptosError, AptosErrorCode, AptosErrorResponse}; | ||
use super::{AptosErrorCode, BadRequestError}; | ||
|
||
#[derive(PartialEq)] | ||
pub enum AcceptType { | ||
Json, | ||
Bcs, | ||
} | ||
|
||
impl TryFrom<&Accept> for AcceptType { | ||
type Error = AptosErrorResponse; | ||
|
||
fn try_from(accept: &Accept) -> Result<Self, Self::Error> { | ||
for mime in &accept.0 { | ||
match mime.as_ref() { | ||
"application/json" => return Ok(AcceptType::Json), | ||
"application/x-bcs" => return Ok(AcceptType::Bcs), | ||
"*/*" => {} | ||
wildcard => { | ||
return Err(AptosErrorResponse::BadRequest(Json( | ||
AptosError::new(format!("Invalid Accept type: {:?}", wildcard)) | ||
.error_code(AptosErrorCode::UnsupportedAcceptType), | ||
))); | ||
} | ||
// I can't use TryFrom here right now: | ||
// https://stackoverflow.com/questions/73072492/apply-trait-bounds-to-associated-type | ||
pub fn parse_accept<E: BadRequestError>(accept: &Accept) -> Result<AcceptType, E> { | ||
for mime in &accept.0 { | ||
match mime.as_ref() { | ||
"application/json" => return Ok(AcceptType::Json), | ||
"application/x-bcs" => return Ok(AcceptType::Bcs), | ||
"*/*" => {} | ||
wildcard => { | ||
return Err(E::bad_request_str(&format!( | ||
"Unsupported Accept type: {:?}", | ||
wildcard | ||
)) | ||
.error_code(AptosErrorCode::UnsupportedAcceptType)); | ||
} | ||
} | ||
|
||
// Default to returning content as JSON. | ||
Ok(AcceptType::Json) | ||
} | ||
|
||
// Default to returning content as JSON. | ||
Ok(AcceptType::Json) | ||
} |
Oops, something went wrong.