Skip to content

Commit

Permalink
fix: unwrap of code (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedhyw authored Mar 17, 2023
1 parent 79a3b8a commit cd7a114
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 44 deletions.
6 changes: 6 additions & 0 deletions internal/pkg/multierr/multierr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package multierr

type MultiError interface {
error
Unwrap() []error
}
58 changes: 41 additions & 17 deletions pkg/v1/grpcerr/grpcerr_generated.go

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

34 changes: 29 additions & 5 deletions pkg/v1/grpcerr/grpcerr_generated.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package grpcerr
import (
"errors"

"github.com/hedhyw/semerr/internal/pkg/multierr"
"github.com/hedhyw/semerr/pkg/v1/semerr"

"google.golang.org/grpc/codes"
Expand All @@ -14,16 +15,39 @@ import (
// Code returns grpc status code for err. In case of joined errors
// it returns the first code found in the chain.
func Code(err error) codes.Code {
switch {
case err == nil:
return codeRecursion(err)
}

func codeRecursion(err error) codes.Code {
switch err.(type) {
case nil:
return codes.OK
{{- range $errorDef := . }}
case errors.As(err, &semerr.{{ $errorDef.Name }}{}):
case semerr.{{ $errorDef.Name }}:
return {{ $errorDef.GRPCStatus }}
{{- end }}
default:
return getGRPCErrorCode(err)
}

grpcCode := getGRPCErrorCode(err)
if grpcCode != codes.Unknown {
return grpcCode
}

if err := errors.Unwrap(err); err != nil {
return codeRecursion(err)
}

if multiErr, ok := err.(multierr.MultiError); ok && multiErr != nil {
for _, err := range multiErr.Unwrap() {
code := codeRecursion(err)

if code != codes.Unknown {
return code
}
}
}

return codes.Unknown
}

func getGRPCErrorCode(err error) codes.Code {
Expand Down
58 changes: 41 additions & 17 deletions pkg/v1/httperr/httperr_generated.go

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

34 changes: 29 additions & 5 deletions pkg/v1/httperr/httperr_generated.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,46 @@ import (
"errors"
"net/http"

"github.com/hedhyw/semerr/internal/pkg/multierr"
"github.com/hedhyw/semerr/pkg/v1/semerr"
)

// Code returns http status code for err. In case of joined errors
// it returns the first code found in the chain.
func Code(err error) int {
switch {
case err == nil:
code := codeRecursion(err)
if code == 0 {
return http.StatusInternalServerError
}

return code
}

func codeRecursion(err error) int {
switch err.(type) {
case nil:
return http.StatusOK
{{- range $errorDef := . }}
case errors.As(err, &semerr.{{ $errorDef.Name }}{}):
case semerr.{{ $errorDef.Name }}:
return {{ $errorDef.HTTPStatus }}
{{- end }}
default:
return http.StatusInternalServerError
}

if err := errors.Unwrap(err); err != nil {
return codeRecursion(err)
}

if multiErr, ok := err.(multierr.MultiError); ok && multiErr != nil {
for _, err := range multiErr.Unwrap() {
code := codeRecursion(err)

if code != 0 {
return code
}
}
}

return 0
}

// Wrap wraps the `err` with an error corresponding to the `code`.
Expand Down

0 comments on commit cd7a114

Please sign in to comment.