Skip to content

Commit f13dc42

Browse files
committedSep 21, 2022
fix(serve): add error handler for auth route middleware
1 parent 04eb9f4 commit f13dc42

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed
 

‎packages/serve/src/lib/middleware/auth/authRouteMiddleware.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,15 @@ export class AuthRouteMiddleware extends BuiltInMiddleware<AuthOptions> {
9898
return;
9999
}
100100
// type does not support
101-
const result = await this.authenticators[type].authIdentity(context);
102-
context.body = result;
103-
return;
101+
try {
102+
const result = await this.authenticators[type].authIdentity(context);
103+
context.body = result;
104+
} catch (err) {
105+
context.status = 400;
106+
context.body = {
107+
message: (err as Error).message,
108+
};
109+
}
104110
});
105111
}
106112
}

‎packages/serve/test/middlewares/built-in-middlewares/authRouteMiddleware.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,30 @@ describe('Test auth route middleware', () => {
182182
expect(response.body).toEqual(expected);
183183
server.close();
184184
});
185+
186+
it('Should failed when call GET /auth?type=a3 but auth identity "a3" failed', async () => {
187+
// Arrange
188+
const error = new Error('please provide "token"');
189+
const expected = {
190+
message: error.message,
191+
};
192+
const stubAuthenticator = sinon.stubInterface<BaseAuthenticator<any>>();
193+
stubAuthenticator.authIdentity.rejects(error);
194+
stubAuthenticator.getExtensionId.returns('a3');
195+
const server = await runServer(
196+
{ a3: {} },
197+
{
198+
a3: stubAuthenticator,
199+
}
200+
);
201+
202+
// Act
203+
const request = supertest(server).get(`/auth?type=a3`);
204+
const response = await request;
205+
206+
// Assert
207+
expect(response.statusCode).toEqual(400);
208+
expect(response.body).toEqual(expected);
209+
server.close();
210+
});
185211
});

0 commit comments

Comments
 (0)
Please sign in to comment.