Skip to content

Commit d502126

Browse files
authored
Add NetworkTimeoutError handling and refactor error management (#950)
Signed-off-by: achour94 <[email protected]>
1 parent a0c00ba commit d502126

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/services/utils.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import { getUserToken } from '../redux/commonStore';
99
import { CustomError } from '../utils/types/CustomError';
10+
import { NetworkTimeoutError } from '../utils/types/NetworkTimeoutError';
1011

1112
const DEFAULT_TIMEOUT_MS = 50_000;
1213

@@ -16,14 +17,6 @@ type FetchInitWithTimeout = RequestInit & {
1617
timeoutMs?: number;
1718
};
1819

19-
/** Custom error type thrown when AbortSignal.timeout triggers. */
20-
export class NetworkTimeoutError extends Error {
21-
constructor(messageKey: string = 'errors.network.timeout') {
22-
super(messageKey);
23-
this.name = 'NetworkTimeoutError';
24-
}
25-
}
26-
2720
const parseError = (text: string) => {
2821
try {
2922
return JSON.parse(text);

src/utils/error.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
import { SnackInputs, UseSnackMessageReturn } from '../hooks/useSnackMessage';
88
import { CustomError, formatMessageValues } from './types/CustomError';
9+
import { NetworkTimeoutError } from './types/NetworkTimeoutError';
910

1011
export type HeaderSnackInputs = Pick<SnackInputs, 'headerId' | 'headerTxt' | 'headerValues'>;
1112

@@ -22,6 +23,13 @@ export function snackWithFallback(
2223
error: unknown,
2324
headerInputs?: HeaderSnackInputs
2425
) {
26+
if (error instanceof NetworkTimeoutError) {
27+
snackError({
28+
messageId: error.message,
29+
...headerInputs,
30+
});
31+
return;
32+
}
2533
if (error instanceof CustomError && error.businessErrorCode) {
2634
snackError({
2735
messageId: error.businessErrorCode,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) 2025, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
8+
/** Custom error type thrown when AbortSignal.timeout triggers. */
9+
export class NetworkTimeoutError extends Error {
10+
constructor(messageKey: string = 'errors.network.timeout') {
11+
super(messageKey);
12+
this.name = 'NetworkTimeoutError';
13+
}
14+
}

src/utils/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77
export * from './CustomError';
8+
export * from './NetworkTimeoutError';
89
export * from './elementType';
910
export * from './equipmentType';
1011
export * from './equipmentTypes';

0 commit comments

Comments
 (0)