mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
feat(api_event_errors): error field in APIEvents (#2808)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: harsh-sharma-juspay <125131007+harsh-sharma-juspay@users.noreply.github.com>
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use reqwest::StatusCode;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, serde::Serialize)]
|
||||
pub enum ErrorType {
|
||||
@ -78,7 +79,8 @@ pub struct Extra {
|
||||
pub reason: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
#[serde(tag = "type", content = "value")]
|
||||
pub enum ApiErrorResponse {
|
||||
Unauthorized(ApiError),
|
||||
ForbiddenCommonResource(ApiError),
|
||||
@ -88,7 +90,7 @@ pub enum ApiErrorResponse {
|
||||
Unprocessable(ApiError),
|
||||
InternalServerError(ApiError),
|
||||
NotImplemented(ApiError),
|
||||
ConnectorError(ApiError, StatusCode),
|
||||
ConnectorError(ApiError, #[serde(skip_serializing)] StatusCode),
|
||||
NotFound(ApiError),
|
||||
MethodNotAllowed(ApiError),
|
||||
BadRequest(ApiError),
|
||||
|
||||
@ -913,6 +913,7 @@ pub async fn webhooks_wrapper<W: types::OutgoingWebhookType, Ctx: PaymentMethodR
|
||||
Some(response_value),
|
||||
None,
|
||||
auth_type,
|
||||
None,
|
||||
api_event,
|
||||
req,
|
||||
);
|
||||
|
||||
@ -36,6 +36,7 @@ pub struct ApiEvent {
|
||||
ip_addr: Option<String>,
|
||||
url_path: String,
|
||||
response: Option<serde_json::Value>,
|
||||
error: Option<serde_json::Value>,
|
||||
#[serde(flatten)]
|
||||
event_type: ApiEventsType,
|
||||
hs_latency: Option<u128>,
|
||||
@ -52,6 +53,7 @@ impl ApiEvent {
|
||||
response: Option<serde_json::Value>,
|
||||
hs_latency: Option<u128>,
|
||||
auth_type: AuthenticationType,
|
||||
error: Option<serde_json::Value>,
|
||||
event_type: ApiEventsType,
|
||||
http_req: &HttpRequest,
|
||||
) -> Self {
|
||||
@ -64,6 +66,7 @@ impl ApiEvent {
|
||||
request,
|
||||
response,
|
||||
auth_type,
|
||||
error,
|
||||
ip_addr: http_req
|
||||
.connection_info()
|
||||
.realip_remote_addr()
|
||||
|
||||
@ -769,7 +769,7 @@ where
|
||||
T: Debug + Serialize + ApiEventMetric,
|
||||
A: AppStateInfo + Clone,
|
||||
E: ErrorSwitch<OErr> + error_stack::Context,
|
||||
OErr: ResponseError + error_stack::Context,
|
||||
OErr: ResponseError + error_stack::Context + Serialize,
|
||||
errors::ApiErrorResponse: ErrorSwitch<OErr>,
|
||||
{
|
||||
let request_id = RequestId::extract(request)
|
||||
@ -826,7 +826,9 @@ where
|
||||
.as_millis();
|
||||
|
||||
let mut serialized_response = None;
|
||||
let mut error = None;
|
||||
let mut overhead_latency = None;
|
||||
|
||||
let status_code = match output.as_ref() {
|
||||
Ok(res) => {
|
||||
if let ApplicationResponse::Json(data) = res {
|
||||
@ -854,7 +856,17 @@ where
|
||||
|
||||
metrics::request::track_response_status_code(res)
|
||||
}
|
||||
Err(err) => err.current_context().status_code().as_u16().into(),
|
||||
Err(err) => {
|
||||
error.replace(
|
||||
serde_json::to_value(err.current_context())
|
||||
.into_report()
|
||||
.attach_printable("Failed to serialize json response")
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError.switch())
|
||||
.ok()
|
||||
.into(),
|
||||
);
|
||||
err.current_context().status_code().as_u16().into()
|
||||
}
|
||||
};
|
||||
|
||||
let api_event = ApiEvent::new(
|
||||
@ -866,6 +878,7 @@ where
|
||||
serialized_response,
|
||||
overhead_latency,
|
||||
auth_type,
|
||||
error,
|
||||
event_type.unwrap_or(ApiEventsType::Miscellaneous),
|
||||
request,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user