Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesfsp committed Jun 23, 2023
1 parent 30eccd1 commit 00f550b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
6 changes: 3 additions & 3 deletions catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
var (
// ErrCatalogErrorNotFound is returned when a custom error isn't found in
// the catalog.
ErrCatalogErrorNotFound = NewNotFoundError("error", WithCode("CE_ERR_CATALOG_ERR_NOT_FOUND"))
ErrCatalogErrorNotFound = NewNotFoundError("error", WithErrorCode("CE_ERR_CATALOG_ERR_NOT_FOUND"))

// ErrCatalogInvalidName is returned when a catalog name is invalid.
ErrCatalogInvalidName = NewInvalidError("name", WithCode("CE_ERR_CATALOG_INVALID_NAME"))
ErrCatalogInvalidName = NewInvalidError("name", WithErrorCode("CE_ERR_CATALOG_INVALID_NAME"))

// ErrErrorCodeInvalidCode is returned when an error code is invalid.
ErrErrorCodeInvalidCode = NewInvalidError("error code. It requires typeOf, and subject", WithCode("CE_ERR_INVALID_ERROR_CODE"))
ErrErrorCodeInvalidCode = NewInvalidError("error code. It requires typeOf, and subject", WithErrorCode("CE_ERR_INVALID_ERROR_CODE"))

// ErrorCodeRegex is a regular expression to validate error codes. It's
// designed to match four distinct patterns:
Expand Down
18 changes: 9 additions & 9 deletions customerror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestNewLowLevel(t *testing.T) {
name: "should work - with message, and code",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code)},
opts: []Option{WithErrorCode(code)},
},
want: "E1010: Failed to create something",
},
Expand Down Expand Up @@ -80,23 +80,23 @@ func TestNewLowLevel(t *testing.T) {
name: "should work - with message, code, and error",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code), WithError(ErrFailedToReachServer)},
opts: []Option{WithErrorCode(code), WithError(ErrFailedToReachServer)},
},
want: "E1010: Failed to create something. Original Error: Failed to reach servers",
},
{
name: "should work - with message, code, error, and deep error",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code), WithError(ErrFailedToReachServerDeep)},
opts: []Option{WithErrorCode(code), WithError(ErrFailedToReachServerDeep)},
},
want: "E1010: Failed to create something. Original Error: Failed to reach servers. Servers are broken",
},
{
name: "should work - with message, code, error, deep error, and status code",
args: args{
message: failedCreateSomethingMsg,
opts: []Option{WithCode(code), WithError(ErrFailedToReachServerDeep), WithStatusCode(statusCode)},
opts: []Option{WithErrorCode(code), WithError(ErrFailedToReachServerDeep), WithStatusCode(statusCode)},
},
want: "E1010: Failed to create something. Original Error: Failed to reach servers. Servers are broken",
},
Expand All @@ -113,11 +113,11 @@ func TestNewLowLevel(t *testing.T) {
}

func TestBuiltin(t *testing.T) {
ErrFailedToCreateFile := NewFailedToError("create file", WithCode("E1010"))
ErrInvalidPath := NewInvalidError("path", WithCode("E1010"))
ErrMissingPath := NewMissingError("path", WithCode("E1010"))
ErrRequiredPath := NewRequiredError("path is", WithCode("E1010"))
ErrNotFound := NewHTTPError(http.StatusNotFound, WithCode("E1010"))
ErrFailedToCreateFile := NewFailedToError("create file", WithErrorCode("E1010"))
ErrInvalidPath := NewInvalidError("path", WithErrorCode("E1010"))
ErrMissingPath := NewMissingError("path", WithErrorCode("E1010"))
ErrRequiredPath := NewRequiredError("path is", WithErrorCode("E1010"))
ErrNotFound := NewHTTPError(http.StatusNotFound, WithErrorCode("E1010"))

testFunc := func(e error) error { return e }

Expand Down
30 changes: 15 additions & 15 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func checkIfStringContainsMany(s string, subs ...string) []string {
// check, and instrospect custom errors.
func ExampleNew() {
// Custom static error definition.
ErrMissingID := NewMissingError("id", WithCode("E1010"))
ErrMissingID := NewMissingError("id", WithErrorCode("E1010"))

// Some function, for demo purpose.
SomeFunc := func(id string) error {
Expand All @@ -39,7 +39,7 @@ func ExampleNew() {
}

// Dynamic custom error.
return NewFailedToError("write to disk", WithCode("E1523"))
return NewFailedToError("write to disk", WithErrorCode("E1523"))
}

// Case: Without `id`, returns `ErrMissingID`.
Expand Down Expand Up @@ -78,7 +78,7 @@ func ExampleNew() {
//nolint:errorlint,forcetypeassert
func ExampleNew_options() {
fmt.Println(
NewMissingError("id", WithCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error"))).(*CustomError).APIError(),
NewMissingError("id", WithErrorCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error"))).(*CustomError).APIError(),
)

// output:
Expand Down Expand Up @@ -156,14 +156,14 @@ func ExampleNew_newHTTPError() {
//nolint:errorlint,forcetypeassert
func ExampleNew_newNoMessage() {
fmt.Println(New("", WithStatusCode(http.StatusAccepted)))
fmt.Println(New("", WithStatusCode(http.StatusAccepted), WithCode("E1010")))
fmt.Println(New("", WithStatusCode(http.StatusAccepted), WithErrorCode("E1010")))
fmt.Println(New("", WithStatusCode(http.StatusAccepted)).(*CustomError).APIError())
fmt.Println(New("", WithStatusCode(http.StatusAccepted), WithCode("E1010")).(*CustomError).APIError())
fmt.Println(New("", WithStatusCode(http.StatusAccepted), WithErrorCode("E1010")).(*CustomError).APIError())

fmt.Println(New("", WithCode("E1010")))
fmt.Println(New("", WithCode("E1010"), WithStatusCode(http.StatusAccepted)))
fmt.Println(New("", WithCode("E1010")).(*CustomError).APIError())
fmt.Println(New("", WithCode("E1010"), WithStatusCode(http.StatusAccepted)).(*CustomError).APIError())
fmt.Println(New("", WithErrorCode("E1010")))
fmt.Println(New("", WithErrorCode("E1010"), WithStatusCode(http.StatusAccepted)))
fmt.Println(New("", WithErrorCode("E1010")).(*CustomError).APIError())
fmt.Println(New("", WithErrorCode("E1010"), WithStatusCode(http.StatusAccepted)).(*CustomError).APIError())

// output:
// Accepted
Expand All @@ -183,15 +183,15 @@ func ExampleNew_optionsWithTag() {
fmt.Println(NewMissingError(
"id",
WithTag("test1", "test2"),
WithCode("E1010"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
))

fmt.Println(NewMissingError(
"id",
WithTag("test1", "test2"),
WithCode("E1010"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
).(*CustomError).APIError())
Expand All @@ -215,7 +215,7 @@ func ExampleNew_optionsWithFields() {
"testKey1": "testValue1",
"testKey2": "testValue2",
}),
WithCode("E1010"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
))
Expand All @@ -227,7 +227,7 @@ func ExampleNew_optionsWithFields() {
"testKey1": "testValue1",
"testKey2": "testValue2",
}),
WithCode("E1010"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
).(*CustomError).APIError())
Expand Down Expand Up @@ -259,7 +259,7 @@ func ExampleNew_optionsWithField() {
WithTag("test1", "test2"),
WithField("testKey1", "testValue1"),
WithField("testKey2", "testValue2"),
WithCode("E1010"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
))
Expand All @@ -269,7 +269,7 @@ func ExampleNew_optionsWithField() {
WithTag("test1", "test2"),
WithField("testKey1", "testValue1"),
WithField("testKey2", "testValue2"),
WithCode("E1010"),
WithErrorCode("E1010"),
WithStatusCode(http.StatusNotAcceptable),
WithError(errors.New("some error")),
).(*CustomError).APIError())
Expand Down
6 changes: 3 additions & 3 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ const (

var (
// ErrInvalidLanguageCode is returned when a language code is invalid.
ErrInvalidLanguageCode = NewInvalidError("it must be a string, two-letter lowercase ISO 639-1 code OR two-letter lowercase ISO 639-1 code followed by an optional hyphen AND a two-letter uppercase ISO 3166-1 alpha-2 country code", WithCode("CE_ERR_INVALID_LANG_CODE"))
ErrInvalidLanguageCode = NewInvalidError("it must be a string, two-letter lowercase ISO 639-1 code OR two-letter lowercase ISO 639-1 code followed by an optional hyphen AND a two-letter uppercase ISO 3166-1 alpha-2 country code", WithErrorCode("CE_ERR_INVALID_LANG_CODE"))

// ErrInvalidLanguageErrorMessage is returned when an error message is invalid.
ErrInvalidLanguageErrorMessage = NewInvalidError("it must be a string, at least 3 characters long", WithCode("CE_ERR_INVALID_LANG_ERROR_MESSAGE"))
ErrInvalidLanguageErrorMessage = NewInvalidError("it must be a string, at least 3 characters long", WithErrorCode("CE_ERR_INVALID_LANG_ERROR_MESSAGE"))

// ErrInvalidLanguageMessageMap is returned when a LanguageMessageMap is
// invalid.
ErrInvalidLanguageMessageMap = NewInvalidError("it must be a non-nil map of language codes to error messages", WithCode("CE_ERR_INVALID_LANGUAGE_MESSAGE_MAP"))
ErrInvalidLanguageMessageMap = NewInvalidError("it must be a non-nil map of language codes to error messages", WithErrorCode("CE_ERR_INVALID_LANGUAGE_MESSAGE_MAP"))

// BuiltInLanguages is a list of built-in prefixes languages.
BuiltInLanguages = []string{
Expand Down
4 changes: 2 additions & 2 deletions languageprefixtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ var (
"template",
"Please set one using `SetErrorPrefixMap`",
strings.Join(BuiltInLanguages, ", "),
), WithCode("CE_ERR_TEMPLATE_NOT_FOUND"))
), WithErrorCode("CE_ERR_TEMPLATE_NOT_FOUND"))

// ErrLanguageNotFound is returned when a language isn't found in the map.
ErrLanguageNotFound = NewNotFoundError("language. Please set one using `SetErrorPrefixMap`", WithCode("CE_ERR_LANGUAGE_NOT_FOUND"))
ErrLanguageNotFound = NewNotFoundError("language. Please set one using `SetErrorPrefixMap`", WithErrorCode("CE_ERR_LANGUAGE_NOT_FOUND"))
)

type (
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func WithMessage(msg string) Option {
}
}

// WithCode allows to specify an error code, such as "E1010".
func WithCode(code string) Option {
// WithErrorCode allows to specify an error code, such as "E1010".
func WithErrorCode(code string) Option {
return func(cE *CustomError) {
cE.Code = code
}
Expand Down

0 comments on commit 00f550b

Please sign in to comment.