Skip to content

Commit 728a8a3

Browse files
fix: catch errors in middleware (#942)
* fix: catch uncaught errors in express middleware * chore: add changeset --------- Co-authored-by: Larry Singh <iamlarrysingh@gmail.com>
1 parent 15b7ff4 commit 728a8a3

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

.changeset/silent-jars-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@logto/express": patch
3+
---
4+
5+
catch error in withLogto middleware

packages/express/src/index.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,18 @@ export const handleAuthRoutes = (config: LogtoExpressConfig): Router => {
116116
export const withLogto =
117117
(config: LogtoExpressConfig): Middleware =>
118118
async (request: IncomingMessage, response: Response, next: NextFunction) => {
119-
const client = createNodeClient(request, response, config);
120-
const user = await client.getContext({
121-
getAccessToken: config.getAccessToken,
122-
resource: config.resource,
123-
fetchUserInfo: config.fetchUserInfo,
124-
getOrganizationToken: config.getOrganizationToken,
125-
});
126-
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
127-
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
128-
next();
119+
try {
120+
const client = createNodeClient(request, response, config);
121+
const user = await client.getContext({
122+
getAccessToken: config.getAccessToken,
123+
resource: config.resource,
124+
fetchUserInfo: config.fetchUserInfo,
125+
getOrganizationToken: config.getOrganizationToken,
126+
});
127+
// eslint-disable-next-line @silverhand/fp/no-mutating-methods
128+
Object.defineProperty(request, 'user', { enumerable: true, get: () => user });
129+
next();
130+
} catch (error) {
131+
next(error);
132+
}
129133
};

0 commit comments

Comments
 (0)