Skip to content

Commit f58d6aa

Browse files
B4nanclaude
andcommitted
fix: handle AuthError in exception handler, assert 401 in tests
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c271c00 commit f58d6aa

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

app/exceptions/handler.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import app from '@adonisjs/core/services/app'
22
import { HttpContext, ExceptionHandler } from '@adonisjs/core/http'
33
import { NotFoundError } from '@mikro-orm/core'
4+
import { AuthError } from '#repositories/user_repository'
45

56
export default class HttpExceptionHandler extends ExceptionHandler {
67
protected debug = !app.inProduction
78

89
async handle(error: unknown, ctx: HttpContext) {
9-
// handle MikroORM's NotFoundError (from `findOneOrFail`)
10+
if (error instanceof AuthError) {
11+
ctx.response.status(401).send({ error: error.message })
12+
return
13+
}
14+
1015
if (error instanceof NotFoundError) {
1116
ctx.response.status(404).send({ error: error.message })
1217
return
@@ -16,6 +21,10 @@ export default class HttpExceptionHandler extends ExceptionHandler {
1621
}
1722

1823
async report(error: unknown, ctx: HttpContext) {
24+
if (error instanceof AuthError) {
25+
return
26+
}
27+
1928
return super.report(error, ctx)
2029
}
2130
}

tests/functional/article.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ test.group('Article', () => {
2828
text: 'Article content',
2929
})
3030

31-
response.assertStatus(500)
31+
response.assertStatus(401)
32+
response.assertBodyContains({ error: 'Please provide your token via Authorization header' })
3233
})
3334
})

0 commit comments

Comments
 (0)