From 0170d647005457af6d7783bc4a00b9f7cad6124b Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Tue, 3 Oct 2023 15:10:46 +0300 Subject: [PATCH] utils/axios: capture only >500 errors process/load-kp: remove error capture --- apps/web/process/load-kp/reactions.ts | 11 ++------- apps/web/utils/axios.ts | 32 +++++++++++++++------------ apps/web/utils/error.ts | 9 ++++++++ 3 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 apps/web/utils/error.ts diff --git a/apps/web/process/load-kp/reactions.ts b/apps/web/process/load-kp/reactions.ts index e55ae35..22e6e29 100644 --- a/apps/web/process/load-kp/reactions.ts +++ b/apps/web/process/load-kp/reactions.ts @@ -2,13 +2,12 @@ import eltHelper from '../elt/lib/helper'; import { message } from '@/Components/Common/Notification'; import type { ProcessContext } from '@/process/types'; -import { captureException, withScope } from '@sentry/nextjs'; import { reaction } from 'mobx'; import { omit } from 'radash'; const key = 'KP_LOADING_INFO'; -export function common({ store, trpcClient, apolloClient, user }: ProcessContext) { +export function common({ store, trpcClient, apolloClient }: ProcessContext) { const { $calculation, $process, $tables } = store; const { init: initElt } = eltHelper({ apolloClient, store }); @@ -86,18 +85,12 @@ export function common({ store, trpcClient, apolloClient, user }: ProcessContext key, }); }) - .catch((error_) => { + .catch(() => { message.error({ content: `Ошибка во время загрузки КП ${quote.label}`, key, }); $calculation.element('selectQuote').resetValue(); - - withScope((scope) => { - scope.setExtra('quote', quote); - scope.setExtra('user', user); - captureException(error_); - }); }) .finally(() => { $process.delete('LoadKP'); diff --git a/apps/web/utils/axios.ts b/apps/web/utils/axios.ts index 22d68e3..ef5d05f 100644 --- a/apps/web/utils/axios.ts +++ b/apps/web/utils/axios.ts @@ -1,3 +1,4 @@ +import { HttpError } from './error'; import { captureException, withScope } from '@sentry/nextjs'; import type { AxiosError } from 'axios'; import { isAxiosError } from 'axios'; @@ -17,25 +18,28 @@ function getErrorMessage(fn: Promise) { return fn.catch((error_: AxiosError | Error) => { if (isAxiosError(error_)) { - const err = pick(error_, ['code', 'message', 'status', 'cause']); - const data = error_.config?.data; - const params = error_.config?.params; - const message = getErrorMessage(error_); - const opts = { ...err, data, message, params }; - error_.message += ` | ${message}`; + if (error_.response?.status && error_.response?.status >= 500) { + const err = pick(error_, ['code', 'message', 'status', 'cause']); + const data = error_.config?.data; + const params = error_.config?.params; - withScope((scope) => { - (Object.keys(opts) as Array).forEach((key) => { - let extra = opts[key]; - if (key === 'data') extra = JSON.stringify(extra); - scope.setExtra(key, extra); + const opts = { ...err, data, message, params }; + + error_.message += ` | ${message}`; + + withScope((scope) => { + (Object.keys(opts) as Array).forEach((key) => { + let extra = opts[key]; + if (key === 'data') extra = JSON.stringify(extra); + scope.setExtra(key, extra); + }); + captureException(error_); }); - captureException(error_); - }); + } - throw new Error(message); + throw new HttpError(message, error_.status || error_.response?.status); } return null as unknown as T; diff --git a/apps/web/utils/error.ts b/apps/web/utils/error.ts new file mode 100644 index 0000000..0aa96f0 --- /dev/null +++ b/apps/web/utils/error.ts @@ -0,0 +1,9 @@ +export class HttpError extends Error { + public statusCode: number; + + constructor(message: string, statusCode = 500) { + super(message); + this.name = 'HttpError'; + this.statusCode = statusCode; + } +}