Created
February 26, 2020 09:36
-
-
Save guoxingx/d170c364e6375f2191a8c433b54c04cf to your computer and use it in GitHub Desktop.
error with code and message
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
package gerror | |
import ( | |
"fmt" | |
) | |
// Error with an code in int type | |
type Error struct { | |
code int | |
message string | |
} | |
// NewError returns an Error object | |
func NewError(c int, m string) Error { | |
return Error{c, m} | |
} | |
// Code returns the code of error | |
func (e Error) Code() int { | |
return e.code | |
} | |
// Message returns the message of error | |
func (e Error) Message() string { | |
return e.message | |
} | |
// Error implement Error() of error interface | |
func (e Error) Error() string { | |
return fmt.Sprintf("code: %d, message: %s", e.code, e.message) | |
} | |
// Same returns true if error has the same code with another | |
func (e Error) Same(anoter Error) bool { | |
return e.code == anoter.code | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment