Skip to content

Commit

Permalink
feat: capture stack trace on recover middleware regardless debug state
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanialr committed Oct 2, 2024
1 parent 3c48c84 commit da76abb
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion middleware/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
"github.com/spotlibs/go-lib/debug"
"github.com/spotlibs/go-lib/log"
"github.com/spotlibs/go-lib/stderr"
"github.com/spotlibs/go-lib/stdresp"
)
Expand All @@ -16,11 +17,14 @@ func Recover(c http.Context) {
defer func() {
// grab any panic occurring
if r := recover(); r != nil {
m := log.Map{"msg": debug.GetStackTraceInString(1)}
log.Runtime(c).Error(m)

prefixMsg := "Panic Runtime Error - "
suffixMsg := "Terjadi kesalahan, silahkan hubungi IT Helpdesk" // use masked message as the default
if facades.Config().GetBool("APP_DEBUG") {
// replace with the debug info if its enabled
suffixMsg = fmt.Sprint(r) + " - " + debug.GetStackTraceInString(1)
suffixMsg = fmt.Sprint(r) + " - " + m["msg"].(string)
}

err := stderr.Err(stderr.ERROR_CODE_SYSTEM, prefixMsg+suffixMsg, http.StatusOK)
Expand Down

0 comments on commit da76abb

Please sign in to comment.