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:
ivor-juspay
2023-11-22 12:55:51 +05:30
committed by GitHub
parent c6a5a85748
commit ce10579a72
4 changed files with 23 additions and 4 deletions

View File

@ -1,6 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use reqwest::StatusCode; use reqwest::StatusCode;
use serde::Serialize;
#[derive(Debug, serde::Serialize)] #[derive(Debug, serde::Serialize)]
pub enum ErrorType { pub enum ErrorType {
@ -78,7 +79,8 @@ pub struct Extra {
pub reason: Option<String>, pub reason: Option<String>,
} }
#[derive(Debug, Clone)] #[derive(Serialize, Debug, Clone)]
#[serde(tag = "type", content = "value")]
pub enum ApiErrorResponse { pub enum ApiErrorResponse {
Unauthorized(ApiError), Unauthorized(ApiError),
ForbiddenCommonResource(ApiError), ForbiddenCommonResource(ApiError),
@ -88,7 +90,7 @@ pub enum ApiErrorResponse {
Unprocessable(ApiError), Unprocessable(ApiError),
InternalServerError(ApiError), InternalServerError(ApiError),
NotImplemented(ApiError), NotImplemented(ApiError),
ConnectorError(ApiError, StatusCode), ConnectorError(ApiError, #[serde(skip_serializing)] StatusCode),
NotFound(ApiError), NotFound(ApiError),
MethodNotAllowed(ApiError), MethodNotAllowed(ApiError),
BadRequest(ApiError), BadRequest(ApiError),

View File

@ -913,6 +913,7 @@ pub async fn webhooks_wrapper<W: types::OutgoingWebhookType, Ctx: PaymentMethodR
Some(response_value), Some(response_value),
None, None,
auth_type, auth_type,
None,
api_event, api_event,
req, req,
); );

View File

@ -36,6 +36,7 @@ pub struct ApiEvent {
ip_addr: Option<String>, ip_addr: Option<String>,
url_path: String, url_path: String,
response: Option<serde_json::Value>, response: Option<serde_json::Value>,
error: Option<serde_json::Value>,
#[serde(flatten)] #[serde(flatten)]
event_type: ApiEventsType, event_type: ApiEventsType,
hs_latency: Option<u128>, hs_latency: Option<u128>,
@ -52,6 +53,7 @@ impl ApiEvent {
response: Option<serde_json::Value>, response: Option<serde_json::Value>,
hs_latency: Option<u128>, hs_latency: Option<u128>,
auth_type: AuthenticationType, auth_type: AuthenticationType,
error: Option<serde_json::Value>,
event_type: ApiEventsType, event_type: ApiEventsType,
http_req: &HttpRequest, http_req: &HttpRequest,
) -> Self { ) -> Self {
@ -64,6 +66,7 @@ impl ApiEvent {
request, request,
response, response,
auth_type, auth_type,
error,
ip_addr: http_req ip_addr: http_req
.connection_info() .connection_info()
.realip_remote_addr() .realip_remote_addr()

View File

@ -769,7 +769,7 @@ where
T: Debug + Serialize + ApiEventMetric, T: Debug + Serialize + ApiEventMetric,
A: AppStateInfo + Clone, A: AppStateInfo + Clone,
E: ErrorSwitch<OErr> + error_stack::Context, E: ErrorSwitch<OErr> + error_stack::Context,
OErr: ResponseError + error_stack::Context, OErr: ResponseError + error_stack::Context + Serialize,
errors::ApiErrorResponse: ErrorSwitch<OErr>, errors::ApiErrorResponse: ErrorSwitch<OErr>,
{ {
let request_id = RequestId::extract(request) let request_id = RequestId::extract(request)
@ -826,7 +826,9 @@ where
.as_millis(); .as_millis();
let mut serialized_response = None; let mut serialized_response = None;
let mut error = None;
let mut overhead_latency = None; let mut overhead_latency = None;
let status_code = match output.as_ref() { let status_code = match output.as_ref() {
Ok(res) => { Ok(res) => {
if let ApplicationResponse::Json(data) = res { if let ApplicationResponse::Json(data) = res {
@ -854,7 +856,17 @@ where
metrics::request::track_response_status_code(res) 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( let api_event = ApiEvent::new(
@ -866,6 +878,7 @@ where
serialized_response, serialized_response,
overhead_latency, overhead_latency,
auth_type, auth_type,
error,
event_type.unwrap_or(ApiEventsType::Miscellaneous), event_type.unwrap_or(ApiEventsType::Miscellaneous),
request, request,
); );