Skip to content

Commit

Permalink
feat(errors): add error details. use string insted of int for error c…
Browse files Browse the repository at this point in the history
…odes
  • Loading branch information
bradub committed Jan 17, 2025
1 parent 2e419f8 commit 2f83e0f
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 59 deletions.
29 changes: 24 additions & 5 deletions errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ type (
// ErrorType is mapped to HTTP codes.
ErrorType string
// ErrorCode represents application error codes.
ErrorCode int
ErrorCode string
)

func (c ErrorCode) String() string { return string(c) }

func (t ErrorType) String() string {
return string(t)
}
Expand All @@ -45,6 +47,13 @@ func (t ErrorType) HTTPStatus() int {
}
}

// HTTPStatusInt32Ptr returns the corresponding HTTP Status as an int32 pointer.
func (t ErrorType) HTTPStatusInt32Ptr() *int32 {
sts := int32(t.HTTPStatus())

return &sts
}

// Available error types.
const (
ErrorTypeInvalid ErrorType = "invalid"
Expand All @@ -58,13 +67,14 @@ const (

// Error object.
type Error struct {
Type ErrorType
Code ErrorCode
Details string
Type ErrorType
Code ErrorCode
Message string
ErrorDetails []ErrorDetail
}

func (e *Error) Error() string {
return fmt.Sprintf("type: %s, code: %d, details: %s", e.Type, e.Code, e.Details)
return fmt.Sprintf("type: %s, code: %s, details: %s", e.Type, e.Code, e.Message)
}

// IsErrorType checks if the error is of the given type.
Expand All @@ -86,3 +96,12 @@ func IsErrorCode(err error, code ErrorCode) bool {

return false
}

// ErrorDetailCode contains additional specific codes to provide context to the error.
type ErrorDetailCode string

// ErrorDetail provides explicit details on an Error.
type ErrorDetail struct {
Code ErrorDetailCode
Message string
}
4 changes: 2 additions & 2 deletions errors/errorsgrpc/errorgrpc_client_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func TestErrors(t *testing.T) {
req.Equal(
&errors.Error{
Type: errors.ErrorTypeNotFound,
Code: 1,
Details: "not found",
Code: "1",
Message: "not found",
},
appErr,
)
Expand Down
2 changes: 1 addition & 1 deletion errors/errorsgrpc/errorsgrpc_client_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func UnmarshalErrorUnaryClientInterceptor() grpc.UnaryClientInterceptor {
return &errors.Error{
Type: errors.ErrorType(sts.Message()),
Code: errors.ErrorCode(errResp.ErrorCode),
Details: errResp.Message,
Message: errResp.Message,
}
}
}
9 changes: 6 additions & 3 deletions errors/errorsgrpc/errorsgrpc_error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errorsgrpc
import (
"context"
"fmt"
"strconv"

// nolint: staticcheck
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -119,7 +120,7 @@ func (h PanicErrorHandler) ReportPanic(
ctx,
&errors.Error{
Type: errors.ErrorTypePanic,
Details: fmt.Sprintf("%+v", p),
Message: fmt.Sprintf("%+v", p),
},
)
}
Expand All @@ -136,8 +137,10 @@ func (h PanicErrorHandler) ReportError(
}

func errorToErrorResponse(err *errors.Error) *commonserr.ErrorResponse {
c, _ := strconv.Atoi(err.Code.String())

return &commonserr.ErrorResponse{
ErrorCode: commonserr.ErrorResponse_ErrorCode(err.Code),
Message: err.Details,
ErrorCode: commonserr.ErrorResponse_ErrorCode(c),
Message: err.Message,
}
}
6 changes: 4 additions & 2 deletions errors/errorsgrpc/errorsgrpc_error_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package errorsgrpc_test

import "fmt"

import (
"testing"

Expand All @@ -16,8 +18,8 @@ func TestRetrieveDetails(t *testing.T) {

appErr := &errors.Error{
Type: errors.ErrorTypeInvalid,
Code: errors.ErrorCode(1),
Details: "message",
Code: errors.ErrorCode(fmt.Sprint(rune(1)),
Message: "message",

Check failure on line 22 in errors/errorsgrpc/errorsgrpc_error_handler_test.go

View workflow job for this annotation

GitHub Actions / Lint

missing ',' in argument list (typecheck)
}

Check failure on line 23 in errors/errorsgrpc/errorsgrpc_error_handler_test.go

View workflow job for this annotation

GitHub Actions / Lint

expected operand, found '}' (typecheck)

sts, err := (&errorsgrpc.PanicErrorHandler{}).ErrorToGRPCStatus(appErr)
Expand Down
28 changes: 17 additions & 11 deletions errors/go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
module github.com/purposeinplay/go-commons/errors

go 1.20
go 1.22

toolchain go1.23.4

require (
github.com/golang/protobuf v1.5.3
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.26.0
google.golang.org/grpc v1.59.0
google.golang.org/grpc/examples v0.0.0-20231205201002-0866ce06badc
google.golang.org/protobuf v1.31.0
github.com/golang/protobuf v1.5.4
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.10.0
go.uber.org/zap v1.27.0
google.golang.org/grpc v1.69.2
google.golang.org/grpc/examples v0.0.0-20250110041721-2d4daf347590
google.golang.org/protobuf v1.36.2
gorm.io/gorm v1.25.12
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250106144421-5f5ef82da422 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
82 changes: 48 additions & 34 deletions errors/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion errors/proto/commons/error/v1/error.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f83e0f

Please sign in to comment.