-
Notifications
You must be signed in to change notification settings - Fork 1
feat(throw):#3 added ApplicationError #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6300e13
feat(throw):#3 added ApplicationError
geax d67fa0d
feat(throw):#3 updated ApplicationError.Message with ApplicationError…
cnlangzi 316fe09
feat(throw):#3 updated README.md
cnlangzi 2ed5f44
feat(throw):#3 updated README.md
cnlangzi 30e08fd
feat(throw):#3 fixed typo
cnlangzi 60e7735
feat(throw):#3 fixed Unwrap
cnlangzi abce18c
feat(throw):#3 updated error message format
cnlangzi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,50 @@ | ||
package errors | ||
|
||
import "strings" | ||
|
||
// ApplicationError an appliction error with predifined error variable and detail message | ||
type ApplicationError struct { | ||
// Inner inner error | ||
Inner error | ||
// MsgList detail message | ||
MsgList []string | ||
} | ||
|
||
// Error implement error.Error | ||
func (e *ApplicationError) Error() string { | ||
if e == nil { | ||
return "" | ||
} | ||
|
||
if e.Inner != nil && e.MsgList != nil { | ||
return e.Inner.Error() + ": " + strings.Join(e.MsgList, " ") | ||
} else if e.Inner != nil { | ||
return e.Inner.Error() | ||
} else { | ||
return strings.Join(e.MsgList, " ") | ||
} | ||
|
||
} | ||
|
||
// Unwrap implement error.Unwrap | ||
func (e *ApplicationError) Unwrap() error { | ||
if e == nil { | ||
return nil | ||
} | ||
|
||
if e.Inner != nil { | ||
return e.Inner | ||
} | ||
|
||
return e | ||
} | ||
|
||
// Throw create an application error with prefinded error variable and message | ||
// example | ||
// errors.Throw(ErrInvalidParameter, "bloober_id is missing") | ||
func Throw(inner error, msgList ...string) error { | ||
return &ApplicationError{ | ||
Inner: inner, | ||
MsgList: msgList, | ||
} | ||
} |
This file contains hidden or 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 ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestThrowIs(t *testing.T) { | ||
|
||
errIs := errors.New("Is") | ||
|
||
errIsNot := errors.New("Is not") | ||
|
||
err := Throw(errIs, "", "errors.Is works") | ||
|
||
require.ErrorIs(t, err, errIs) | ||
require.Equal(t, false, errors.Is(err, errIsNot)) | ||
} | ||
|
||
func TestThrowUnwrap(t *testing.T) { | ||
|
||
inner := errors.New("Inner") | ||
|
||
err := Throw(inner, "errors.Unwrap works") | ||
|
||
require.Equal(t, inner, errors.Unwrap(err)) | ||
} | ||
|
||
func TestThrowAs(t *testing.T) { | ||
|
||
inner := errors.New("Inner") | ||
|
||
err := Throw(inner, "errors.As works") | ||
|
||
var appErr *ApplicationError | ||
|
||
require.Equal(t, true, errors.As(err, &appErr)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.