-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
56 lines (45 loc) · 1.47 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package stdresp
import (
"strings"
"github.com/spotlibs/go-lib/debug"
"github.com/spotlibs/go-lib/stderr"
)
// StdOpt option signature that accept and modify Std response object.
type StdOpt func(std *Std)
// WithDesc embed given string data to the standard response as field `responseDesc`.
func WithDesc(desc string) StdOpt {
return func(s *Std) {
s.ResponseDesc = desc
}
}
// WithData embed given data object to the standard response as field `responseData`.
func WithData(data any) StdOpt {
return func(s *Std) {
s.ResponseData = data
}
}
// WithErr embed given error to the standard response.
func WithErr(e error) StdOpt {
return func(s *Std) {
// set default response code and description
s.ResponseCode = stderr.ERROR_CODE_SYSTEM
s.ResponseDesc = "Terjadi kesalahan, mohon coba beberapa saat lagi yaa... "
// check if the error is created using stderr pkg
if stderr.IsStdError(e) {
// override the code and description
s.ResponseCode = stderr.GetCode(e)
// do trim space in case stacktrace is empty
s.ResponseDesc = strings.TrimSpace(stderr.GetMsg(e) + " " + stderr.GetStackTrace(e))
// also if any, get the validation message too
s.ResponseValidation = stderr.GetValidationErrorMsg(e)
// 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()
}
}
}