Skip to content

Commit

Permalink
feat(api_event_errors): error field in APIEvents (#2808)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: harsh-sharma-juspay <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2023
1 parent c6a5a85 commit ce10579
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
6 changes: 4 additions & 2 deletions crates/api_models/src/errors/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::borrow::Cow;

use reqwest::StatusCode;
use serde::Serialize;

#[derive(Debug, serde::Serialize)]
pub enum ErrorType {
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ pub async fn webhooks_wrapper<W: types::OutgoingWebhookType, Ctx: PaymentMethodR
Some(response_value),
None,
auth_type,
None,
api_event,
req,
);
Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/events/api_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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 {
Expand All @@ -64,6 +66,7 @@ impl ApiEvent {
request,
response,
auth_type,
error,
ip_addr: http_req
.connection_info()
.realip_remote_addr()
Expand Down
17 changes: 15 additions & 2 deletions crates/router/src/services/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -866,6 +878,7 @@ where
serialized_response,
overhead_latency,
auth_type,
error,
event_type.unwrap_or(ApiEventsType::Miscellaneous),
request,
);
Expand Down

0 comments on commit ce10579

Please sign in to comment.