Skip to content

Commit

Permalink
Export FunctionsError class
Browse files Browse the repository at this point in the history
  • Loading branch information
dlarocque committed Sep 27, 2024
1 parent f3402b9 commit dd2f91d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
9 changes: 9 additions & 0 deletions common/api-review/functions.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`;

Expand Down
3 changes: 1 addition & 2 deletions packages/functions/src/callable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
Expand Down
30 changes: 3 additions & 27 deletions packages/functions/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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',
Expand All @@ -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:
Expand All @@ -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';
Expand Down
26 changes: 25 additions & 1 deletion packages/functions/src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/functions/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit dd2f91d

Please sign in to comment.