Skip to content

Commit

Permalink
Better error reporting from lambdas (#624)
Browse files Browse the repository at this point in the history
* feat: report if function times out

* chore: include lambda requestId in event
  • Loading branch information
niekcandaele authored Sep 17, 2023
1 parent d71de54 commit 8762435
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/app-api/src/executors/executeFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface ILog {
interface IFunctionResult {
success: boolean;
logs: ILog[];
requestId?: string;
}

export type FunctionExecutor = (code: string, data: Record<string, unknown>, token: string) => Promise<IFunctionResult>;
Expand Down
26 changes: 24 additions & 2 deletions packages/lib-aws/src/executeLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ interface executeLambdaOpts {
token: string;
}

interface IFunctionResult {
success: boolean;
logs: any[];
requestId?: string;
}

const lambda = new Lambda({
region: config.get('aws.region'),
credentials: {
Expand Down Expand Up @@ -45,10 +51,26 @@ export async function executeLambda({ data, fn, token, domainId }: executeLambda

async function tryExecuteLambda({ data, fn, token, domainId }: executeLambdaOpts) {
const result = await lambda.invoke({ FunctionName: domainId, Payload: JSON.stringify({ data, token, fn }) });
let returnVal: IFunctionResult = {
requestId: result.$metadata.requestId,
logs: [],
success: false,
};

if (result.Payload) {
const tmpResult = Buffer.from(result.Payload).toString();
const parsedRes = JSON.parse(tmpResult);
const parsedBody = JSON.parse(parsedRes.body);
return parsedBody;

if (parsedRes.errorMessage && parsedRes.errorMessage.includes('Task timed out')) {
returnVal['success'] = false;
returnVal['logs'] = [
{
msg: 'Task timed out',
},
];
} else {
returnVal = JSON.parse(parsedRes.body);
}
}
return returnVal;
}

0 comments on commit 8762435

Please sign in to comment.