Skip to content

Commit 16a6ea2

Browse files
committed
fix(runtime-handler): fixes async handler functions thrownin
g errors When a Twilio Function deemed an async function throws an error, the handler doesn't catch it and instead throws an unhandled promise rejection error. Also, when constructing the context and the checks for valid account sids and auth tokens in the getTwilioClient function, we say that it should print the error. However, passing the logger to the context serializes and deserialises it, since it is being passed to another process, and and it is no longer an instance of Logger. This causes another error, trying to call on the logger's error function that no longer exists. So, this PR does 2 things, it checks the handler function to see if it is an async function and calls it with await so that it can catch the error. And it sets shouldPrintMessage to true only if there is a logger object that has an error function.
1 parent 33ca348 commit 16a6ea2

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const handleSuccess = (responseObject?: string | number | boolean | object) => {
6363

6464
process.on(
6565
'message',
66-
({ functionPath, event, config, path }: FunctionRunnerOptions) => {
66+
async ({ functionPath, event, config, path }: FunctionRunnerOptions) => {
6767
try {
6868
setRoutes(config.routes);
6969
constructGlobalScope(config);
@@ -102,7 +102,11 @@ process.on(
102102
`Could not find a "handler" function in file ${functionPath}`
103103
);
104104
}
105-
handler(context, event, callback);
105+
if (handler.constructor.name === 'AsyncFunction') {
106+
await handler(context, event, callback);
107+
} else {
108+
handler(context, event, callback);
109+
}
106110
} catch (err) {
107111
if (process.send) {
108112
process.send({ err: serializeError(err) });

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

+7-6
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';
@@ -157,13 +157,13 @@ export function constructContext<T extends {} = {}>(
157157
}> {
158158
function getTwilioClient(opts?: TwilioClientOptions): TwilioClient {
159159
checkForValidAccountSid(env.ACCOUNT_SID, {
160-
shouldPrintMessage: true,
160+
shouldPrintMessage: logger ? !!logger.error : false,
161161
shouldThrowError: true,
162162
functionName: 'context.getTwilioClient()',
163163
logger: logger,
164164
});
165165
checkForValidAuthToken(env.AUTH_TOKEN, {
166-
shouldPrintMessage: true,
166+
shouldPrintMessage: logger ? !!logger.error : false,
167167
shouldThrowError: true,
168168
functionName: 'context.getTwilioClient()',
169169
logger: logger,
@@ -221,12 +221,13 @@ export function handleError(
221221
err: Error | string | object,
222222
req: ExpressRequest,
223223
res: ExpressResponse,
224-
functionFilePath?: string
224+
functionFilePath?: string,
225+
logger?: LoggerInstance
225226
) {
226227
res.status(500);
227228
if (isError(err)) {
228229
const cleanedupError = cleanUpStackTrace(err);
229-
230+
logger?.error(cleanedupError.toString());
230231
if (req.useragent && (req.useragent.isDesktop || req.useragent.isMobile)) {
231232
res.type('text/html');
232233
res.send(wrapErrorInHtml(cleanedupError, functionFilePath));
@@ -315,7 +316,7 @@ export function functionPathToRoute(
315316
}
316317
if (err) {
317318
const error = deserializeError(err);
318-
handleError(error, req, res, functionPath);
319+
handleError(error, req, res, functionPath, config.logger);
319320
}
320321
if (reply) {
321322
res.status(reply.statusCode);

0 commit comments

Comments
 (0)