-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. add offline and history msg defination 2. refactor transport.response to provide simpler and easy to use error for all Signed-off-by: Yusan Kurban <yusankurban@gmail.com>
- Loading branch information
Showing
21 changed files
with
1,471 additions
and
1,484 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// This file Written Manually. | ||
|
||
package errors | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
/* | ||
* Define BaseResponse | ||
*/ | ||
|
||
var _ error = &Error{} | ||
|
||
func NewErrorWithCode(code ErrorCode) *Error { | ||
return &Error{ | ||
ErrorCode: code, | ||
Reason: code.String(), | ||
} | ||
} | ||
|
||
func ErrorOK() *Error { | ||
return NewErrorWithCode(ErrorCode_OK) | ||
} | ||
|
||
func NewErrorWithError(err error) *Error { | ||
// check err is BaseResponse | ||
if br, ok := err.(*Error); ok { | ||
return br | ||
} | ||
|
||
return &Error{ | ||
ErrorCode: ErrorCode_InternalError, | ||
Reason: ErrorCode_InternalError.String(), | ||
Message: err.Error(), | ||
} | ||
} | ||
|
||
// Error is implement of error interface. | ||
func (x *Error) Error() string { | ||
return fmt.Sprintf("ErrorCode: %d, Reason: %s, Message: %s", x.ErrorCode, x.Reason, x.Message) | ||
} | ||
|
||
// Err returns as error. | ||
func (x *Error) Err() error { | ||
if x.ErrorCode == ErrorCode_OK { | ||
return nil | ||
} | ||
|
||
return x | ||
} | ||
|
||
func (x *Error) Success() bool { | ||
return x.ErrorCode == ErrorCode_OK | ||
} | ||
|
||
func (x *Error) WithMessage(msg string) *Error { | ||
x.Message = msg | ||
return x | ||
} | ||
|
||
func (x *Error) WithError(err error) *Error { | ||
x.Message = err.Error() | ||
return x | ||
} | ||
|
||
/* | ||
* ErrorCode As Error. | ||
*/ | ||
|
||
func (x ErrorCode) Error() string { | ||
return NewErrorWithCode(x).Error() | ||
} | ||
|
||
func (x ErrorCode) Err() error { | ||
if x == ErrorCode_OK { | ||
return nil | ||
} | ||
|
||
return NewErrorWithCode(x) | ||
} | ||
|
||
// Err2 returns as Error we defined. | ||
func (x ErrorCode) Err2() *Error { | ||
return NewErrorWithCode(x) | ||
} | ||
|
||
func (x ErrorCode) WithMessage(msg string) *Error { | ||
return NewErrorWithCode(x).WithMessage(msg) | ||
} | ||
|
||
func (x ErrorCode) WithError(err error) *Error { | ||
return NewErrorWithCode(x).WithError(err) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.