UNPKG

1.76 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4export interface AzFuncError {
5 /**
6 * System errors can be tracked in our telemetry
7 * User errors cannot be tracked in our telemetry because they could have user information (users can still track it themselves in their app insights resource)
8 */
9 isAzureFunctionsSystemError: boolean;
10}
11
12export class AzFuncSystemError extends Error {
13 isAzureFunctionsSystemError = true;
14}
15
16export class AzFuncTypeError extends TypeError implements AzFuncError {
17 isAzureFunctionsSystemError = true;
18}
19
20export class AzFuncRangeError extends RangeError implements AzFuncError {
21 isAzureFunctionsSystemError = true;
22}
23
24export class ReadOnlyError extends AzFuncTypeError {
25 constructor(propertyName: string) {
26 super(`Cannot assign to read only property '${propertyName}'`);
27 }
28}
29
30export 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 * This is mostly for callbacks where `null` or `undefined` indicates there is no error
50 * By contrast, anything thrown/caught is assumed to be an error regardless of what it is
51 */
52export function isError(err: unknown): boolean {
53 return err !== null && err !== undefined;
54}