-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payments): map wrapped errors to grpc errors in plugins
- Loading branch information
Showing
3 changed files
with
77 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,83 @@ | ||
package plugins | ||
|
||
import "errors" | ||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/formancehq/payments/internal/connectors/httpwrapper" | ||
"github.com/formancehq/payments/internal/models" | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
const ( | ||
FailureReasonInvalidRequest = "INVALID_REQUEST" | ||
FailureReasonInvalidConfig = "INVALID_CONFIG" | ||
FailureReasonBadRequestToUpstream = "BAD_REQUEST_TO_UPSTREAM" | ||
FailureReasonUnimplemented = "UNIMPLEMENTED" | ||
) | ||
|
||
var ( | ||
ErrNotImplemented = errors.New("not implemented") | ||
ErrNotYetInstalled = errors.New("not yet installed") | ||
) | ||
|
||
type Error struct { | ||
RawMessage string | ||
Status *status.Status | ||
} | ||
|
||
func NewError(code codes.Code, reason string, err error) error { | ||
st := status.Newf(code, err.Error()) | ||
var dtErr error | ||
st, dtErr = st.WithDetails(&errdetails.ErrorInfo{ | ||
Reason: reason, | ||
}) | ||
if dtErr != nil { | ||
return Error{ | ||
RawMessage: fmt.Sprintf("%s (%s)", err.Error(), dtErr.Error()), | ||
Status: status.Newf(code, err.Error()), | ||
} | ||
} | ||
|
||
return Error{ | ||
RawMessage: err.Error(), | ||
Status: st, | ||
} | ||
} | ||
|
||
func (e Error) Error() string { | ||
return fmt.Sprintf("PLUGIN ERROR: %d, %s", e.Status.Code(), e.RawMessage) | ||
} | ||
|
||
func (e Error) GRPCStatus() *status.Status { | ||
return e.Status | ||
} | ||
|
||
func translateErrorToGRPC(err error) error { | ||
var ( | ||
code codes.Code | ||
reason string | ||
) | ||
|
||
switch { | ||
case errors.Is(err, ErrNotImplemented): | ||
code = codes.Unimplemented | ||
reason = FailureReasonUnimplemented | ||
case errors.Is(err, models.ErrMissingFromPayloadInRequest), | ||
errors.Is(err, models.ErrMissingAccountInMetadata): | ||
code = codes.FailedPrecondition | ||
reason = FailureReasonInvalidRequest | ||
case errors.Is(err, models.ErrInvalidConfig): | ||
code = codes.FailedPrecondition | ||
reason = FailureReasonInvalidConfig | ||
case errors.Is(err, httpwrapper.ErrStatusCodeClientError): | ||
code = codes.InvalidArgument | ||
reason = FailureReasonBadRequestToUpstream | ||
default: | ||
code = codes.Internal | ||
} | ||
|
||
return NewError(code, reason, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters