Skip to content

Commit

Permalink
feat: capture common error in stdresp
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanialr committed Oct 7, 2024
1 parent 387e886 commit 9417746
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import "sync/atomic"
// isDebug concurrent safe debug signal holder.
var isDebug atomic.Bool

// IsOn return the debug state, return true if it's on.
func IsOn() bool {
return isDebug.Load()
}

// EnableDebug concurrent safe helper to enable debug information in the
// stderr.
func EnableDebug() {
Expand Down
10 changes: 9 additions & 1 deletion stdresp/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stdresp
import (
"strings"

"github.com/spotlibs/go-lib/debug"
"github.com/spotlibs/go-lib/stderr"
)

Expand All @@ -28,7 +29,7 @@ func WithErr(e error) StdOpt {
return func(s *Std) {
// set default response code and description
s.ResponseCode = stderr.ERROR_CODE_SYSTEM
s.ResponseDesc = stderr.ERROR_DESC_SYSTEM
s.ResponseDesc = "Terjadi kesalahan, mohon coba beberapa saat lagi yaa... "

// check if the error is created using stderr pkg
if stderr.IsStdError(e) {
Expand All @@ -43,6 +44,13 @@ func WithErr(e error) StdOpt {

// get the http code
s.httpCode = stderr.GetHttpCode(e)
return
}

// capture in case its random error that's not constructed with stderr pkg
// but only print if the debug flag is on
if debug.IsOn() {
s.ResponseDesc = e.Error()
}
}
}

0 comments on commit 9417746

Please sign in to comment.