fix(router/webhooks): use api error response for returning errors from webhooks core (#1305)

This commit is contained in:
ItsMeShashank
2023-05-29 19:28:38 +05:30
committed by GitHub
parent 864d85534f
commit cd0cf40fe2
6 changed files with 212 additions and 109 deletions

View File

@ -1,4 +1,7 @@
use error_stack::ResultExt;
use crate::{
core::errors,
db::{get_and_deserialize_key, StorageInterface},
types::api,
};
@ -45,3 +48,30 @@ 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::WebhookEventTypeNotFound
| errors::ConnectorError::WebhookResourceObjectNotFound
| errors::ConnectorError::WebhookBodyDecodingFailed => {
Err(e).change_context(errors::ApiErrorResponse::WebhookBadRequest)
}
_ => Err(e).change_context(errors::ApiErrorResponse::InternalServerError),
},
}
}
}