Exceptions
Apa itu exceptions di gaman? gaman memiki exceptions handler bisa di sebut error handling, jadi mengontroll error system yang ada. penggunaanya cukup simple memakai composeExceptionHandler
saja.
Exception Definition
Section titled “Exception Definition”composeExceptionHandler()
Section titled “composeExceptionHandler()”composeExceptionHandler
disini tidak akan otomatis keregister otomatis kedalam sistem, jadi kamu perlu meregister secara manual.
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("Ini Adalah dashboard") } }
return Res.json({ message: 'Internal Server Error' }, { status: 500 })})
disini kamu perlu meregister exception kamu ke spesifik route atau secara global
Contoh Spesifik Route:
// single routeroute.get('/user', Handler).exception(UserException);
// atau langsung group biar ga satu saturoute.group('/user', (route) => { // other routes}).exception([UserException, AppException])
Contoh Register Global:
defineBootstrap((app) => { app.mount(UserException)})
autoComposeExceptionHandler()
Section titled “autoComposeExceptionHandler()”jika kamu memakai autoComposeExceptionHandler
maka exception handler akan otomatis di register kedalam sistem secara global dengan persyaratan harus berada di dalam folder src/exceptions/**
.
import { autoComposeExceptionHandler } from "@gaman/core"
export default autoComposeExceptionHandler((err: Error) => { return Res.json({ message: "Internal server error!" }, { status: 500 })})
disini kamu tidak perlu meregister manual karna sudah otomatis di register oleh sistem