refactor(router): remove WebhookApiErrorSwitch and implement error mapping using ErrorSwitch (#1660)

Co-authored-by: Sanchith Hegde <sanchith.hegde@juspay.in>
This commit is contained in:
Panagiotis Ganelis
2023-07-17 20:57:01 +03:00
committed by GitHub
parent 728177666b
commit a7c66ddea2
5 changed files with 257 additions and 277 deletions

View File

@ -1,7 +1,4 @@
use error_stack::ResultExt;
use crate::{
core::errors,
db::{get_and_deserialize_key, StorageInterface},
types::api,
};
@ -48,34 +45,3 @@ pub async fn lookup_webhook_event(
}
}
}
pub trait WebhookApiErrorSwitch<T> {
fn switch(self) -> errors::RouterResult<T>;
}
impl<T> WebhookApiErrorSwitch<T> for errors::CustomResult<T, errors::ConnectorError> {
fn switch(self) -> errors::RouterResult<T> {
match self {
Ok(res) => Ok(res),
Err(e) => match e.current_context() {
errors::ConnectorError::WebhookSourceVerificationFailed => {
Err(e).change_context(errors::ApiErrorResponse::WebhookAuthenticationFailed)
}
errors::ConnectorError::WebhookSignatureNotFound
| errors::ConnectorError::WebhookReferenceIdNotFound
| errors::ConnectorError::WebhookResourceObjectNotFound
| errors::ConnectorError::WebhookBodyDecodingFailed
| errors::ConnectorError::WebhooksNotImplemented => {
Err(e).change_context(errors::ApiErrorResponse::WebhookBadRequest)
}
errors::ConnectorError::WebhookEventTypeNotFound => {
Err(e).change_context(errors::ApiErrorResponse::WebhookUnprocessableEntity)
}
_ => Err(e).change_context(errors::ApiErrorResponse::InternalServerError),
},
}
}
}