Skip to content

Commit e673dc8

Browse files
authored
Add public FunctionsErrorCode type (#6344)
1 parent d87d3a8 commit e673dc8

File tree

7 files changed

+53
-31
lines changed

7 files changed

+53
-31
lines changed

.changeset/tiny-donuts-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/functions': patch
3+
---
4+
5+
Update public `FunctionsErrorCode` type to include "functions/" prefix.

common/api-review/functions.api.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export interface FunctionsError extends FirebaseError {
2424
}
2525

2626
// @public
27-
export type FunctionsErrorCode = 'ok' | 'cancelled' | 'unknown' | 'invalid-argument' | 'deadline-exceeded' | 'not-found' | 'already-exists' | 'permission-denied' | 'resource-exhausted' | 'failed-precondition' | 'aborted' | 'out-of-range' | 'unimplemented' | 'internal' | 'unavailable' | 'data-loss' | 'unauthenticated';
27+
export type FunctionsErrorCode = `functions/${FunctionsErrorCodeCore}`;
28+
29+
// @public
30+
export type FunctionsErrorCodeCore = 'ok' | 'cancelled' | 'unknown' | 'invalid-argument' | 'deadline-exceeded' | 'not-found' | 'already-exists' | 'permission-denied' | 'resource-exhausted' | 'failed-precondition' | 'aborted' | 'out-of-range' | 'unimplemented' | 'internal' | 'unavailable' | 'data-loss' | 'unauthenticated';
2831

2932
// @public
3033
export function getFunctions(app?: FirebaseApp, regionOrCustomDomain?: string): Functions;

packages/functions-compat/src/callable.test.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ async function expectError(
3535
await promise;
3636
} catch (e) {
3737
failed = true;
38-
// Errors coming from callable functions usually have the functions
39-
// code in the message since it's thrown inside functions.
40-
expect(e.code).to.match(new RegExp(`functions.*/${code}`));
38+
expect(e.code).to.equal(code);
4139
expect(e.message).to.equal(message);
4240
expect(e.details).to.deep.equal(details);
4341
}
@@ -106,25 +104,29 @@ describe('Firebase Functions > Call', () => {
106104
it('missing result', async () => {
107105
const functions = createTestService(app, region);
108106
const func = functions.httpsCallable('missingResultTest');
109-
await expectError(func(), 'internal', 'Response is missing data field.');
107+
await expectError(
108+
func(),
109+
'functions/internal',
110+
'Response is missing data field.'
111+
);
110112
});
111113

112114
it('unhandled error', async () => {
113115
const functions = createTestService(app, region);
114116
const func = functions.httpsCallable('unhandledErrorTest');
115-
await expectError(func(), 'internal', 'internal');
117+
await expectError(func(), 'functions/internal', 'internal');
116118
});
117119

118120
it('unknown error', async () => {
119121
const functions = createTestService(app, region);
120122
const func = functions.httpsCallable('unknownErrorTest');
121-
await expectError(func(), 'internal', 'internal');
123+
await expectError(func(), 'functions/internal', 'internal');
122124
});
123125

124126
it('explicit error', async () => {
125127
const functions = createTestService(app, region);
126128
const func = functions.httpsCallable('explicitErrorTest');
127-
await expectError(func(), 'out-of-range', 'explicit nope', {
129+
await expectError(func(), 'functions/out-of-range', 'explicit nope', {
128130
start: 10,
129131
end: 20,
130132
long: 30
@@ -134,12 +136,16 @@ describe('Firebase Functions > Call', () => {
134136
it('http error', async () => {
135137
const functions = createTestService(app, region);
136138
const func = functions.httpsCallable('httpErrorTest');
137-
await expectError(func(), 'invalid-argument', 'invalid-argument');
139+
await expectError(func(), 'functions/invalid-argument', 'invalid-argument');
138140
});
139141

140142
it('timeout', async () => {
141143
const functions = createTestService(app, region);
142144
const func = functions.httpsCallable('timeoutTest', { timeout: 10 });
143-
await expectError(func(), 'deadline-exceeded', 'deadline-exceeded');
145+
await expectError(
146+
func(),
147+
'functions/deadline-exceeded',
148+
'deadline-exceeded'
149+
);
144150
});
145151
});

packages/functions/src/callable.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { expect } from 'chai';
1818
import * as sinon from 'sinon';
1919
import { FirebaseApp } from '@firebase/app';
20-
import { FunctionsErrorCode } from './public-types';
20+
import { FunctionsErrorCodeCore } from './public-types';
2121
import {
2222
Provider,
2323
ComponentContainer,
@@ -44,7 +44,7 @@ export const TEST_PROJECT = require('../../../config/project.json');
4444
// https://github.com/chaijs/chai/issues/608
4545
async function expectError(
4646
promise: Promise<any>,
47-
code: FunctionsErrorCode,
47+
code: FunctionsErrorCodeCore,
4848
message: string,
4949
details?: any
5050
): Promise<void> {

packages/functions/src/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FunctionsErrorCode } from './public-types';
18+
import { FunctionsErrorCodeCore as FunctionsErrorCode } from './public-types';
1919
import { decode } from './serializer';
2020
import { HttpResponseBody } from './service';
2121
import { FirebaseError } from '@firebase/util';

packages/functions/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
import { registerFunctions } from './config';
2424

2525
export * from './api';
26+
export * from './public-types';
2627

2728
registerFunctions(fetch.bind(self));

packages/functions/src/public-types.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ export interface Functions {
7272
customDomain: string | null;
7373
}
7474

75+
/**
76+
* Functions error code string appended after "functions/" product prefix.
77+
* See {@link FunctionsErrorCode} for full documentation of codes.
78+
* @public
79+
*/
80+
export type FunctionsErrorCodeCore =
81+
| 'ok'
82+
| 'cancelled'
83+
| 'unknown'
84+
| 'invalid-argument'
85+
| 'deadline-exceeded'
86+
| 'not-found'
87+
| 'already-exists'
88+
| 'permission-denied'
89+
| 'resource-exhausted'
90+
| 'failed-precondition'
91+
| 'aborted'
92+
| 'out-of-range'
93+
| 'unimplemented'
94+
| 'internal'
95+
| 'unavailable'
96+
| 'data-loss'
97+
| 'unauthenticated';
98+
7599
/**
76100
* The set of Firebase Functions status codes. The codes are the same at the
77101
* ones exposed by gRPC here:
@@ -112,24 +136,7 @@ export interface Functions {
112136
* credentials for the operation.
113137
* @public
114138
*/
115-
export type FunctionsErrorCode =
116-
| 'ok'
117-
| 'cancelled'
118-
| 'unknown'
119-
| 'invalid-argument'
120-
| 'deadline-exceeded'
121-
| 'not-found'
122-
| 'already-exists'
123-
| 'permission-denied'
124-
| 'resource-exhausted'
125-
| 'failed-precondition'
126-
| 'aborted'
127-
| 'out-of-range'
128-
| 'unimplemented'
129-
| 'internal'
130-
| 'unavailable'
131-
| 'data-loss'
132-
| 'unauthenticated';
139+
export type FunctionsErrorCode = `functions/${FunctionsErrorCodeCore}`;
133140

134141
/**
135142
* An error returned by the Firebase Functions client SDK.

0 commit comments

Comments
 (0)