Skip to content

Commit 926cb39

Browse files
committed
fix(@twilio/runtime-handler): add cross-fork logger instance for local dev
1 parent 9211e97 commit 926cb39

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

Diff for: .changeset/forty-snails-rescue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@twilio/runtime-handler': patch
3+
---
4+
5+
Fix error messages in local development
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { LoggingLevel, LoggingLevelValue } from 'twilio-run/dist/utils/logger';
2+
import { LoggerInstance } from '../types';
3+
4+
export class CrossForkLogger implements LoggerInstance {
5+
constructor() {}
6+
7+
debug(msg: string) {
8+
this.sendLog('debug', msg);
9+
}
10+
11+
info(msg: string) {
12+
this.sendLog('info', msg);
13+
}
14+
15+
warn(msg: string, title: string = '') {
16+
this.sendLog('warn', msg, title);
17+
}
18+
19+
error(msg: string, title: string = '') {
20+
this.sendLog('error', msg, title);
21+
}
22+
23+
log(msg: string, level: LoggingLevelValue) {
24+
level = level || LoggingLevel.info;
25+
26+
this.sendLog('log', msg, level);
27+
}
28+
29+
private sendLog(level: keyof LoggerInstance, ...args: (string | number)[]) {
30+
process.send &&
31+
process.send({
32+
crossForkLogMessage: {
33+
level,
34+
args: args,
35+
},
36+
});
37+
}
38+
}

Diff for: packages/runtime-handler/src/dev-runtime/internal/functionRunner.ts

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isTwiml,
88
} from '../route';
99
import { ServerConfig, Headers } from '../types';
10+
import { CrossForkLogger } from './crossForkLogger';
1011
import { Response } from './response';
1112
import { setRoutes } from './route-cache';
1213

@@ -61,12 +62,19 @@ const handleSuccess = (responseObject?: string | number | boolean | object) => {
6162
}
6263
};
6364

65+
process.on('uncaughtException', (err, origin) => {
66+
if (process.send) {
67+
process.send({ err: serializeError(err) });
68+
}
69+
});
70+
6471
process.on(
6572
'message',
6673
({ functionPath, event, config, path }: FunctionRunnerOptions) => {
6774
try {
6875
setRoutes(config.routes);
6976
constructGlobalScope(config);
77+
config.logger = new CrossForkLogger();
7078
let context = constructContext(config, path);
7179
sendDebugMessage('Context for %s: %p', path, context);
7280
context = augmentContextWithOptionals(config, context);

Diff for: packages/runtime-handler/src/dev-runtime/route.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import { Reply } from './internal/functionRunner';
2727
import { Response } from './internal/response';
2828
import * as Runtime from './internal/runtime';
29-
import { ServerConfig } from './types';
29+
import { LoggerInstance, ServerConfig } from './types';
3030
import debug from './utils/debug';
3131
import { wrapErrorInHtml } from './utils/error-html';
3232
import { getCodeLocation } from './utils/getCodeLocation';
@@ -303,20 +303,41 @@ export function functionPathToRoute(
303303
reply,
304304
debugMessage,
305305
debugArgs = [],
306+
crossForkLogMessage,
306307
}: {
307308
err?: Error | number | string;
308309
reply?: Reply;
309310
debugMessage?: string;
310311
debugArgs?: any[];
312+
crossForkLogMessage?: {
313+
level: keyof LoggerInstance;
314+
args: [string] | [string, number] | [string, string];
315+
};
311316
}) => {
312317
if (debugMessage) {
313318
log(debugMessage, ...debugArgs);
314319
return;
315320
}
321+
322+
if (crossForkLogMessage) {
323+
if (
324+
config.logger &&
325+
typeof config.logger[crossForkLogMessage.level] === 'function'
326+
) {
327+
config.logger[crossForkLogMessage.level](
328+
// @ts-ignore
329+
...crossForkLogMessage.args
330+
);
331+
}
332+
return;
333+
}
334+
316335
if (err) {
317336
const error = deserializeError(err);
318337
handleError(error, req, res, functionPath);
338+
return;
319339
}
340+
320341
if (reply) {
321342
res.status(reply.statusCode);
322343
res.set(reply.headers);

0 commit comments

Comments
 (0)