1 |
|
2 |
|
3 |
|
4 | export interface AzFuncError {
|
5 | |
6 |
|
7 |
|
8 |
|
9 | isAzureFunctionsSystemError: boolean;
|
10 | }
|
11 |
|
12 | export class AzFuncSystemError extends Error {
|
13 | isAzureFunctionsSystemError = true;
|
14 | }
|
15 |
|
16 | export class AzFuncTypeError extends TypeError implements AzFuncError {
|
17 | isAzureFunctionsSystemError = true;
|
18 | }
|
19 |
|
20 | export class AzFuncRangeError extends RangeError implements AzFuncError {
|
21 | isAzureFunctionsSystemError = true;
|
22 | }
|
23 |
|
24 | export class ReadOnlyError extends AzFuncTypeError {
|
25 | constructor(propertyName: string) {
|
26 | super(`Cannot assign to read only property '${propertyName}'`);
|
27 | }
|
28 | }
|
29 |
|
30 | export function ensureErrorType(err: unknown): Error & Partial<AzFuncError> {
|
31 | if (err instanceof Error) {
|
32 | return err;
|
33 | } else {
|
34 | let message: string;
|
35 | if (err === undefined || err === null) {
|
36 | message = 'Unknown error';
|
37 | } else if (typeof err === 'string') {
|
38 | message = err;
|
39 | } else if (typeof err === 'object') {
|
40 | message = JSON.stringify(err);
|
41 | } else {
|
42 | message = String(err);
|
43 | }
|
44 | return new Error(message);
|
45 | }
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | export function isError(err: unknown): boolean {
|
53 | return err !== null && err !== undefined;
|
54 | }
|