Skip to content

Commit

Permalink
Error handler to catch all unhandled exceptions and log with executio…
Browse files Browse the repository at this point in the history
…n id.
  • Loading branch information
liuyunnnn committed Feb 27, 2024
1 parent 37eebd7 commit 79270d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,20 @@ export function sendCrashResponse({
}

export function loggingHandlerAddExecutionContext() {
interceptStdoutWrite();
interceptStderrWrite();
}

function interceptStdoutWrite() {
const originalStdoutWrite = process.stdout.write;
process.stdout.write = (data, ...args) => {
const {encoding, cb} = splitArgs(args);
const modifiedData = getModifiedData(data, encoding);
return originalStdoutWrite.apply(process.stdout, [modifiedData, cb]);
};
}

function interceptStderrWrite() {
const originalStderrWrite = process.stderr.write;
process.stderr.write = (data, ...args) => {
const {encoding, cb} = splitArgs(args);
Expand All @@ -83,6 +90,17 @@ export function loggingHandlerAddExecutionContext() {
};
}

export const errorHandler = (
err: Error | any,

Check warning on line 94 in src/logger.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
req: express.Request,
res: express.Response,
next: express.NextFunction

Check warning on line 97 in src/logger.ts

View workflow job for this annotation

GitHub Actions / lint

'next' is defined but never used
) => {
interceptStderrWrite();
res.status(500);
res.render('error', {error: err});
};

export function splitArgs(args: any[]) {

Check warning on line 104 in src/logger.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
let encoding, cb;
if (
Expand Down
13 changes: 6 additions & 7 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as http from 'http';
import * as onFinished from 'on-finished';
import * as semver from 'semver';
import {HandlerFunction, Request, Response} from './functions';
import {SignatureType} from './types';
import {setLatestRes} from './invoker';
Expand All @@ -25,7 +24,7 @@ import {cloudEventToBackgroundEventMiddleware} from './middleware/cloud_event_to
import {backgroundEventToCloudEventMiddleware} from './middleware/background_event_to_cloud_event';
import {wrapUserFunction} from './function_wrappers';
import {executionContextMiddleware} from './execution_context';
import {requriedNodeJsVersion} from './options';
import {errorHandler} from './logger';

/**
* Creates and configures an Express application and returns an HTTP server
Expand Down Expand Up @@ -113,11 +112,8 @@ export function getServer(
// Disable Express eTag response header
app.set('etag', false);

// Stores execution context to asyncLocalStorage.
// asyncLocalStorage is introduced to Node.js on 12.17.0
if (semver.gte(process.versions.node, requriedNodeJsVersion)) {
app.use(executionContextMiddleware);
}
// Get execution context.
app.use(executionContextMiddleware);

if (
functionSignatureType === 'event' ||
Expand Down Expand Up @@ -159,5 +155,8 @@ export function getServer(
app.post('/*', requestHandler);
}

// Error Handler
app.use(errorHandler);

return http.createServer(app);
}

0 comments on commit 79270d5

Please sign in to comment.