Skip to content

Commit

Permalink
Merge pull request #27 from strvcom/feat/add-message-to-error
Browse files Browse the repository at this point in the history
feat: improved error response options
  • Loading branch information
TomasKocman committed Jan 20, 2023
2 parents 0f6aa2e + a9adc84 commit 78018b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ How to release a new version:

## [Unreleased]

## [0.5.0] - 2022-01-20
### Added
- `ErrorResponseOptions` contains public error message.
- `ErrorResponseOptions` contains request ID.
- Error response options:
- `WithErrorMessage`
- `WithRequestID`

## [0.4.0] - 2022-01-12
### Changed
- JSON tags in `ErrorResponseOptions`.
Expand Down Expand Up @@ -32,7 +40,8 @@ How to release a new version:
### Added
- Added Changelog.

[Unreleased]: https://github.com/strvcom/strv-backend-go-net/compare/v0.4.0...HEAD
[Unreleased]: https://github.com/strvcom/strv-backend-go-net/compare/v0.5.0...HEAD
[0.5.0]: https://github.com/strvcom/strv-backend-go-net/compare/v0.4.0...v0.5.0
[0.4.0]: https://github.com/strvcom/strv-backend-go-net/compare/v0.3.0...v0.4.0
[0.3.0]: https://github.com/strvcom/strv-backend-go-net/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/strvcom/strv-backend-go-net/compare/v0.1.0...v0.2.0
Expand Down
21 changes: 18 additions & 3 deletions http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,22 @@ func WriteErrorResponse(
type ErrorResponseOptions struct {
ResponseOptions `json:"-"`

Err error `json:"-"`
ErrCode string `json:"errorCode"`
ErrData any `json:"errorData,omitempty"`
RequestID string `json:"requestId,omitempty"`

Err error `json:"-"`
ErrCode string `json:"errorCode"`
ErrMessage string `json:"errorMessage,omitempty"`
ErrData any `json:"errorData,omitempty"`
}

type ErrorResponseOption func(*ErrorResponseOptions)

func WithRequestID(id string) ErrorResponseOption {
return func(o *ErrorResponseOptions) {
o.RequestID = id
}
}

func WithError(err error) ErrorResponseOption {
return func(o *ErrorResponseOptions) {
o.Err = err
Expand All @@ -108,6 +117,12 @@ func WithErrorCode(code string) ErrorResponseOption {
}
}

func WithErrorMessage(msg string) ErrorResponseOption {
return func(o *ErrorResponseOptions) {
o.ErrMessage = msg
}
}

func WithErrorData(data any) ErrorResponseOption {
return func(o *ErrorResponseOptions) {
o.ErrData = data
Expand Down

0 comments on commit 78018b3

Please sign in to comment.