Skip to content

Commit 66964b1

Browse files
authored
Merge pull request #37 from go-goim/feature/offline-and-history-msg
refactor: unified error for all
2 parents 5a3eeb3 + f422d8d commit 66964b1

21 files changed

+1471
-1484
lines changed

errors/errors.ext.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// This file Written Manually.
2+
3+
package errors
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
/*
10+
* Define BaseResponse
11+
*/
12+
13+
var _ error = &Error{}
14+
15+
func NewErrorWithCode(code ErrorCode) *Error {
16+
return &Error{
17+
ErrorCode: code,
18+
Reason: code.String(),
19+
}
20+
}
21+
22+
func ErrorOK() *Error {
23+
return NewErrorWithCode(ErrorCode_OK)
24+
}
25+
26+
func NewErrorWithError(err error) *Error {
27+
// check err is BaseResponse
28+
if br, ok := err.(*Error); ok {
29+
return br
30+
}
31+
32+
return &Error{
33+
ErrorCode: ErrorCode_InternalError,
34+
Reason: ErrorCode_InternalError.String(),
35+
Message: err.Error(),
36+
}
37+
}
38+
39+
// Error is implement of error interface.
40+
func (x *Error) Error() string {
41+
return fmt.Sprintf("ErrorCode: %d, Reason: %s, Message: %s", x.ErrorCode, x.Reason, x.Message)
42+
}
43+
44+
// Err returns as error.
45+
func (x *Error) Err() error {
46+
if x.ErrorCode == ErrorCode_OK {
47+
return nil
48+
}
49+
50+
return x
51+
}
52+
53+
func (x *Error) Success() bool {
54+
return x.ErrorCode == ErrorCode_OK
55+
}
56+
57+
func (x *Error) WithMessage(msg string) *Error {
58+
x.Message = msg
59+
return x
60+
}
61+
62+
func (x *Error) WithError(err error) *Error {
63+
x.Message = err.Error()
64+
return x
65+
}
66+
67+
/*
68+
* ErrorCode As Error.
69+
*/
70+
71+
func (x ErrorCode) Error() string {
72+
return NewErrorWithCode(x).Error()
73+
}
74+
75+
func (x ErrorCode) Err() error {
76+
if x == ErrorCode_OK {
77+
return nil
78+
}
79+
80+
return NewErrorWithCode(x)
81+
}
82+
83+
// Err2 returns as Error we defined.
84+
func (x ErrorCode) Err2() *Error {
85+
return NewErrorWithCode(x)
86+
}
87+
88+
func (x ErrorCode) WithMessage(msg string) *Error {
89+
return NewErrorWithCode(x).WithMessage(msg)
90+
}
91+
92+
func (x ErrorCode) WithError(err error) *Error {
93+
return NewErrorWithCode(x).WithError(err)
94+
}

errors/errors.pb.go

Lines changed: 299 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)