From 3b481f572456e1eab3435bfc25717770d95a8c49 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 4 Jan 2022 10:57:12 -0700 Subject: [PATCH] Change FirestoreError to extend FirebaseError (#5831) --- .changeset/big-otters-reply.md | 7 ++++++ common/api-review/firestore-lite.api.md | 4 ++-- common/api-review/firestore.api.md | 4 ++-- packages/firestore/src/util/error.ts | 9 ++++---- packages/storage/src/implementation/error.ts | 4 +++- packages/util/src/errors.ts | 3 +++ repo-scripts/prune-dts/.run/AllTests.run.xml | 19 +++++++++++++++ repo-scripts/prune-dts/extract-public-api.ts | 2 +- repo-scripts/prune-dts/prune-dts.ts | 12 ++++++---- repo-scripts/prune-dts/tests/error.input.d.ts | 23 +++++++++++++++++++ .../prune-dts/tests/error.output.d.ts | 21 +++++++++++++++++ 11 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 .changeset/big-otters-reply.md create mode 100644 repo-scripts/prune-dts/.run/AllTests.run.xml create mode 100644 repo-scripts/prune-dts/tests/error.input.d.ts create mode 100644 repo-scripts/prune-dts/tests/error.output.d.ts diff --git a/.changeset/big-otters-reply.md b/.changeset/big-otters-reply.md new file mode 100644 index 00000000000..003bf36e79e --- /dev/null +++ b/.changeset/big-otters-reply.md @@ -0,0 +1,7 @@ +--- +"@firebase/firestore": patch +"@firebase/storage": patch +"@firebase/util": patch +--- + +FirestoreError and StorageError now extend FirebaseError diff --git a/common/api-review/firestore-lite.api.md b/common/api-review/firestore-lite.api.md index 666c72047d4..f026ffc2840 100644 --- a/common/api-review/firestore-lite.api.md +++ b/common/api-review/firestore-lite.api.md @@ -6,6 +6,7 @@ import { EmulatorMockTokenOptions } from '@firebase/util'; import { FirebaseApp } from '@firebase/app'; +import { FirebaseError } from '@firebase/util'; import { LogLevelString as LogLevel } from '@firebase/logger'; // @public @@ -147,10 +148,9 @@ export interface FirestoreDataConverter { } // @public -export class FirestoreError extends Error { +export class FirestoreError extends FirebaseError { readonly code: FirestoreErrorCode; readonly message: string; - readonly name: string; readonly stack?: string; } diff --git a/common/api-review/firestore.api.md b/common/api-review/firestore.api.md index e892ed38209..39828bda9ff 100644 --- a/common/api-review/firestore.api.md +++ b/common/api-review/firestore.api.md @@ -6,6 +6,7 @@ import { EmulatorMockTokenOptions } from '@firebase/util'; import { FirebaseApp } from '@firebase/app'; +import { FirebaseError } from '@firebase/util'; import { LogLevelString as LogLevel } from '@firebase/logger'; // @public @@ -177,10 +178,9 @@ export interface FirestoreDataConverter { } // @public -export class FirestoreError extends Error { +export class FirestoreError extends FirebaseError { readonly code: FirestoreErrorCode; readonly message: string; - readonly name: string; readonly stack?: string; } diff --git a/packages/firestore/src/util/error.ts b/packages/firestore/src/util/error.ts index 4a050ca5bbe..029490e692e 100644 --- a/packages/firestore/src/util/error.ts +++ b/packages/firestore/src/util/error.ts @@ -15,6 +15,8 @@ * limitations under the License. */ +import { FirebaseError } from '@firebase/util'; + /** * The set of Firestore status codes. The codes are the same at the ones * exposed by gRPC here: @@ -208,10 +210,7 @@ export const Code = { }; /** An error returned by a Firestore operation. */ -export class FirestoreError extends Error { - /** The custom name for all FirestoreErrors. */ - readonly name: string = 'FirebaseError'; - +export class FirestoreError extends FirebaseError { /** The stack of the error. */ readonly stack?: string; @@ -226,7 +225,7 @@ export class FirestoreError extends Error { */ readonly message: string ) { - super(message); + super(code, message); // HACK: We write a toString property directly because Error is not a real // class and so inheritance does not work correctly. We could alternatively diff --git a/packages/storage/src/implementation/error.ts b/packages/storage/src/implementation/error.ts index 6d9f3bcb909..cdccfc51053 100644 --- a/packages/storage/src/implementation/error.ts +++ b/packages/storage/src/implementation/error.ts @@ -1,4 +1,3 @@ -import { FirebaseError } from '@firebase/util'; /** * @license * Copyright 2017 Google LLC @@ -15,6 +14,9 @@ import { FirebaseError } from '@firebase/util'; * See the License for the specific language governing permissions and * limitations under the License. */ + +import { FirebaseError } from '@firebase/util'; + import { CONFIG_STORAGE_BUCKET_KEY } from './constants'; /** diff --git a/packages/util/src/errors.ts b/packages/util/src/errors.ts index 97384e78cca..271998cc72a 100644 --- a/packages/util/src/errors.ts +++ b/packages/util/src/errors.ts @@ -72,11 +72,14 @@ export interface ErrorData { // Based on code from: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types export class FirebaseError extends Error { + /** The custom name for all FirebaseErrors. */ readonly name = ERROR_NAME; constructor( + /** The error code for this error. */ readonly code: string, message: string, + /** Custom data for this error. */ public customData?: Record ) { super(message); diff --git a/repo-scripts/prune-dts/.run/AllTests.run.xml b/repo-scripts/prune-dts/.run/AllTests.run.xml new file mode 100644 index 00000000000..8bf76d7d33b --- /dev/null +++ b/repo-scripts/prune-dts/.run/AllTests.run.xml @@ -0,0 +1,19 @@ + + + project + + $PROJECT_DIR$/../../node_modules/mocha + $PROJECT_DIR$ + true + + + + + + bdd + --require ts-node/register/type-check + PATTERN + *.test.ts + + + diff --git a/repo-scripts/prune-dts/extract-public-api.ts b/repo-scripts/prune-dts/extract-public-api.ts index 50b1a0e07cf..86fd588bbf5 100644 --- a/repo-scripts/prune-dts/extract-public-api.ts +++ b/repo-scripts/prune-dts/extract-public-api.ts @@ -138,7 +138,7 @@ export async function generateApi( untrimmedRollupDtsPath: string, publicDtsPath: string ): Promise { - console.log(`Configuring API Extractor for #{packageName}`); + console.log(`Configuring API Extractor for ${packageName}`); writeTypescriptConfig(packageRoot); writePackageJson(packageName); diff --git a/repo-scripts/prune-dts/prune-dts.ts b/repo-scripts/prune-dts/prune-dts.ts index e880cde2a83..f031a7bf08a 100644 --- a/repo-scripts/prune-dts/prune-dts.ts +++ b/repo-scripts/prune-dts/prune-dts.ts @@ -119,11 +119,15 @@ function isExported( sourceFile: ts.SourceFile, name: ts.Identifier ): boolean { + const declarations = + typeChecker.getSymbolAtLocation(name)?.declarations ?? []; + // Check is this is a public symbol (e.g. part of the DOM library) - const sourceFileNames = typeChecker - .getSymbolAtLocation(name) - ?.declarations?.map(d => d.getSourceFile().fileName); - if (sourceFileNames?.find(s => s.indexOf('typescript/lib') != -1)) { + const isTypescriptType = declarations.find( + d => d.getSourceFile().fileName.indexOf('typescript/lib') != -1 + ); + const isImported = declarations.find(d => ts.isImportSpecifier(d)); + if (isTypescriptType || isImported) { return true; } diff --git a/repo-scripts/prune-dts/tests/error.input.d.ts b/repo-scripts/prune-dts/tests/error.input.d.ts new file mode 100644 index 00000000000..9a1d8abb6e1 --- /dev/null +++ b/repo-scripts/prune-dts/tests/error.input.d.ts @@ -0,0 +1,23 @@ +/** + * @license + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { FirebaseError } from '@firebase/util'; + +export declare interface StorageError extends FirebaseError { + serverResponse: string | null; +} + +export {}; diff --git a/repo-scripts/prune-dts/tests/error.output.d.ts b/repo-scripts/prune-dts/tests/error.output.d.ts new file mode 100644 index 00000000000..b45c06687f1 --- /dev/null +++ b/repo-scripts/prune-dts/tests/error.output.d.ts @@ -0,0 +1,21 @@ +/** + * @license + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { FirebaseError } from '@firebase/util'; +export declare interface StorageError extends FirebaseError { + serverResponse: string | null; +} +export {};