From 94177468e746df1c9c565e62d5433bd0b1e2af47 Mon Sep 17 00:00:00 2001 From: Danial Date: Mon, 7 Oct 2024 12:45:59 +0700 Subject: [PATCH] feat: capture common error in stdresp --- debug/debug.go | 5 +++++ stdresp/option.go | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/debug/debug.go b/debug/debug.go index 2029883..7dec60d 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -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() { diff --git a/stdresp/option.go b/stdresp/option.go index 96888f6..1c9311d 100644 --- a/stdresp/option.go +++ b/stdresp/option.go @@ -3,6 +3,7 @@ package stdresp import ( "strings" + "github.com/spotlibs/go-lib/debug" "github.com/spotlibs/go-lib/stderr" ) @@ -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) { @@ -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() } } }