diff --git a/README.md b/README.md index 867cf3a..c1762cc 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Go Error Detail -[![Project status](https://img.shields.io/badge/version-v1.0.0-vividgreen.svg)](https://github.com/GabrielHCataldo/go-error-detail/releases/tag/v1.0.5) +[![Project status](https://img.shields.io/badge/version-v1.0.1-vividgreen.svg)](https://github.com/GabrielHCataldo/go-error-detail/releases/tag/v1.0.1) [![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-error-detail)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-error-detail) [![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-error-detail/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-error-detail?branch=main) [![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-error-detail/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-error-detail) diff --git a/_example/main.go b/_example/main.go index ce09fb4..f77a4a1 100644 --- a/_example/main.go +++ b/_example/main.go @@ -31,4 +31,7 @@ func all() { logger.Error(errors.NewByErrSkipCaller(1, errJson)) logger.Error(errors.NewEByErrSkipCaller(1, errJson, "/endpoint")) logger.Error(errors.NewEByErrSkipCaller(1, errJson, "/endpoint")) + err = errors.New("test") + target := errors.New("test") + logger.Info("errors is:", errors.Is(err, target)) } diff --git a/errors/errors.go b/errors/errors.go index 7e7f0a5..f142878 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -11,11 +11,11 @@ import ( type ErrorDetail struct { // File name file from caller new error - File string `json:"file"` + File string `json:"file,omitempty"` // Line from caller new error - Line int `json:"line"` + Line int `json:"line,omitempty"` // Message error - Message string `json:"message"` + Message string `json:"message,omitempty"` // Endpoint from error Endpoint string `json:"endpoint,omitempty"` } @@ -105,19 +105,43 @@ func NewEByErrSkipCaller(skipCaller int, err error, endpoint string) *ErrorDetai return e } -func NewError(message ...any) error { +// NewErr new error interface +func NewErr(message ...any) error { return errors.New(printMessage(message...)) } +// Is validate equal errors, if ErrorDetail we only consider the ErrorDetail.Message field +func Is(err, target error) bool { + if IsErrorDetail(err) && IsErrorDetail(target) { + errDetail, _ := parseErrorToDetail(err) + targetDetail, _ := parseErrorToDetail(target) + return equal(*errDetail, *targetDetail) + } + return errors.Is(err, target) +} + +// IsErrorDetail check if error interface is ErrorDetail +func IsErrorDetail(err error) bool { + _, errParse := parseErrorToDetail(err) + return errParse == nil +} + +// Error print the error as a string, genetic implementation of error in go +func (e *ErrorDetail) Error() string { + b, _ := json.Marshal(e) + return string(b) +} + +func equal(a, b ErrorDetail) bool { + return a.Message == b.Message +} + func printMessage(v ...any) string { return strings.Replace(fmt.Sprintln(v...), "\n", "", -1) } -func (e *ErrorDetail) Error() string { - r := "" - if helper.IsNotEmpty(e) { - b, _ := json.Marshal(e) - r = string(b) - } - return r +func parseErrorToDetail(err error) (*ErrorDetail, error) { + var dest ErrorDetail + errConvert := helper.ConvertToDest(err, &dest) + return &dest, errConvert } diff --git a/errors/errors_test.go b/errors/errors_test.go index c835211..c53be46 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -34,8 +34,18 @@ func TestNewEByErrSkipCaller(t *testing.T) { logger.Info("err:", NewEByErrSkipCaller(1, errors.New("test error detail"), "/test")) } -func TestNewError(t *testing.T) { - logger.Info("err:", NewError("test error detail")) +func TestNewErr(t *testing.T) { + logger.Info("err:", NewErr("test error detail")) +} + +func TestIs(t *testing.T) { + err := errors.New("test") + target := New("test") + logger.Info("errors is:", Is(err, target)) + + errDetail := New("test") + targetDetail := New("test") + logger.Info("errors is:", Is(errDetail, targetDetail)) } func TestError(t *testing.T) { diff --git a/go.mod b/go.mod index fb98a9f..239464f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/GabrielHCataldo/go-error-detail go 1.21.3 require ( - github.com/GabrielHCataldo/go-helper v1.0.0 + github.com/GabrielHCataldo/go-helper v1.0.5 github.com/GabrielHCataldo/go-logger v1.0.9 ) diff --git a/go.sum b/go.sum index d27e717..06ccf12 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ -github.com/GabrielHCataldo/go-helper v1.0.0 h1:QjZJJqjOm3L9NxFrYe+XblOXQN04/rR5j9tfyRvFHdo= -github.com/GabrielHCataldo/go-helper v1.0.0/go.mod h1:PQzAUM8zkykqh91zBwTjjUYwMU/wjDJb4bkbCI3ddbk= +github.com/GabrielHCataldo/go-helper v1.0.4 h1:7AAKPL0pqfcytMOXfa65LJ3URQUHUAxrc8GZfSW0R5g= +github.com/GabrielHCataldo/go-helper v1.0.4/go.mod h1:PQzAUM8zkykqh91zBwTjjUYwMU/wjDJb4bkbCI3ddbk= +github.com/GabrielHCataldo/go-helper v1.0.5 h1:YvJKcPU/L/Q4QWw7oc3MITefAWosRtK2bVyf2ZgpptE= +github.com/GabrielHCataldo/go-helper v1.0.5/go.mod h1:PQzAUM8zkykqh91zBwTjjUYwMU/wjDJb4bkbCI3ddbk= github.com/GabrielHCataldo/go-logger v1.0.9 h1:dhg/Zrxl8iygjEO/rQezsZk6v9RCsEnch30hYwUGK8U= github.com/GabrielHCataldo/go-logger v1.0.9/go.mod h1:xFMqeAgNIh2QK0wYTMbcxByzC2cXOU3mb+olBgXxgl8= github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=