diff --git a/common/api-review/functions.api.md b/common/api-review/functions.api.md index 8ce13d974ba..abc730bc1c8 100644 --- a/common/api-review/functions.api.md +++ b/common/api-review/functions.api.md @@ -5,6 +5,7 @@ ```ts import { FirebaseApp } from '@firebase/app'; +import { FirebaseError } from '@firebase/app'; // @public export function connectFunctionsEmulator(functionsInstance: Functions, host: string, port: number): void; @@ -16,6 +17,14 @@ export interface Functions { region: string; } +// @public +export class FunctionsError extends FirebaseError { + constructor( + code: FunctionsErrorCodeCore, message?: string, + details?: unknown); + readonly details?: unknown; +} + // @public export type FunctionsErrorCode = `functions/${FunctionsErrorCodeCore}`; diff --git a/packages/functions/src/callable.test.ts b/packages/functions/src/callable.test.ts index a2036046f1a..274419f6b2d 100644 --- a/packages/functions/src/callable.test.ts +++ b/packages/functions/src/callable.test.ts @@ -17,7 +17,7 @@ import { expect } from 'chai'; import * as sinon from 'sinon'; import { FirebaseApp } from '@firebase/app'; -import { FunctionsErrorCodeCore } from './public-types'; +import { FunctionsError, FunctionsErrorCodeCore } from './public-types'; import { Provider, ComponentContainer, @@ -39,7 +39,6 @@ import { import { makeFakeApp, createTestService } from '../test/utils'; import { httpsCallable } from './service'; import { FUNCTIONS_TYPE } from './constants'; -import { FunctionsError } from './error'; // eslint-disable-next-line @typescript-eslint/no-require-imports export const TEST_PROJECT = require('../../../config/project.json'); diff --git a/packages/functions/src/error.ts b/packages/functions/src/error.ts index 62d5ecb6a4f..16515b8f5d4 100644 --- a/packages/functions/src/error.ts +++ b/packages/functions/src/error.ts @@ -15,11 +15,9 @@ * limitations under the License. */ -import { FunctionsErrorCodeCore as FunctionsErrorCode } from './public-types'; +import { FunctionsError, FunctionsErrorCodeCore } from './public-types'; import { decode } from './serializer'; import { HttpResponseBody } from './service'; -import { FirebaseError } from '@firebase/util'; -import { FUNCTIONS_TYPE } from './constants'; /** * Standard error codes for different ways a request can fail, as defined by: @@ -28,7 +26,7 @@ import { FUNCTIONS_TYPE } from './constants'; * This map is used primarily to convert from a backend error code string to * a client SDK error code string, and make sure it's in the supported set. */ -const errorCodeMap: { [name: string]: FunctionsErrorCode } = { +const errorCodeMap: { [name: string]: FunctionsErrorCodeCore } = { OK: 'ok', CANCELLED: 'cancelled', UNKNOWN: 'unknown', @@ -48,28 +46,6 @@ const errorCodeMap: { [name: string]: FunctionsErrorCode } = { DATA_LOSS: 'data-loss' }; -/** - * An explicit error that can be thrown from a handler to send an error to the - * client that called the function. - */ -export class FunctionsError extends FirebaseError { - constructor( - /** - * A standard error code that will be returned to the client. This also - * determines the HTTP status code of the response, as defined in code.proto. - */ - code: FunctionsErrorCode, - message?: string, - /** - * Extra data to be converted to JSON and included in the error response. - */ - readonly details?: unknown - ) { - super(`${FUNCTIONS_TYPE}/${code}`, message || ''); - // TODO (dlarocque): Set this to be the root of the stack trace. - } -} - /** * Takes an HTTP status code and returns the corresponding ErrorCode. * This is the standard HTTP status code -> error mapping defined in: @@ -78,7 +54,7 @@ export class FunctionsError extends FirebaseError { * @param status An HTTP status code. * @return The corresponding ErrorCode, or ErrorCode.UNKNOWN if none. */ -function codeForHTTPStatus(status: number): FunctionsErrorCode { +function codeForHTTPStatus(status: number): FunctionsErrorCodeCore { // Make sure any successful status is OK. if (status >= 200 && status < 300) { return 'ok'; diff --git a/packages/functions/src/public-types.ts b/packages/functions/src/public-types.ts index 311493d5fda..de8c6aecb5d 100644 --- a/packages/functions/src/public-types.ts +++ b/packages/functions/src/public-types.ts @@ -14,7 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { FirebaseApp } from '@firebase/app'; +import { FirebaseApp, FirebaseError } from '@firebase/app'; +import { FUNCTIONS_TYPE } from './constants'; /** * An `HttpsCallableResult` wraps a single result from a function call. @@ -143,6 +144,29 @@ export type FunctionsErrorCodeCore = */ export type FunctionsErrorCode = `functions/${FunctionsErrorCodeCore}`; +/** + * An explicit error that can be thrown from a handler to send an error to the + * client that called the function. + * + * @public + */ +export class FunctionsError extends FirebaseError { + constructor( + /** + * A standard error code that will be returned to the client. This also + * determines the HTTP status code of the response, as defined in code.proto. + */ + code: FunctionsErrorCodeCore, + message?: string, + /** + * Extra data to be converted to JSON and included in the error response. + */ + readonly details?: unknown + ) { + super(`${FUNCTIONS_TYPE}/${code}`, message || ''); + } +} + declare module '@firebase/component' { interface NameServiceMapping { 'functions': Functions; diff --git a/packages/functions/src/service.ts b/packages/functions/src/service.ts index c5fe7fa8a85..2c8ff099fe5 100644 --- a/packages/functions/src/service.ts +++ b/packages/functions/src/service.ts @@ -17,11 +17,12 @@ import { FirebaseApp, _FirebaseService } from '@firebase/app'; import { + FunctionsError, HttpsCallable, HttpsCallableResult, HttpsCallableOptions } from './public-types'; -import { _errorForResponse, FunctionsError } from './error'; +import { _errorForResponse } from './error'; import { ContextProvider } from './context'; import { encode, decode } from './serializer'; import { Provider } from '@firebase/component';