@azure-net/kit 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/delivery/injectableDependencies/AsyncHelpers.d.ts +4 -2
- package/dist/core/delivery/injectableDependencies/AsyncHelpers.js +40 -4
- package/dist/core/delivery/injectableDependencies/ErrorHandler.d.ts +5 -4
- package/dist/core/delivery/injectableDependencies/ErrorHandler.js +19 -13
- package/package.json +1 -1
|
@@ -5,8 +5,8 @@ export interface AsyncActionResponse<T, D = unknown, CustomErrorField = never> {
|
|
|
5
5
|
error?: AppError<D, CustomErrorField>;
|
|
6
6
|
}
|
|
7
7
|
type ActionOrThunk<Res> = Promise<Res> | (() => Promise<Res>);
|
|
8
|
-
export declare const createAsyncHelpers: <Custom = unknown>(opts?: {
|
|
9
|
-
parseError?: ReturnType<typeof createErrorParser<
|
|
8
|
+
export declare const createAsyncHelpers: <BaseError = unknown, Custom = unknown>(opts?: {
|
|
9
|
+
parseError?: ReturnType<typeof createErrorParser<BaseError, Custom>>;
|
|
10
10
|
}) => {
|
|
11
11
|
createAsyncAction: <Res = unknown, Req = unknown>(action: ActionOrThunk<Res>, args?: {
|
|
12
12
|
beforeSend?: (next: () => void, abort: () => void) => void | Promise<void>;
|
|
@@ -18,6 +18,7 @@ export declare const createAsyncHelpers: <Custom = unknown>(opts?: {
|
|
|
18
18
|
onAbort?: () => void;
|
|
19
19
|
};
|
|
20
20
|
fallbackResponse?: Res;
|
|
21
|
+
maxRetries?: number;
|
|
21
22
|
}) => Promise<AsyncActionResponse<Res, Req, Custom>>;
|
|
22
23
|
createAsyncResource: <Res, Req_1 = unknown>(action: ActionOrThunk<Res>, args?: {
|
|
23
24
|
beforeSend?: (next: () => void, abort: () => void) => void | Promise<void>;
|
|
@@ -29,6 +30,7 @@ export declare const createAsyncHelpers: <Custom = unknown>(opts?: {
|
|
|
29
30
|
onAbort?: () => void;
|
|
30
31
|
};
|
|
31
32
|
fallbackResponse?: Res;
|
|
33
|
+
maxRetries?: number;
|
|
32
34
|
}) => Promise<Res>;
|
|
33
35
|
};
|
|
34
36
|
export {};
|
|
@@ -42,14 +42,32 @@ export const createAsyncHelpers = (opts) => {
|
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
const maxRetries = args?.maxRetries ?? 3;
|
|
46
|
+
let retries = 0;
|
|
47
|
+
const req = async (isRetry = false) => {
|
|
48
|
+
if (isRetry) {
|
|
49
|
+
retries++;
|
|
50
|
+
if (retries > maxRetries) {
|
|
51
|
+
return Promise.reject(Error('Max retries reached'));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
46
54
|
const response = await Promise.resolve(typeof action === 'function' ? action() : action);
|
|
47
55
|
const result = { response, success: true };
|
|
48
56
|
await args?.onSuccess?.(result);
|
|
49
57
|
return result;
|
|
58
|
+
};
|
|
59
|
+
try {
|
|
60
|
+
return await req();
|
|
50
61
|
}
|
|
51
62
|
catch (err) {
|
|
52
|
-
|
|
63
|
+
let retryResult;
|
|
64
|
+
const retry = async () => await req(true)
|
|
65
|
+
.then((res) => (retryResult = res))
|
|
66
|
+
.catch(() => undefined);
|
|
67
|
+
const error = await errorParser(err, async () => await retry());
|
|
68
|
+
if (retryResult) {
|
|
69
|
+
return retryResult;
|
|
70
|
+
}
|
|
53
71
|
const { bus } = AppEvents();
|
|
54
72
|
bus.publish('OnAsyncHelperError', error);
|
|
55
73
|
const result = { error, response: args?.fallbackResponse, success: false };
|
|
@@ -84,13 +102,31 @@ export const createAsyncHelpers = (opts) => {
|
|
|
84
102
|
return args.fallbackResponse;
|
|
85
103
|
}
|
|
86
104
|
}
|
|
87
|
-
|
|
105
|
+
const maxRetries = args?.maxRetries ?? 3;
|
|
106
|
+
let retries = 0;
|
|
107
|
+
const req = async (isRetry = false) => {
|
|
108
|
+
if (isRetry) {
|
|
109
|
+
retries++;
|
|
110
|
+
if (retries > maxRetries) {
|
|
111
|
+
return Promise.reject(Error('Max retries reached'));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
88
114
|
const response = await Promise.resolve(typeof action === 'function' ? action() : action);
|
|
89
115
|
await args?.onSuccess?.(response);
|
|
90
116
|
return response;
|
|
117
|
+
};
|
|
118
|
+
try {
|
|
119
|
+
return await req();
|
|
91
120
|
}
|
|
92
121
|
catch (err) {
|
|
93
|
-
|
|
122
|
+
let retryResult;
|
|
123
|
+
const retry = async () => await req(true)
|
|
124
|
+
.then((res) => (retryResult = res))
|
|
125
|
+
.catch(() => undefined);
|
|
126
|
+
const error = await errorParser(err, async () => await retry());
|
|
127
|
+
if (retryResult) {
|
|
128
|
+
return retryResult;
|
|
129
|
+
}
|
|
94
130
|
const { bus } = AppEvents();
|
|
95
131
|
bus.publish('OnAsyncHelperError', error);
|
|
96
132
|
await args?.onError?.(error);
|
|
@@ -8,13 +8,14 @@ export interface AppError<T = unknown, CustomErrorField = never> {
|
|
|
8
8
|
status?: number;
|
|
9
9
|
original?: HttpServiceError<T> | SchemaFail<T> | Error;
|
|
10
10
|
custom?: CustomErrorField;
|
|
11
|
+
retry?: () => unknown;
|
|
11
12
|
}
|
|
12
13
|
export type ErrorType<T = unknown> = Error | HttpServiceError<T> | SchemaFail<T>;
|
|
13
|
-
export declare const baseParseHttpError: <T = unknown, D = never>(error: HttpServiceError<T
|
|
14
|
-
export declare const baseParseSchemaError: <SchemaData = unknown, D = never>(error: SchemaFail<SchemaData
|
|
15
|
-
export declare const baseParseBaseError: <D = never>(error: Error) => AppError<never, D
|
|
14
|
+
export declare const baseParseHttpError: <T = unknown, D = never>(error: HttpServiceError<T>, retry?: () => unknown) => Promise<AppError<T, D>>;
|
|
15
|
+
export declare const baseParseSchemaError: <SchemaData = unknown, D = never>(error: SchemaFail<SchemaData>, retry?: () => unknown) => Promise<AppError<SchemaData, D>>;
|
|
16
|
+
export declare const baseParseBaseError: <D = never>(error: Error, retry?: () => unknown) => Promise<AppError<never, D>>;
|
|
16
17
|
export declare const createErrorParser: <BaseError = unknown, Custom = unknown>(parsers?: {
|
|
17
18
|
parseBaseError?: typeof baseParseBaseError<Custom>;
|
|
18
19
|
parseHttpError?: typeof baseParseHttpError<BaseError, Custom>;
|
|
19
20
|
parseSchemaError?: typeof baseParseSchemaError<BaseError, Custom>;
|
|
20
|
-
}) => (<T = unknown>(error: ErrorType<BaseError
|
|
21
|
+
}) => (<T = unknown>(error: ErrorType<BaseError>, retry?: () => unknown) => Promise<AppError<T, Custom>>);
|
|
@@ -1,39 +1,45 @@
|
|
|
1
1
|
import { HttpServiceError } from '../../infra/httpService/index.js';
|
|
2
2
|
import { SchemaFail } from '../schema/index.js';
|
|
3
|
-
export const baseParseHttpError = (error) => {
|
|
4
|
-
|
|
3
|
+
export const baseParseHttpError = async (error, retry) => {
|
|
4
|
+
const err = {
|
|
5
5
|
type: 'http',
|
|
6
6
|
message: error.message ?? 'unexpected error',
|
|
7
7
|
status: error.status ?? 500,
|
|
8
|
-
original: error
|
|
8
|
+
original: error,
|
|
9
|
+
retry
|
|
9
10
|
};
|
|
11
|
+
return { ...err, retry: undefined, original: undefined };
|
|
10
12
|
};
|
|
11
|
-
export const baseParseSchemaError = (error) => {
|
|
12
|
-
|
|
13
|
+
export const baseParseSchemaError = async (error, retry) => {
|
|
14
|
+
const err = {
|
|
13
15
|
type: 'schema',
|
|
14
16
|
message: 'schema validation error',
|
|
15
17
|
status: 422,
|
|
16
18
|
fields: error.getErrors(),
|
|
17
|
-
original: error
|
|
19
|
+
original: error,
|
|
20
|
+
retry
|
|
18
21
|
};
|
|
22
|
+
return { ...err, retry: undefined, original: undefined };
|
|
19
23
|
};
|
|
20
|
-
export const baseParseBaseError = (error) => {
|
|
21
|
-
|
|
24
|
+
export const baseParseBaseError = async (error, retry) => {
|
|
25
|
+
const err = {
|
|
22
26
|
type: 'app',
|
|
23
27
|
message: error.message,
|
|
24
|
-
original: error
|
|
28
|
+
original: error,
|
|
29
|
+
retry
|
|
25
30
|
};
|
|
31
|
+
return { ...err, retry: undefined, original: undefined };
|
|
26
32
|
};
|
|
27
33
|
export const createErrorParser = (parsers) => {
|
|
28
|
-
return (error) => {
|
|
34
|
+
return async (error, retry) => {
|
|
29
35
|
const { parseBaseError = baseParseBaseError, parseHttpError = baseParseHttpError, parseSchemaError = baseParseSchemaError } = parsers ?? {};
|
|
30
36
|
switch (true) {
|
|
31
37
|
case error instanceof HttpServiceError:
|
|
32
|
-
return parseHttpError(error);
|
|
38
|
+
return (await parseHttpError(error, retry));
|
|
33
39
|
case error instanceof SchemaFail:
|
|
34
|
-
return parseSchemaError(error);
|
|
40
|
+
return (await parseSchemaError(error, retry));
|
|
35
41
|
default:
|
|
36
|
-
return parseBaseError(error);
|
|
42
|
+
return (await parseBaseError(error, retry));
|
|
37
43
|
}
|
|
38
44
|
};
|
|
39
45
|
};
|