42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { test } from '@japa/runner'
|
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
import { FileNotFoundException } from '#exceptions/file_manager_exceptions'
|
|
import { PageRouteConflictException } from '#exceptions/web_manager_exceptions'
|
|
|
|
function captureContext() {
|
|
const captured: { status?: number; body?: unknown } = {}
|
|
const response = {
|
|
status(code: number) {
|
|
captured.status = code
|
|
return response
|
|
},
|
|
send(body: unknown) {
|
|
captured.body = body
|
|
},
|
|
}
|
|
return { captured, ctx: { response } as unknown as HttpContext }
|
|
}
|
|
|
|
test.group('API exceptions', () => {
|
|
test('page manager exceptions self-render as message and code', async ({ assert }) => {
|
|
const { captured, ctx } = captureContext()
|
|
const error = new PageRouteConflictException('en', 'about')
|
|
await error.handle(error, ctx)
|
|
|
|
assert.equal(captured.status, 409)
|
|
assert.deepEqual(captured.body, {
|
|
message: 'A page already uses /en/about',
|
|
code: 'E_PAGE_ROUTE_CONFLICT',
|
|
})
|
|
})
|
|
|
|
test('file manager exceptions self-render as message and code', async ({ assert }) => {
|
|
const { captured, ctx } = captureContext()
|
|
const error = new FileNotFoundException()
|
|
await error.handle(error, ctx)
|
|
|
|
assert.equal(captured.status, 404)
|
|
assert.deepEqual(captured.body, { message: 'File not found', code: 'E_FILE_NOT_FOUND' })
|
|
})
|
|
})
|