Skip to content

Commit 08ec8ac

Browse files
committed
添加一次Data
1 parent e3f0693 commit 08ec8ac

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

exception/base.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type APIException interface {
77
error
88
ErrorCode() int
99
Meta() interface{}
10+
Data() interface{}
1011
Is(code int) bool
1112
Namespace() string
1213
Reason() string
@@ -34,6 +35,7 @@ type exception struct {
3435
reason string
3536
message string
3637
meta interface{}
38+
data interface{}
3739
}
3840

3941
func (e *exception) Error() string {
@@ -55,6 +57,15 @@ func (e *exception) Meta() interface{} {
5557
return e.meta
5658
}
5759

60+
func (e *exception) WithData(d interface{}) APIException {
61+
e.data = d
62+
return e
63+
}
64+
65+
func (e *exception) Data() interface{} {
66+
return e.data
67+
}
68+
5869
func (e *exception) Is(code int) bool {
5970
return e.ErrorCode() == code
6071
}

http/response/send.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ func Failed(w http.ResponseWriter, err error) {
1515
httpCode int
1616
ns string
1717
reason string
18+
data interface{}
1819
)
1920

2021
switch t := err.(type) {
2122
case exception.APIException:
2223
errCode = t.ErrorCode()
2324
reason = t.Reason()
25+
data = t.Data()
2426
ns = t.Namespace()
2527
default:
2628
errCode = exception.UnKnownException
@@ -39,6 +41,7 @@ func Failed(w http.ResponseWriter, err error) {
3941
Namespace: ns,
4042
Reason: reason,
4143
Message: err.Error(),
44+
Data: data,
4245
}
4346

4447
// set response heanders

0 commit comments

Comments
 (0)