Skip to content

Commit 53e8a2e

Browse files
committed
增强异常接口
1 parent 08ec8ac commit 53e8a2e

File tree

4 files changed

+9
-11
lines changed

4 files changed

+9
-11
lines changed

exception/base.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@ import "fmt"
66
type APIException interface {
77
error
88
ErrorCode() int
9+
WithMeta(m interface{})
910
Meta() interface{}
11+
WithData(d interface{})
1012
Data() interface{}
1113
Is(code int) bool
1214
Namespace() string
1315
Reason() string
1416
}
1517

16-
// WithAPIException 携带元信息的异常
17-
type WithAPIException interface {
18-
APIException
19-
WithMeta(m interface{}) APIException
20-
}
21-
2218
func newException(namespace Namespace, code int, format string, a ...interface{}) *exception {
2319
return &exception{
2420
namespace: namespace,
@@ -48,18 +44,16 @@ func (e *exception) ErrorCode() int {
4844
}
4945

5046
// WithMeta 携带一些额外信息
51-
func (e *exception) WithMeta(m interface{}) APIException {
47+
func (e *exception) WithMeta(m interface{}) {
5248
e.meta = m
53-
return e
5449
}
5550

5651
func (e *exception) Meta() interface{} {
5752
return e.meta
5853
}
5954

60-
func (e *exception) WithData(d interface{}) APIException {
55+
func (e *exception) WithData(d interface{}) {
6156
e.data = d
62-
return e
6357
}
6458

6559
func (e *exception) Data() interface{} {

exception/exception.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "fmt"
44

55
// NewAPIException 创建一个API异常
66
// 用于其他模块自定义异常
7-
func NewAPIException(namespace string, code int, reason, format string, a ...interface{}) WithAPIException {
7+
func NewAPIException(namespace string, code int, reason, format string, a ...interface{}) APIException {
88
// 0表示正常状态, 但是要排除变量的零值
99
if code == 0 {
1010
code = -1

http/response/data.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Data struct {
88
Reason string `json:"reason,omitempty"` // 异常原因
99
Message string `json:"message,omitempty"` // 关于这次响应的说明信息
1010
Data interface{} `json:"data,omitempty"` // 返回的具体数据
11+
Meta interface{} `json:"meta,omitempty"` // 数据meta
1112
}
1213

1314
// NewData new实例

http/response/send.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ func Failed(w http.ResponseWriter, err error) {
1616
ns string
1717
reason string
1818
data interface{}
19+
meta interface{}
1920
)
2021

2122
switch t := err.(type) {
2223
case exception.APIException:
2324
errCode = t.ErrorCode()
2425
reason = t.Reason()
2526
data = t.Data()
27+
meta = t.Meta()
2628
ns = t.Namespace()
2729
default:
2830
errCode = exception.UnKnownException
@@ -42,6 +44,7 @@ func Failed(w http.ResponseWriter, err error) {
4244
Reason: reason,
4345
Message: err.Error(),
4446
Data: data,
47+
Meta: meta,
4548
}
4649

4750
// set response heanders

0 commit comments

Comments
 (0)