Skip to content

Commit bf3dfe4

Browse files
committed
refactor: cleanup exceptions
1 parent 15e1a2f commit bf3dfe4

File tree

9 files changed

+38
-79
lines changed

9 files changed

+38
-79
lines changed

index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,4 @@ export { defineConfig } from './src/define_config.js'
1818
export { RouteResource } from './src/router/resource.js'
1919
export { BriskRoute } from './src/router/brisk.js'
2020
export { HttpContext } from './src/http_context/main.js'
21-
export { HttpException } from './src/exceptions/http_exception.js'
22-
export { AbortException } from './src/exceptions/abort_exception.js'
23-
export { RouteNotFoundException } from './src/exceptions/route_not_found.js'
24-
export { CannotLookupRouteException } from './src/exceptions/cannot_lookup_route.js'
21+
export * as errors from './src/exceptions/main.js'

src/exceptions/abort_exception.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/exceptions/cannot_lookup_route.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import { Exception } from '@poppinss/utils'
10+
import { createError, Exception } from '@poppinss/utils'
11+
import type { HttpContext } from '../http_context/main.js'
1112

12-
/**
13-
* Exception class to represent an HTTP exception
14-
*/
15-
export class HttpException extends Exception {
13+
export const E_ROUTE_NOT_FOUND = createError<[method: string, url: string]>(
14+
'Cannot %s:%s',
15+
'E_ROUTE_NOT_FOUND',
16+
404
17+
)
18+
19+
export const E_CANNOT_LOOKUP_ROUTE = createError<[routeIdentifier: string]>(
20+
'Cannot lookup route "%s"',
21+
'E_CANNOT_LOOKUP_ROUTE',
22+
500
23+
)
24+
25+
export const E_HTTP_EXCEPTION = class HttpException extends Exception {
1626
body: any
1727
static code = 'E_HTTP_EXCEPTION'
1828

@@ -37,3 +47,9 @@ export class HttpException extends Exception {
3747
return error
3848
}
3949
}
50+
51+
export const E_HTTP_REQUEST_ABORTED = class AbortException extends E_HTTP_EXCEPTION {
52+
handle(error: AbortException, ctx: HttpContext) {
53+
ctx.response.status(error.status).send(error.body)
54+
}
55+
}

src/exceptions/route_not_found.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/response.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { Redirect } from './redirect.js'
2828
import type { Router } from './router/main.js'
2929
import type { HttpContext } from './http_context/main.js'
3030
import { CookieSerializer } from './cookies/serializer.js'
31-
import { AbortException } from './exceptions/abort_exception.js'
31+
import { E_HTTP_REQUEST_ABORTED } from './exceptions/main.js'
3232
import type {
3333
CastableHeader,
3434
CookieOptions,
@@ -927,7 +927,7 @@ export class Response extends Macroable {
927927
* used when status is not defined
928928
*/
929929
abort(body: any, status?: number): never {
930-
throw AbortException.invoke(body, status || 400)
930+
throw E_HTTP_REQUEST_ABORTED.invoke(body, status || 400)
931931
}
932932

933933
/**

src/router/lookup_store/route_finder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import * as errors from '../../exceptions/main.js'
1011
import type { RouteJSON } from '../../types/route.js'
11-
import { CannotLookupRouteException } from '../../exceptions/cannot_lookup_route.js'
1212

1313
/**
1414
* Route finder is used to find a route by its name, route pattern
@@ -45,7 +45,7 @@ export class RouteFinder {
4545
findOrFail(routeIdentifier: string): RouteJSON {
4646
const route = this.find(routeIdentifier)
4747
if (!route) {
48-
throw new CannotLookupRouteException(`Cannot lookup route "${routeIdentifier}"`)
48+
throw new errors.E_CANNOT_LOOKUP_ROUTE([routeIdentifier])
4949
}
5050

5151
return route

src/server/factories/final_handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
import type { ContainerResolver } from '@adonisjs/fold'
1111

12+
import * as errors from '../../exceptions/main.js'
1213
import type { Router } from '../../router/main.js'
1314
import type { HttpContext } from '../../http_context/main.js'
14-
import { RouteNotFoundException } from '../../exceptions/route_not_found.js'
1515

1616
/**
1717
* The final handler is executed after the server middleware stack.
@@ -33,6 +33,6 @@ export function finalHandler(router: Router, resolver: ContainerResolver<any>, c
3333
return route.route.execute(route.route, resolver, ctx)
3434
}
3535

36-
return Promise.reject(new RouteNotFoundException(`Cannot ${method}:${url}`))
36+
return Promise.reject(new errors.E_ROUTE_NOT_FOUND([method, url]))
3737
}
3838
}

tests/exceptions/http_exception.spec.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
*/
99

1010
import { test } from '@japa/runner'
11-
import { HttpException } from '../../src/exceptions/http_exception.js'
11+
import { E_HTTP_EXCEPTION } from '../../src/exceptions/main.js'
1212

1313
test.group('Http exception', () => {
1414
test('create http exception with an error object', ({ assert }) => {
1515
const error = new Error('Something went wrong')
16-
const exception = HttpException.invoke(new Error('Something went wrong'), 500)
16+
const exception = E_HTTP_EXCEPTION.invoke(new Error('Something went wrong'), 500)
1717

1818
assert.deepEqual(exception.body, error)
1919
assert.equal(exception.message, 'Something went wrong')
@@ -22,7 +22,7 @@ test.group('Http exception', () => {
2222
})
2323

2424
test('create http exception with a string message', ({ assert }) => {
25-
const exception = HttpException.invoke('Something went wrong', 500)
25+
const exception = E_HTTP_EXCEPTION.invoke('Something went wrong', 500)
2626

2727
assert.deepEqual(exception.body, 'Something went wrong')
2828
assert.equal(exception.message, 'Something went wrong')
@@ -31,7 +31,7 @@ test.group('Http exception', () => {
3131
})
3232

3333
test('create http exception with an error of values', ({ assert }) => {
34-
const exception = HttpException.invoke([{ message: 'Something went wrong' }], 500)
34+
const exception = E_HTTP_EXCEPTION.invoke([{ message: 'Something went wrong' }], 500)
3535

3636
assert.deepEqual(exception.body, [{ message: 'Something went wrong' }])
3737
assert.equal(exception.message, 'HTTP Exception')
@@ -40,7 +40,10 @@ test.group('Http exception', () => {
4040
})
4141

4242
test('create http exception with an object without message', ({ assert }) => {
43-
const exception = HttpException.invoke({ errors: [{ message: 'Something went wrong' }] }, 500)
43+
const exception = E_HTTP_EXCEPTION.invoke(
44+
{ errors: [{ message: 'Something went wrong' }] },
45+
500
46+
)
4447

4548
assert.deepEqual(exception.body, { errors: [{ message: 'Something went wrong' }] })
4649
assert.equal(exception.message, 'HTTP Exception')
@@ -49,7 +52,7 @@ test.group('Http exception', () => {
4952
})
5053

5154
test('create http exception with null body', ({ assert }) => {
52-
const exception = HttpException.invoke(null, 500)
55+
const exception = E_HTTP_EXCEPTION.invoke(null, 500)
5356

5457
assert.deepEqual(exception.body, 'Internal server error')
5558
assert.equal(exception.message, 'HTTP Exception')
@@ -58,7 +61,7 @@ test.group('Http exception', () => {
5861
})
5962

6063
test('create http exception with undefined body', ({ assert }) => {
61-
const exception = HttpException.invoke(undefined, 500)
64+
const exception = E_HTTP_EXCEPTION.invoke(undefined, 500)
6265

6366
assert.deepEqual(exception.body, 'Internal server error')
6467
assert.equal(exception.message, 'HTTP Exception')

0 commit comments

Comments
 (0)