Skip to content
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

Add cross-fork logger for runtime-handler #459

Merged
merged 2 commits into from
Mar 26, 2024
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
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);
}
Comment on lines +22 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious, when do we use a log level? Also, what does level represent here?


private sendLog(level: keyof LoggerInstance, ...args: (string | number)[]) {
process.send &&
process.send({
crossForkLogMessage: {
level,
args: args,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args: 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];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't imagine these ever changing, but does it make sense to colocation the potential args shapes with the LoggerInstance type? In case the logger instance type is used similarly elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args in the sendLog has the type (string | number)[] . This was cleaner to read.

};
}) => {
if (debugMessage) {
log(debugMessage, ...debugArgs);
return;
}

if (crossForkLogMessage) {
if (
config.logger &&
typeof config.logger[crossForkLogMessage.level] === 'function'
) {
Comment on lines +321 to +326
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: crossForkLogMessage && config.logger && typeof config.logger[crossForkLogMessage.level] === 'function'

will typeof config.logger[crossForkLogMessage.level] ever return something other than a function?

config.logger[crossForkLogMessage.level](
// @ts-ignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too happy about this but typing here would have been quite the pain and in my opinion would distract from the fact that this is just a pass through.

...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
Loading