Exceptions
What are exceptions in Gaman? Gaman has an exceptions handler, which can be called error handling, so it controls the existing error system. Its usage is quite simple, just using composeExceptionHandler.
Exception Definition
Section titled “Exception Definition”composeExceptionHandler()
Section titled “composeExceptionHandler()”composeExceptionHandler here will not be automatically registered into the system, so you need to register it manually.
import { composeExceptionHandler } from "@gaman/core"
export default composeExceptionHandler((err: Error) => { if(err instanceof HttpException){ const context = err.context;
if(context.url.pathname.includes('/dashboard')){ return Res.text("This is the dashboard") } }
return Res.json({ message: 'Internal Server Error' }, { status: 500 })})Here you need to register your exception to a specific route or globally.
Example Specific Route:
// single routeroute.get('/user', Handler).exception(UserException);
// or directly group to avoid one by oneroute.group('/user', (route) => { // other routes}).exception([UserException, AppException])Example Register Global:
defineBootstrap((app) => { app.mount(UserException)})autoComposeExceptionHandler()
Section titled “autoComposeExceptionHandler()”If you use autoComposeExceptionHandler, the exception handler will be automatically registered into the system globally, with the requirement that it must be in the src/exceptions/** folder.
import { autoComposeExceptionHandler } from "@gaman/core"
export default autoComposeExceptionHandler((err: Error) => { return Res.json({ message: "Internal server error!" }, { status: 500 })})Here you don’t need to register manually because it’s already automatically registered by the system.