Error Handling
Introduction
All exceptions thrown by your application will be reported by the Formidable framework. However, you may wish to define your own exception reporting behavior, including logging exceptions or sending exceptions to an external service like Sentry or Bugsnag.
Configuration
The debug option in your config/app.imba or config/app.ts configuration file determines how errors are displayed by your application. By default, this option is set to true, which results in detailed error messages being displayed. In production applications, it's recommended to set this value to false.
The Exception Handler
Logging
By default, the Formidable Framework will log all exceptions excluding HttpException based exceptions to Bugsnag. You can exclude your own exceptions by adding a dontReport getter in the app/Exceptions/Handler.imba or app/Exceptions/Handler.ts file:
- Imba
- TypeScript
import { ExceptionHandler, HttpException } from '@formidablejs/framework'
export class Handler < ExceptionHandler
get dontReport
[
HttpException
]
import { ExceptionHandler, HttpException } from '@formidablejs/framework'
export class Handler extends ExceptionHandler {
get dontReport() {
return [
HttpException
]
}
}
Handling
You can handle exceptions by adding a handle method in the app/Exceptions/Handler.imba or app/Exceptions/Handler.ts file. Out of the box, you don't need to add a handle method as the framework will handle all exceptions for you. However, if you want to add your own custom exception handling, you can do so by adding a handle method:
- Imba
- TypeScript
import { ExceptionHandler, NotFoundException, view } from '@formidablejs/framework'
import { NotFound } from '../../resources/views/errors/NotFound'
export class Handler < ExceptionHandler
def handle exception\Error
if exception instanceof NotFoundException
view(NotFound)
import { ExceptionHandler, NotFoundException, view } from '@formidablejs/framework'
import { NotFound } from '../../resources/views/errors/NotFound'
export class Handler extends ExceptionHandler {
handle(exception: Error) {
if (exception instanceof NotFoundException) {
return view(NotFound)
}
}
}
Conditional Handling
If your application handles both web based and api based requests, you can use the request argument to determine the type of request. For example, you can return a JSON response for api requests and a view for web requests:
- Imba
- TypeScript
import { ExceptionHandler, NotFoundException, view, response } from '@formidablejs/framework'
import { NotFound } from '../../resources/views/errors/NotFound'
export class Handler < ExceptionHandler
def handle exception\Error, request
if exception instanceof NotFoundException
if request.expectsJson!
response({ message: 'Not Found' }, 404)
else
view(NotFound)
import { ExceptionHandler, NotFoundException, view, response } from '@formidablejs/framework'
import { NotFound } from '../../resources/views/errors/NotFound'
export class Handler extends ExceptionHandler {
handle(exception: Error, request) {
if (exception instanceof NotFoundException) {
if (request.expectsJson()) {
return response({ message: 'Not Found' }, 404)
} else {
return view(NotFound)
}
}
}
}