File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import app from '@adonisjs/core/services/app'
22import { HttpContext , ExceptionHandler } from '@adonisjs/core/http'
33import { NotFoundError } from '@mikro-orm/core'
4+ import { AuthError } from '#repositories/user_repository'
45
56export 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}
Original file line number Diff line number Diff 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} )
You can’t perform that action at this time.
0 commit comments