Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesfsp committed Feb 18, 2022
1 parent 42cfebc commit 5ee7055
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Roadmap

## [1.0.1] - 2022-02-18

### Added

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

## [1.0.0] - 2022-02-17
### Added
- Functional `Option`s.
Expand Down
20 changes: 16 additions & 4 deletions builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,32 @@ import (
//
// Note: Status code can be redefined, call `SetStatusCode`.
func NewFailedToError(message string, opts ...Option) error {
return New(fmt.Sprintf("failed to %s", message), append(opts, WithStatusCode(http.StatusInternalServerError))...)
return New(fmt.Sprintf("failed to %s", message), prependOptions(
opts,
WithStatusCode(http.StatusInternalServerError),
)...)
}

// NewInvalidError is the building block for errors usually thrown when
// something fail validation, e.g: "Invalid port". Default status code is `400`.
//
// Note: Status code can be redefined, call `SetStatusCode`.
func NewInvalidError(message string, opts ...Option) error {
return New(fmt.Sprintf("invalid %s", message), append(opts, WithStatusCode(http.StatusBadRequest))...)
return New(fmt.Sprintf("invalid %s", message), prependOptions(
opts,
WithStatusCode(http.StatusBadRequest),
)...)
}

// NewMissingError is the building block for errors usually thrown when required
// information is missing, e.g: "Missing host". Default status code is `400`.
//
// Note: Status code can be redefined, call `SetStatusCode`.
func NewMissingError(message string, opts ...Option) error {
return New(fmt.Sprintf("missing %s", message), append(opts, WithStatusCode(http.StatusBadRequest))...)
return New(fmt.Sprintf("missing %s", message), prependOptions(
opts,
WithStatusCode(http.StatusBadRequest),
)...)
}

// NewRequiredError is the building block for errors usually thrown when
Expand All @@ -43,5 +52,8 @@ func NewMissingError(message string, opts ...Option) error {
//
// Note: Status code can be redefined, call `SetStatusCode`.
func NewRequiredError(message string, opts ...Option) error {
return New(fmt.Sprintf("%s required", message), append(opts, WithStatusCode(http.StatusBadRequest))...)
return New(fmt.Sprintf("%s required", message), prependOptions(
opts,
WithStatusCode(http.StatusBadRequest),
)...)
}
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func ExampleNew() {
// Demonstrates how to create static, and dynamic custom errors, also how to
// check, and instrospect custom errors.
func ExampleNew_options() {
fmt.Println(NewMissingError("id", WithCode("E1010"), WithStatusCode(http.StatusOK), WithError(errors.New("some error"))))
fmt.Println(NewMissingError("id", WithCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error"))))

// output:
// E1010: missing id (400 - Bad Request). Original Error: some error
// E1010: missing id (406 - Not Acceptable). Original Error: some error
}
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ package customerror
// Option allows to define error options.
type Option func(s *CustomError)

// Prepend options.
func prependOptions(source []Option, item Option) []Option {
source = append(source, nil)

copy(source[1:], source)

source[0] = item

return source
}

// WithError allows to specify an error which will be wrapped by the custom
// error.
func WithError(err error) Option {
Expand Down

0 comments on commit 5ee7055

Please sign in to comment.