diff --git a/catalog.go b/catalog.go index 1ae50a5..2e3a470 100644 --- a/catalog.go +++ b/catalog.go @@ -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: diff --git a/customerror_test.go b/customerror_test.go index eb1e77a..3dbc7a9 100644 --- a/customerror_test.go +++ b/customerror_test.go @@ -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", }, @@ -80,7 +80,7 @@ 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", }, @@ -88,7 +88,7 @@ func TestNewLowLevel(t *testing.T) { 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", }, @@ -96,7 +96,7 @@ func TestNewLowLevel(t *testing.T) { 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", }, @@ -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 } diff --git a/example_test.go b/example_test.go index 557e9f3..e47c5bc 100644 --- a/example_test.go +++ b/example_test.go @@ -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 { @@ -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`. @@ -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: @@ -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 @@ -183,7 +183,7 @@ func ExampleNew_optionsWithTag() { fmt.Println(NewMissingError( "id", WithTag("test1", "test2"), - WithCode("E1010"), + WithErrorCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error")), )) @@ -191,7 +191,7 @@ func ExampleNew_optionsWithTag() { fmt.Println(NewMissingError( "id", WithTag("test1", "test2"), - WithCode("E1010"), + WithErrorCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error")), ).(*CustomError).APIError()) @@ -215,7 +215,7 @@ func ExampleNew_optionsWithFields() { "testKey1": "testValue1", "testKey2": "testValue2", }), - WithCode("E1010"), + WithErrorCode("E1010"), WithStatusCode(http.StatusNotAcceptable), WithError(errors.New("some error")), )) @@ -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()) @@ -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")), )) @@ -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()) diff --git a/language.go b/language.go index 827e806..75b1bca 100644 --- a/language.go +++ b/language.go @@ -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{ diff --git a/languageprefixtemplate.go b/languageprefixtemplate.go index 779cd6b..b10447b 100644 --- a/languageprefixtemplate.go +++ b/languageprefixtemplate.go @@ -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 ( diff --git a/options.go b/options.go index 926bba4..0e1e879 100644 --- a/options.go +++ b/options.go @@ -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 }