axios: capture more error information to sentry

This commit is contained in:
vchikalkin 2024-03-01 10:29:06 +03:00
parent c2982cf31b
commit c9326697f6

View File

@ -1,7 +1,8 @@
import { HttpError } from './error';
import { captureException } from '@sentry/nextjs';
import { captureException, getCurrentScope } from '@sentry/nextjs';
import type { AxiosError } from 'axios';
import { isAxiosError } from 'axios';
import { pick } from 'radash';
function getErrorMessage<T extends { error?: string; errors?: string[]; message?: string }>(
error: AxiosError<T>
@ -18,8 +19,11 @@ export async function withHandleError<T>(fn: Promise<T>) {
return fn.catch((error_: AxiosError | Error) => {
if (isAxiosError(error_)) {
const message = getErrorMessage(error_);
if (error_.response?.status && error_.response?.status >= 500) {
if (error_.config && error_.response?.status && error_.response?.status >= 500) {
const scope = getCurrentScope();
const extras = pick(error_.config, ['url', 'data', 'params']);
scope.setExtras(extras);
scope.setTag('target_url', extras.url);
error_.message += ` | ${message}`;
captureException(error_);