Skip to content

Commit

Permalink
v1.0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesfsp committed Oct 22, 2022
1 parent 9698240 commit 1410a0b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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).

## [1.0.11] - 2022-10-21
### Added
- Added the ability to tag errors with a custom tags. Tags is a list of tags which helps to categorize the error.

## [1.0.10] - 2022-10-21
### Changed
- Fixed type in the `WithStatusCode` `Option`.
Expand Down
11 changes: 11 additions & 0 deletions customerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type CustomError struct {
// StatusCode is a valid HTTP status code, e.g.: 404.
StatusCode int `json:"-" validate:"omitempty,gte=100,lte=511"`

// Tags is a list of tags which helps to categorize the error.
Tags []string `json:"tags,omitempty"`

// If set to true, the error will be ignored (return nil).
ignore bool `json:"-"`
}
Expand All @@ -51,6 +54,10 @@ func (cE *CustomError) Error() string {
}
}

if cE.Tags != nil {
errMsg = fmt.Sprintf("%s. Tags: %s", errMsg, strings.Join(cE.Tags, ", "))
}

if cE.Err != nil {
errMsg = fmt.Errorf("%s. Original Error: %w", errMsg, cE.Err).Error()
}
Expand Down Expand Up @@ -78,6 +85,10 @@ func (cE *CustomError) APIError() string {
}
}

if cE.Tags != nil {
errMsg = fmt.Sprintf("%s. Tags: %s", errMsg, strings.Join(cE.Tags, ", "))
}

if cE.Err != nil {
errMsg = fmt.Errorf("%s. Original Error: %w", errMsg, cE.Err).Error()
}
Expand Down
25 changes: 25 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,28 @@ func ExampleNew_newNoMessage() {
// E1010
// E1010: Accepted (202)
}

// Demonstrates the WithTag option.
//
//nolint:errorlint,forcetypeassert
func ExampleNew_optionsWithTag() {
fmt.Println(NewMissingError(
"id",
WithTag("test1", "test2"),
WithCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
))

fmt.Println(NewMissingError(
"id",
WithTag("test1", "test2"),
WithCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
).(*CustomError).APIError())

// output:
// E1010: missing id. Tags: test1, test2. Original Error: some error
// E1010: missing id (406 - Not Acceptable). Tags: test1, test2. Original Error: some error
}
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ func WithIgnoreString(s ...string) Option {
return false
})
}

// WithTag allows to specify tags for the error.
func WithTag(tag ...string) Option {
return func(cE *CustomError) {
cE.Tags = tag
}
}

0 comments on commit 1410a0b

Please sign in to comment.