From 1410a0be5c8f241b4b44403dc8038585f282207f Mon Sep 17 00:00:00 2001 From: Energy Star Date: Fri, 21 Oct 2022 18:19:59 -0700 Subject: [PATCH] v1.0.11 --- CHANGELOG.md | 4 ++++ customerror.go | 11 +++++++++++ example_test.go | 25 +++++++++++++++++++++++++ options.go | 7 +++++++ 4 files changed, 47 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec3e502..53aa16a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/customerror.go b/customerror.go index 2794da3..28186ca 100644 --- a/customerror.go +++ b/customerror.go @@ -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:"-"` } @@ -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() } @@ -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() } diff --git a/example_test.go b/example_test.go index 2d205de..c316097 100644 --- a/example_test.go +++ b/example_test.go @@ -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 +} diff --git a/options.go b/options.go index f6e99da..a0ff3fc 100644 --- a/options.go +++ b/options.go @@ -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 + } +}