Skip to content

fix: do not send error when error code is fine #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as http from 'http';
import * as onFinished from 'on-finished';

import {FUNCTION_STATUS_HEADER_FIELD} from './types';
import {logAndSendError} from './logger';
import {sendCrashResponse} from './logger';
import {isBinaryCloudEvent, getBinaryCloudEventContext} from './cloudevents';
import {
HttpFunction,
Expand Down Expand Up @@ -111,7 +111,7 @@ function makeHttpHandler(execute: HttpFunction): express.RequestHandler {
console.error(`Exception from a finished function: ${err}`);
} else {
res.locals.functionExecutionFinished = true;
logAndSendError(err, res);
sendCrashResponse({err, res});
}
});
d.run(() => {
Expand Down Expand Up @@ -305,16 +305,20 @@ export class ErrorHandler {
register() {
process.on('uncaughtException', err => {
console.error('Uncaught exception');
logAndSendError(err, latestRes, killInstance);
sendCrashResponse({err, res: latestRes, callback: killInstance});
});

process.on('unhandledRejection', err => {
console.error('Unhandled rejection');
logAndSendError(err, latestRes, killInstance);
sendCrashResponse({err, res: latestRes, callback: killInstance});
});

process.on('exit', code => {
logAndSendError(new Error(`Process exited with code ${code}`), latestRes);
sendCrashResponse({
err: new Error(`Process exited with code ${code}`),
res: latestRes,
silent: code === 0,
});
});

['SIGINT', 'SIGTERM'].forEach(signal => {
Expand Down
20 changes: 14 additions & 6 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ import {FUNCTION_STATUS_HEADER_FIELD} from './types';
* @param res Express response object.
* @param callback A function to be called synchronously.
*/
export function logAndSendError(
export function sendCrashResponse({
err,
res,
callback,
silent = false,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
err: Error | any,
res: express.Response | null,
callback?: Function
) {
console.error(err.stack || err);
err: Error | any;
res: express.Response | null;
callback?: Function;
silent?: boolean;
}) {
if (!silent) {
console.error(err.stack || err);
}

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