diff --git a/src/services/utils.ts b/src/services/utils.ts index b41400ef..a2daaa16 100644 --- a/src/services/utils.ts +++ b/src/services/utils.ts @@ -7,6 +7,7 @@ import { getUserToken } from '../redux/commonStore'; import { CustomError } from '../utils/types/CustomError'; +import { NetworkTimeoutError } from '../utils/types/NetworkTimeoutError'; const DEFAULT_TIMEOUT_MS = 50_000; @@ -16,14 +17,6 @@ type FetchInitWithTimeout = RequestInit & { timeoutMs?: number; }; -/** Custom error type thrown when AbortSignal.timeout triggers. */ -export class NetworkTimeoutError extends Error { - constructor(messageKey: string = 'errors.network.timeout') { - super(messageKey); - this.name = 'NetworkTimeoutError'; - } -} - const parseError = (text: string) => { try { return JSON.parse(text); diff --git a/src/utils/error.ts b/src/utils/error.ts index e97cfbd8..2ba3fbec 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -6,6 +6,7 @@ */ import { SnackInputs, UseSnackMessageReturn } from '../hooks/useSnackMessage'; import { CustomError, formatMessageValues } from './types/CustomError'; +import { NetworkTimeoutError } from './types/NetworkTimeoutError'; export type HeaderSnackInputs = Pick; @@ -22,6 +23,13 @@ export function snackWithFallback( error: unknown, headerInputs?: HeaderSnackInputs ) { + if (error instanceof NetworkTimeoutError) { + snackError({ + messageId: error.message, + ...headerInputs, + }); + return; + } if (error instanceof CustomError && error.businessErrorCode) { snackError({ messageId: error.businessErrorCode, diff --git a/src/utils/types/NetworkTimeoutError.ts b/src/utils/types/NetworkTimeoutError.ts new file mode 100644 index 00000000..75cf61ec --- /dev/null +++ b/src/utils/types/NetworkTimeoutError.ts @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2025, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +/** Custom error type thrown when AbortSignal.timeout triggers. */ +export class NetworkTimeoutError extends Error { + constructor(messageKey: string = 'errors.network.timeout') { + super(messageKey); + this.name = 'NetworkTimeoutError'; + } +} diff --git a/src/utils/types/index.ts b/src/utils/types/index.ts index bb55ba2d..7a3fa7aa 100644 --- a/src/utils/types/index.ts +++ b/src/utils/types/index.ts @@ -5,6 +5,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ export * from './CustomError'; +export * from './NetworkTimeoutError'; export * from './elementType'; export * from './equipmentType'; export * from './equipmentTypes';