Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesfsp committed Feb 18, 2022
1 parent 5ee7055 commit 9fc323f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Roadmap
## [1.0.2] - 2022-02-18
### Added

## [1.0.1] - 2022-02-18
- API info is only added to error message on `APIError`.

## [1.0.1] - 2022-02-18
### Added

- Added ability to pre-append `Option`s.

## [1.0.0] - 2022-02-17
Expand Down
15 changes: 15 additions & 0 deletions customerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ func (cE *CustomError) Error() string {
errMsg = fmt.Sprintf("%s: %s", cE.Code, errMsg)
}

if cE.Err != nil {
errMsg = fmt.Errorf("%s. Original Error: %w", errMsg, cE.Err).Error()
}

return errMsg
}

// APIError is like error plus status code information.
func (cE *CustomError) APIError() string {
errMsg := cE.Message

if cE.Code != "" {
errMsg = fmt.Sprintf("%s: %s", cE.Code, errMsg)
}

if cE.StatusCode != 0 {
errMsg = fmt.Sprintf("%s (%d - %s)", errMsg, cE.StatusCode, http.StatusText(cE.StatusCode))
}
Expand Down
13 changes: 6 additions & 7 deletions customerror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var (
ErrFailedToReachServerDeepRev = fmt.Errorf("%s. %w", errors.New("servers are broken"), ErrFailedToReachServer)
)

//nolint:lll
func TestNewLowLevel(t *testing.T) {
type args struct {
message string
Expand Down Expand Up @@ -70,7 +69,7 @@ func TestNewLowLevel(t *testing.T) {
message: failedCreateSomethingMsg,
opts: []Option{WithStatusCode(statusCode)},
},
want: "Failed to create something (404 - Not Found)",
want: "Failed to create something",
},
{
name: "should work - with message, code, and error",
Expand All @@ -94,7 +93,7 @@ func TestNewLowLevel(t *testing.T) {
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code), WithError(ErrFailedToReachServerDeep), WithStatusCode(statusCode)},
},
want: "E1010: Failed to create something (404 - Not Found). Original Error: Failed to reach servers. Servers are broken",
want: "E1010: Failed to create something. Original Error: Failed to reach servers. Servers are broken",
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -130,31 +129,31 @@ func TestBuiltin(t *testing.T) {
args: args{
err: ErrFailedToCreateFile,
},
want: "failed to create file (500 - Internal Server Error)",
want: "failed to create file",
wantAs: "failed to create file",
},
{
name: "Should work - ErrInvalidPath",
args: args{
err: ErrInvalidPath,
},
want: "invalid path (400 - Bad Request)",
want: "invalid path",
wantAs: "invalid path",
},
{
name: "Should work - ErrMissingPath",
args: args{
err: ErrMissingPath,
},
want: "missing path (400 - Bad Request)",
want: "missing path",
wantAs: "missing path",
},
{
name: "Should work - ErrRequiredPath",
args: args{
err: ErrRequiredPath,
},
want: "path is required (400 - Bad Request)",
want: "path is required",
wantAs: "path is required",
},
}
Expand Down
9 changes: 6 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ func ExampleNew() {
// output:
// true
// 400
// E1010: missing id (400 - Bad Request)
// E1010: missing id
// 500
// E1523: failed to write to disk (500 - Internal Server Error)
// E1523: failed to write to disk
}

// Demonstrates how to create static, and dynamic custom errors, also how to
// check, and instrospect custom errors.
//nolint:errorlint
func ExampleNew_options() {
fmt.Println(NewMissingError("id", WithCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error"))))
fmt.Println(
NewMissingError("id", WithCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error"))).(*CustomError).APIError(),
)

// output:
// E1010: missing id (406 - Not Acceptable). Original Error: some error
Expand Down

0 comments on commit 9fc323f

Please sign in to comment.