Skip to content

Commit

Permalink
Simplify error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Aug 1, 2024
1 parent 72b7445 commit 7948a90
Showing 1 changed file with 21 additions and 69 deletions.
90 changes: 21 additions & 69 deletions compliant-reward-distribution/indexer/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ use concordium_rust_sdk::{
id::{
constants::ArCurve,
id_proof_types::{AtomicStatement, RevealAttributeStatement},
types::{AccountAddress, AccountCredentialWithoutProofs, GlobalContext},
types::{AccountAddress, AccountCredentialWithoutProofs, AttributeTag, GlobalContext},
},
types::{AbsoluteBlockHeight, AccountInfo},
v2::{AccountIdentifier, BlockIdentifier, Client, QueryResponse},
web3id::{
did::Network, get_public_data, CredentialLookupError, Presentation,
PresentationVerificationError, Web3IdAttribute,
did::Network,
get_public_data, CredentialLookupError,
CredentialStatement::{Account, Web3Id},
Presentation, PresentationVerificationError, Web3IdAttribute,
},
};
use concordium_rust_sdk::{
id::types::AttributeTag,
web3id::CredentialStatement::{Account, Web3Id},
};
use http::StatusCode;
use indexer::db::{ConversionError, Sha256};
use sha2::Digest;
Expand Down Expand Up @@ -104,77 +102,31 @@ impl<'a> From<DatabaseError> for ServerError<'a> {
impl<'a> IntoResponse for ServerError<'a> {
fn into_response(self) -> Response {
let r = match self {
ServerError::DatabaseErrorPostgres(_) => {
tracing::error!("Internal error: {self}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json("Internal error".to_string()),
)
}
ServerError::DatabaseErrorTypeConversion(..) => {
tracing::error!("Internal error: {self}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json("Internal error".to_string()),
)
}
ServerError::DatabaseErrorConfiguration(..) => {
// Internal errors.
ServerError::DatabaseErrorPostgres(_)
| ServerError::DatabaseErrorTypeConversion(..)
| ServerError::DatabaseErrorConfiguration(..) => {
tracing::error!("Internal error: {self}");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json("Internal error".to_string()),
)
}
ServerError::JsonRejection(_) => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, error_message.into())
}
ServerError::MaxRequestLimit(_) => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, error_message.into())
}
ServerError::SignerNotAdmin => {
// Bad request errors.
ServerError::JsonRejection(_)
| ServerError::MaxRequestLimit(_)
| ServerError::SignerNotAdmin
| ServerError::InvalidSignature
| ServerError::CredentialLookup(_)
| ServerError::InactiveCredentials
| ServerError::InvalidProof(_)
| ServerError::WrongLength(..)
| ServerError::AccountStatement
| ServerError::WrongStatement(_) => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, error_message.into())
}
ServerError::InvalidSignature => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, error_message.into())
}
ServerError::CredentialLookup(_) => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, Json(error_message.into()))
}
ServerError::InactiveCredentials => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, Json(error_message.into()))
}
ServerError::InvalidProof(_) => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, Json(error_message.into()))
}
ServerError::WrongLength(..) => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, Json(error_message.into()))
}
ServerError::AccountStatement => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, Json(error_message.into()))
}
ServerError::WrongStatement(_) => {
let error_message = format!("Bad request: {self}");
tracing::warn!(error_message);
(StatusCode::BAD_REQUEST, Json(error_message.into()))
}
};
r.into_response()
}
Expand Down Expand Up @@ -402,7 +354,7 @@ async fn post_zk_proof<'a>(
// TODO: use account_address to insert into database.
let _account_address = account_info.account_address;

if let Some(AtomicStatement::RevealAttribute { statement: stmt }) = statement.get(0) {
if let Some(AtomicStatement::RevealAttribute { statement: stmt }) = statement.first() {
if stmt != &ZK_STATEMENT_0 {
return Err(ServerError::WrongStatement(0));
}
Expand Down

0 comments on commit 7948a90

Please sign in to comment.