Skip to content

Commit 9ad6a08

Browse files
committed
feat: add propagate errors to client cli option
1 parent c33308a commit 9ad6a08

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

src/options.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import * as minimist from 'minimist';
1616
import * as semver from 'semver';
1717
import {resolve} from 'path';
18-
import {SignatureType, isValidSignatureType} from './types';
18+
import {isValidSignatureType, SignatureType} from './types';
1919

2020
/**
2121
* Error thrown when an invalid option is provided.
@@ -60,6 +60,10 @@ export interface FrameworkOptions {
6060
* Routes that should return a 404 without invoking the function.
6161
*/
6262
ignoredRoutes: string | null;
63+
/**
64+
* Whether or not to propagate framework errors to the client.
65+
*/
66+
propagateFrameworkErrors: boolean;
6367
}
6468

6569
/**
@@ -167,6 +171,18 @@ const ExecutionIdOption = new ConfigurableOption(
167171
}
168172
);
169173

174+
const PropagateFrameworkErrorsOption = new ConfigurableOption(
175+
'propagate-framework-errors',
176+
'PROPAGATE_FRAMEWORK_ERRORS',
177+
false,
178+
x => {
179+
return (
180+
(typeof x === 'boolean' && x) ||
181+
(typeof x === 'string' && x.toLowerCase() === 'true')
182+
);
183+
}
184+
);
185+
170186
export const helpText = `Example usage:
171187
functions-framework --target=helloWorld --port=8080
172188
Documentation:
@@ -191,6 +207,7 @@ export const parseOptions = (
191207
SourceLocationOption.cliOption,
192208
TimeoutOption.cliOption,
193209
IgnoredRoutesOption.cliOption,
210+
PropagateFrameworkErrorsOption.cliOption,
194211
],
195212
});
196213
return {
@@ -202,5 +219,9 @@ export const parseOptions = (
202219
printHelp: cliArgs[2] === '-h' || cliArgs[2] === '--help',
203220
enableExecutionId: ExecutionIdOption.parse(argv, envVars),
204221
ignoredRoutes: IgnoredRoutesOption.parse(argv, envVars),
222+
propagateFrameworkErrors: PropagateFrameworkErrorsOption.parse(
223+
argv,
224+
envVars
225+
),
205226
};
206227
};

src/testing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ export const getTestServer = (functionName: string): Server => {
5757
sourceLocation: '',
5858
printHelp: false,
5959
ignoredRoutes: null,
60+
propagateFrameworkErrors: false,
6061
});
6162
};

test/integration/legacy_event.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const testOptions = {
4141
sourceLocation: '',
4242
printHelp: false,
4343
ignoredRoutes: null,
44+
propagateFrameworkErrors: false,
4445
};
4546

4647
describe('Event Function', () => {

test/options.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ describe('parseOptions', () => {
6161
enableExecutionId: false,
6262
timeoutMilliseconds: 0,
6363
ignoredRoutes: null,
64+
propagateFrameworkErrors: false,
6465
},
6566
},
6667
{
@@ -77,6 +78,7 @@ describe('parseOptions', () => {
7778
'--timeout',
7879
'6',
7980
'--ignored-routes=banana',
81+
'--propagate-framework-errors=true',
8082
],
8183
envVars: {},
8284
expectedOptions: {
@@ -88,6 +90,7 @@ describe('parseOptions', () => {
8890
enableExecutionId: false,
8991
timeoutMilliseconds: 6000,
9092
ignoredRoutes: 'banana',
93+
propagateFrameworkErrors: true,
9194
},
9295
},
9396
{
@@ -100,6 +103,7 @@ describe('parseOptions', () => {
100103
FUNCTION_SOURCE: '/source',
101104
CLOUD_RUN_TIMEOUT_SECONDS: '2',
102105
IGNORED_ROUTES: '',
106+
PROPAGATE_FRAMEWORK_ERRORS: 'true',
103107
},
104108
expectedOptions: {
105109
port: '1234',
@@ -110,6 +114,7 @@ describe('parseOptions', () => {
110114
enableExecutionId: false,
111115
timeoutMilliseconds: 2000,
112116
ignoredRoutes: '',
117+
propagateFrameworkErrors: true,
113118
},
114119
},
115120
{
@@ -125,6 +130,7 @@ describe('parseOptions', () => {
125130
'--source=/source',
126131
'--timeout=3',
127132
'--ignored-routes=avocado',
133+
'--propagate-framework-errors',
128134
],
129135
envVars: {
130136
PORT: '4567',
@@ -133,6 +139,7 @@ describe('parseOptions', () => {
133139
FUNCTION_SOURCE: '/somewhere/else',
134140
CLOUD_RUN_TIMEOUT_SECONDS: '5',
135141
IGNORED_ROUTES: 'banana',
142+
PROPAGATE_FRAMEWORK_ERRORS: 'false',
136143
},
137144
expectedOptions: {
138145
port: '1234',
@@ -143,6 +150,7 @@ describe('parseOptions', () => {
143150
enableExecutionId: false,
144151
timeoutMilliseconds: 3000,
145152
ignoredRoutes: 'avocado',
153+
propagateFrameworkErrors: false,
146154
},
147155
},
148156
];
@@ -236,4 +244,33 @@ describe('parseOptions', () => {
236244
});
237245
});
238246
});
247+
248+
it('default disable propagate framework errors', () => {
249+
const options = parseOptions(['bin/node', '/index.js'], {});
250+
assert.strictEqual(options.propagateFrameworkErrors, false);
251+
});
252+
253+
it('disable propagate framework errors by cli flag', () => {
254+
const options = parseOptions(
255+
['bin/node', '/index.js', '--propagate-framework-errors=false'],
256+
{}
257+
);
258+
assert.strictEqual(options.propagateFrameworkErrors, false);
259+
});
260+
261+
it('enable propagate framework errors by cli flag', () => {
262+
const options = parseOptions(
263+
['bin/node', '/index.js', '--propagate-framework-errors=true'],
264+
{}
265+
);
266+
assert.strictEqual(options.propagateFrameworkErrors, true);
267+
});
268+
269+
it('disable propagate framework errors by env var', () => {
270+
const envVars = {
271+
PROPAGATE_FRAMEWORK_ERRORS: 'False',
272+
};
273+
const options = parseOptions(cliOpts, envVars);
274+
assert.strictEqual(options.propagateFrameworkErrors, false);
275+
});
239276
});

0 commit comments

Comments
 (0)