Skip to content

Commit

Permalink
Merge branch 'purposeinplay:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgepurposeinplay authored Jan 19, 2025
2 parents 8966c82 + e1bfe26 commit dfa4c75
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 63 deletions.
38 changes: 33 additions & 5 deletions errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ 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) }

// StringPtr returns the ErrorCode as a string pointer.
func (c ErrorCode) StringPtr() *string { p := string(c); return &p }

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

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

return &sts
}

// Available error types.
const (
ErrorTypeInvalid ErrorType = "invalid"
Expand All @@ -58,13 +71,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 +100,17 @@ func IsErrorCode(err error, code ErrorCode) bool {

return false
}

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

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

// StringPtr returns the ErrorDetailCode as a string pointer.
func (c ErrorDetailCode) StringPtr() *string { p := string(c); return &p }

// ErrorDetail provides explicit details on an Error.
type ErrorDetail struct {
Code ErrorDetailCode
Message string
}
11 changes: 6 additions & 5 deletions errors/errorsgrpc/errorgrpc_client_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/purposeinplay/go-commons/errors"
"github.com/purposeinplay/go-commons/errors/errorsgrpc"
commonserr "github.com/purposeinplay/go-commons/errors/proto/commons/error/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -64,11 +65,11 @@ func TestErrors(t *testing.T) {
defer close(done)

serveErr := grpcServer.Serve(lis)
req.NoError(serveErr)
assert.NoError(t, serveErr)
}()

clientConn, err := grpc.Dial(
"bufnet",
clientConn, err := grpc.NewClient(
"passthrough://bufnet",
grpc.WithContextDialer(bufDialer),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(errorsgrpc.UnmarshalErrorUnaryClientInterceptor()),
Expand All @@ -90,8 +91,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
4 changes: 2 additions & 2 deletions errors/errorsgrpc/errorsgrpc_client_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func UnmarshalErrorUnaryClientInterceptor() grpc.UnaryClientInterceptor {

return &errors.Error{
Type: errors.ErrorType(sts.Message()),
Code: errors.ErrorCode(errResp.ErrorCode),
Details: errResp.Message,
Code: errors.ErrorCode(errResp.ErrorCode.String()),
Message: errResp.Message,
}
}
}
10 changes: 7 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,11 @@ 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,
//nolint:gosec // disable G115
ErrorCode: commonserr.ErrorResponse_ErrorCode(c),
Message: err.Message,
}
}
5 changes: 3 additions & 2 deletions errors/errorsgrpc/errorsgrpc_error_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errorsgrpc_test

import (
"strconv"
"testing"

// nolint: staticcheck
Expand All @@ -16,8 +17,8 @@ func TestRetrieveDetails(t *testing.T) {

appErr := &errors.Error{
Type: errors.ErrorTypeInvalid,
Code: errors.ErrorCode(1),
Details: "message",
Code: errors.ErrorCode(strconv.Itoa(int(rune(1)))),
Message: "message",
}

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 dfa4c75

Please sign in to comment.