Skip to content

Commit

Permalink
Added comparison equal
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cataldo committed Jan 5, 2024
1 parent 79eab97 commit fe559db
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Go Error Detail
<!--suppress ALL -->
<img align="right" src="gopher-debug.png" alt="">

[![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)
Expand Down
3 changes: 3 additions & 0 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
46 changes: 35 additions & 11 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
}
14 changes: 12 additions & 2 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit fe559db

Please sign in to comment.