Skip to content

Commit

Permalink
Ensure authorizer and requestContext are always populated.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Sep 9, 2023
1 parent 8dbe91a commit 9f75e4d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,18 @@ class ApiFactory {
event.stageVariables = event.stageVariables || {};
event.pathParameters = event.pathParameters || {};

const method = event.httpMethod || event.requestContext && event.requestContext.http && event.requestContext.http.method;
if (!event.requestContext) {
event.requestContext = {};
}
event.requestContext.authorizer = event.requestContext.authorizer || {};

const method = event.httpMethod || event.requestContext.http && event.requestContext.http.method;
let definedRoute = null;

const proxyPath = '/{proxy+}';
event.path = event.path || event.requestContext && event.requestContext.http && event.requestContext.http.path || event.requestContext.path;
event.path = event.path || event.requestContext.http && event.requestContext.http.path || event.requestContext.path;
// Remove stage from Path
event.path = event.requestContext && event.path.startsWith(`/${event.requestContext.stage}`) ? event.path.substring(event.requestContext.stage.length + 1) : event.path;
event.path = event.path.startsWith(`/${event.requestContext.stage}`) ? event.path.substring(event.requestContext.stage.length + 1) : event.path;

// The replace handles cases where the route key is prepended with a method from API Gateway
const routeKey = event.routeKey && event.routeKey.replace(/^[A-Z]+\s/, '') || event.resource;
Expand Down Expand Up @@ -193,12 +198,12 @@ class ApiFactory {
if (error instanceof Resp) { return error; }
if (error instanceof Error) {
apiFactory.logger({ level: 'ERROR', title: 'Exception thrown by invocation of the runtime lambda function, check the implementation.', api: definedRoute, error: e });
return new Resp({ title: 'Unexpected error', errorId: event.requestContext && event.requestContext.requestId }, 500);
return new Resp({ title: 'Unexpected error', errorId: event.requestContext.requestId }, 500);
}
return new Resp(error, error && error.statusCode ? null : 500);
} catch (middleE) {
apiFactory.logger({ level: 'ERROR', title: 'Exception thrown by invocation of the error middleware, check the implementation.', api: definedRoute, error: e, middleware: middleE });
return new Resp({ title: 'Unexpected error', errorId: event.requestContext && event.requestContext.requestId }, 500);
return new Resp({ title: 'Unexpected error', errorId: event.requestContext.requestId }, 500);
}
}
}
Expand Down

0 comments on commit 9f75e4d

Please sign in to comment.