Skip to content

Commit

Permalink
fix(@twilio/runtime-handler): add cross-fork logger instance for loca…
Browse files Browse the repository at this point in the history
…l dev (#459)

Co-authored-by: Victor A <33580233+victoray@users.noreply.github.com>
  • Loading branch information
dkundel and victoray committed Mar 26, 2024
1 parent 4432c02 commit 7bebf6f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/forty-snails-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@twilio/runtime-handler': patch
---

Fix error messages in local development
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { LoggerInstance } from '../types';

export class CrossForkLogger implements LoggerInstance {
constructor() {}

debug(msg: string) {
this.sendLog('debug', msg);
}

info(msg: string) {
this.sendLog('info', msg);
}

warn(msg: string, title: string = '') {
this.sendLog('warn', msg, title);
}

error(msg: string, title: string = '') {
this.sendLog('error', msg, title);
}

log(msg: string, level: number) {
this.sendLog('log', msg, level);
}

private sendLog(level: keyof LoggerInstance, ...args: (string | number)[]) {
process.send &&
process.send({
crossForkLogMessage: {
level,
args: args,
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isTwiml,
} from '../route';
import { ServerConfig, Headers } from '../types';
import { CrossForkLogger } from './crossForkLogger';
import { Response } from './response';
import { setRoutes } from './route-cache';

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

process.on('uncaughtException', (err, origin) => {
if (process.send) {
process.send({ err: serializeError(err) });
}
});

process.on(
'message',
({ functionPath, event, config, path }: FunctionRunnerOptions) => {
try {
setRoutes(config.routes);
constructGlobalScope(config);
config.logger = new CrossForkLogger();
let context = constructContext(config, path);
sendDebugMessage('Context for %s: %p', path, context);
context = augmentContextWithOptionals(config, context);
Expand Down
23 changes: 22 additions & 1 deletion packages/runtime-handler/src/dev-runtime/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { Reply } from './internal/functionRunner';
import { Response } from './internal/response';
import * as Runtime from './internal/runtime';
import { ServerConfig } from './types';
import { LoggerInstance, ServerConfig } from './types';
import debug from './utils/debug';
import { wrapErrorInHtml } from './utils/error-html';
import { getCodeLocation } from './utils/getCodeLocation';
Expand Down Expand Up @@ -303,25 +303,46 @@ export function functionPathToRoute(
reply,
debugMessage,
debugArgs = [],
crossForkLogMessage,
}: {
err?: Error | number | string;
reply?: Reply;
debugMessage?: string;
debugArgs?: any[];
crossForkLogMessage?: {
level: keyof LoggerInstance;
args: [string] | [string, number] | [string, string];
};
}) => {
if (debugMessage) {
log(debugMessage, ...debugArgs);
return;
}

if (crossForkLogMessage) {
if (
config.logger &&
typeof config.logger[crossForkLogMessage.level] === 'function'
) {
config.logger[crossForkLogMessage.level](
// @ts-ignore
...crossForkLogMessage.args
);
}
return;
}

if (err) {
const error = deserializeError(err);
handleError(error, req, res, functionPath);
}

if (reply) {
res.status(reply.statusCode);
res.set(reply.headers);
res.send(reply.body);
}

forked.kill();
}
);
Expand Down

0 comments on commit 7bebf6f

Please sign in to comment.