Skip to content

Commit d616c4c

Browse files
authored
fix: do not send error when error code is fine (#187)
* fix: do not send error when error code is fine Signed-off-by: Grant Timmerman <[email protected]> * fix: silent exit on process exit Signed-off-by: Grant Timmerman <[email protected]> * fix: fix logic to only be silent when code is 0 Signed-off-by: Grant Timmerman <[email protected]>
1 parent b4fa7c0 commit d616c4c

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/invoker.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as http from 'http';
2828
import * as onFinished from 'on-finished';
2929

3030
import {FUNCTION_STATUS_HEADER_FIELD} from './types';
31-
import {logAndSendError} from './logger';
31+
import {sendCrashResponse} from './logger';
3232
import {isBinaryCloudEvent, getBinaryCloudEventContext} from './cloudevents';
3333
import {
3434
HttpFunction,
@@ -111,7 +111,7 @@ function makeHttpHandler(execute: HttpFunction): express.RequestHandler {
111111
console.error(`Exception from a finished function: ${err}`);
112112
} else {
113113
res.locals.functionExecutionFinished = true;
114-
logAndSendError(err, res);
114+
sendCrashResponse({err, res});
115115
}
116116
});
117117
d.run(() => {
@@ -305,16 +305,20 @@ export class ErrorHandler {
305305
register() {
306306
process.on('uncaughtException', err => {
307307
console.error('Uncaught exception');
308-
logAndSendError(err, latestRes, killInstance);
308+
sendCrashResponse({err, res: latestRes, callback: killInstance});
309309
});
310310

311311
process.on('unhandledRejection', err => {
312312
console.error('Unhandled rejection');
313-
logAndSendError(err, latestRes, killInstance);
313+
sendCrashResponse({err, res: latestRes, callback: killInstance});
314314
});
315315

316316
process.on('exit', code => {
317-
logAndSendError(new Error(`Process exited with code ${code}`), latestRes);
317+
sendCrashResponse({
318+
err: new Error(`Process exited with code ${code}`),
319+
res: latestRes,
320+
silent: code === 0,
321+
});
318322
});
319323

320324
['SIGINT', 'SIGTERM'].forEach(signal => {

src/logger.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ import {FUNCTION_STATUS_HEADER_FIELD} from './types';
2222
* @param res Express response object.
2323
* @param callback A function to be called synchronously.
2424
*/
25-
export function logAndSendError(
25+
export function sendCrashResponse({
26+
err,
27+
res,
28+
callback,
29+
silent = false,
30+
}: {
2631
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27-
err: Error | any,
28-
res: express.Response | null,
29-
callback?: Function
30-
) {
31-
console.error(err.stack || err);
32+
err: Error | any;
33+
res: express.Response | null;
34+
callback?: Function;
35+
silent?: boolean;
36+
}) {
37+
if (!silent) {
38+
console.error(err.stack || err);
39+
}
3240

3341
// If user function has already sent response headers, the response with
3442
// error message cannot be sent. This check is done inside the callback,

0 commit comments

Comments
 (0)