-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10bb6ba
commit d25a81a
Showing
6 changed files
with
117 additions
and
28 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
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
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,41 @@ | ||
package errors | ||
|
||
import "fmt" | ||
|
||
type ErrorCode int | ||
|
||
const ( | ||
GeneralError ErrorCode = iota | ||
FormatError | ||
ParseError | ||
ServiceError | ||
) | ||
|
||
var ( | ||
ErrChannelUnavailable = &Error{Code: ServiceError, Message: "channel unavailable"} | ||
ErrChannelMessage = &Error{Code: ServiceError, Message: "fail to get message"} | ||
ErrConnection = &Error{Code: ServiceError, Message: "fail to connect"} | ||
ErrMessagePublishing = &Error{Code: ServiceError, Message: "error on message publishing"} | ||
ErrJobRegister = &Error{Code: ServiceError, Message: "error on job register"} | ||
ErrJobEnqueue = &Error{Code: ServiceError, Message: "error on enqueue job"} | ||
ErrEmptyURL = &Error{Code: FormatError, Message: "needs a non-empty url"} | ||
ErrMessageSerialize = &Error{Code: ParseError, Message: "error on message serialize"} | ||
) | ||
|
||
type Error struct { | ||
Code ErrorCode | ||
Message string | ||
Err error | ||
} | ||
|
||
func (e *Error) Error() string { | ||
if e.Err != nil { | ||
return fmt.Sprintf("[Error %d] %s: %s", e.Code, e.Message, e.Err) | ||
} | ||
return fmt.Sprintf("[Error %d] %s", e.Code, e.Message) | ||
} | ||
|
||
func (e *Error) WithValue(Err error) *Error { | ||
e.Err = Err | ||
return e | ||
} |
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,40 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func TestErrorMessage(t *testing.T) { | ||
customError := errors.New("fail") | ||
|
||
tt := []struct { | ||
Err *Error | ||
Expectation string | ||
}{ | ||
{ | ||
Err: &Error{Code: FormatError, Message: "empty value"}, | ||
Expectation: fmt.Sprintf("[Error %d] empty value", FormatError), | ||
}, | ||
{ | ||
Err: &Error{Code: ServiceError, Message: "not found"}, | ||
Expectation: fmt.Sprintf("[Error %d] not found", ServiceError), | ||
}, | ||
{ | ||
Err: (&Error{Code: FormatError, Message: "nil pointer"}).WithValue(customError), | ||
Expectation: fmt.Sprintf("[Error %d] nil pointer: fail", FormatError), | ||
}, | ||
{ | ||
Err: (&Error{Code: ServiceError, Message: "mysql error"}).WithValue(customError), | ||
Expectation: fmt.Sprintf("[Error %d] mysql error: fail", ServiceError), | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
if tc.Err.Error() != tc.Expectation { | ||
t.Errorf("expect '%s', but got '%s'", tc.Expectation, tc.Err) | ||
} | ||
} | ||
} |
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
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