go-aah / aah

A secure, flexible, rapid Go web framework
https://aahframework.org
MIT License
690 stars 33 forks source link

invalid memory address or nil pointer dereference when registering centralized error handler #261

Closed arpodungge closed 4 years ago

arpodungge commented 5 years ago

Using aah v0.12.3

I'm creating a new api project with aah new I've found an issue when i try to create centralized error handler by creating AppErrorHandler function and register it in init.go by following the instruction from https://docs.aahframework.org/v0.12/error-handling.html#registering-centralized-error-handler

func AppErrorHandler(ctx *aah.Context, err *aah.Error) bool {
    switch err.Code {
    case 400:
        // handle bad request
    case 401:
        // handle unauthenticated request
    case 403:
        // handle permission issues
    case 500:
        // handle internal server issues
    }
    return true
}
func init() {
    app := aah.App()
    app.SetErrorHandler(AppErrorHandler)
}

an error occurred when i run it with aah run :

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x8a7974]

goroutine 1 [running]: aahframe%2ework.(errorManager).SetHandler(0x0, 0xa29780) /home/user1/go/pkg/mod/aahframe.work@v0.12.3/error.go:132 +0x34 aahframe%2ework.(Application).SetErrorHandler(0xc0001e21c0, 0xa29780) /home/user1/go/pkg/mod/aahframe.work@v0.12.3/aah.go:400 +0x3c main.init.0() /home/user1/ws_aah/aah-app/app/init.go:43 +0x118 FATAL aah application did not start

Thanks

jeevatkm commented 5 years ago

@arpodungge It seems I need to update the documentation. Please use OnStart event, update your code to

app := aah.App()
app.OnStart(func(e *aah.Event) {
  app.SetErrorHandler(AppErrorHandler)
})
jeevatkm commented 4 years ago

I'm closing this one. Please let me know.