Skip to content

Commit

Permalink
feat: add support for self rendering custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 9, 2024
1 parent dcd5f89 commit 278fdf1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/exception_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export class ExceptionHandler {
return
}

/**
* Allow errors to be self handled.
*/
if ('render' in error && typeof error.render === 'function') {
return error.render(error, kernel)
}

/**
* Log error message only when not in debug mode
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/exception_handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,22 @@ test.group('Exception handler', () => {
assert.equal(logs[0].stream, 'stderr')
assert.equal(logs[0].message, 'bgRed(white( ERROR )) Something went wrong')
})

test('allow errors to be self handled', async ({ assert }) => {
const kernel = Kernel.create()
kernel.ui.switchMode('raw')

class CustomError extends Exception {
render(error: CustomError, $kernel: Kernel<any>) {
$kernel.ui.logger.logError(`custom: ${error.message}`)
}
}

await new ExceptionHandler().render(new CustomError('Something went wrong'), kernel)

const logs = kernel.ui.logger.getLogs()
assert.lengthOf(logs, 1)
assert.equal(logs[0].stream, 'stderr')
assert.equal(logs[0].message, 'custom: Something went wrong')
})
})

0 comments on commit 278fdf1

Please sign in to comment.