1 | /**
|
2 | * @license
|
3 | * Copyright 2017 Google LLC
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | /**
|
18 | * @fileoverview Standardized Firebase Error.
|
19 | *
|
20 | * Usage:
|
21 | *
|
22 | * // Typescript string literals for type-safe codes
|
23 | * type Err =
|
24 | * 'unknown' |
|
25 | * 'object-not-found'
|
26 | * ;
|
27 | *
|
28 | * // Closure enum for type-safe error codes
|
29 | * // at-enum {string}
|
30 | * var Err = {
|
31 | * UNKNOWN: 'unknown',
|
32 | * OBJECT_NOT_FOUND: 'object-not-found',
|
33 | * }
|
34 | *
|
35 | * let errors: Map<Err, string> = {
|
36 | * 'generic-error': "Unknown error",
|
37 | * 'file-not-found': "Could not find file: {$file}",
|
38 | * };
|
39 | *
|
40 | * // Type-safe function - must pass a valid error code as param.
|
41 | * let error = new ErrorFactory<Err>('service', 'Service', errors);
|
42 | *
|
43 | * ...
|
44 | * throw error.create(Err.GENERIC);
|
45 | * ...
|
46 | * throw error.create(Err.FILE_NOT_FOUND, {'file': fileName});
|
47 | * ...
|
48 | * // Service: Could not file file: foo.txt (service/file-not-found).
|
49 | *
|
50 | * catch (e) {
|
51 | * assert(e.message === "Could not find file: foo.txt.");
|
52 | * if (e.code === 'service/file-not-found') {
|
53 | * console.log("Could not read file: " + e['file']);
|
54 | * }
|
55 | * }
|
56 | */
|
57 | export declare type ErrorMap<ErrorCode extends string> = {
|
58 | readonly [K in ErrorCode]: string;
|
59 | };
|
60 | export interface StringLike {
|
61 | toString(): string;
|
62 | }
|
63 | export interface ErrorData {
|
64 | [key: string]: unknown;
|
65 | }
|
66 | export declare class FirebaseError extends Error {
|
67 | /** The error code for this error. */
|
68 | readonly code: string;
|
69 | /** Custom data for this error. */
|
70 | customData?: Record<string, unknown> | undefined;
|
71 | /** The custom name for all FirebaseErrors. */
|
72 | readonly name = "FirebaseError";
|
73 | constructor(
|
74 | /** The error code for this error. */
|
75 | code: string, message: string,
|
76 | /** Custom data for this error. */
|
77 | customData?: Record<string, unknown> | undefined);
|
78 | }
|
79 | export declare class ErrorFactory<ErrorCode extends string, ErrorParams extends {
|
80 | readonly [K in ErrorCode]?: ErrorData;
|
81 | } = {}> {
|
82 | private readonly service;
|
83 | private readonly serviceName;
|
84 | private readonly errors;
|
85 | constructor(service: string, serviceName: string, errors: ErrorMap<ErrorCode>);
|
86 | create<K extends ErrorCode>(code: K, ...data: K extends keyof ErrorParams ? [ErrorParams[K]] : []): FirebaseError;
|
87 | }
|