Skip to content

Commit

Permalink
Update EventError with Is and Unwrap functions (#221)
Browse files Browse the repository at this point in the history
* update EventError with Is and Unwrap functions for use in errors API

Signed-off-by: Daniel Rammer <[email protected]>

* changed EventError Is* functionality to check entire error stack

Signed-off-by: Daniel Rammer <[email protected]>

* added nil tests to EventError.Is function

Signed-off-by: Daniel Rammer <[email protected]>
Signed-off-by: Eduardo Apolinario <[email protected]>
  • Loading branch information
hamersaw authored and eapolinario committed Sep 13, 2023
1 parent be07d1c commit 2f38a97
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions flyteidl/clients/go/events/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors

import (
"context"
"errors"
"fmt"

"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
Expand Down Expand Up @@ -31,6 +32,24 @@ func (r EventError) Error() string {
return fmt.Sprintf("%s: %s, caused by [%s]", r.Code, r.Message, r.Cause.Error())
}

func (r *EventError) Is(target error) bool {
t, ok := target.(*EventError)
if !ok {
return false
}
if r == nil && t == nil {
return true
}
if r == nil || t == nil {
return false
}
return r.Code == t.Code && (r.Cause == t.Cause || t.Cause == nil) && (r.Message == t.Message || t.Message == "")
}

func (r *EventError) Unwrap() error {
return r.Cause
}

func WrapError(err error) error {
// check if error is gRPC, and convert into our own custom error
statusErr, ok := status.FromError(err)
Expand Down Expand Up @@ -78,46 +97,25 @@ func wrapf(code ErrorCode, cause error, msg string) error {

// Checks if the error is of type EventError and the ErrorCode is of type AlreadyExists
func IsAlreadyExists(err error) bool {
e, ok := err.(*EventError)
if ok {
return e.Code == AlreadyExists
}
return false
return errors.Is(err, &EventError{Code: AlreadyExists})
}

// Checks if the error is of type EventError and the ErrorCode is of type InvalidArgument
func IsInvalidArguments(err error) bool {
e, ok := err.(*EventError)
if ok {
return e.Code == InvalidArgument
}
return false
return errors.Is(err, &EventError{Code: InvalidArgument})
}

// Checks if the error is of type EventError and the ErrorCode is of type ExecutionNotFound
func IsNotFound(err error) bool {
e, ok := err.(*EventError)
if ok {
return e.Code == ExecutionNotFound
}
return false
return errors.Is(err, &EventError{Code: ExecutionNotFound})
}

// Checks if the error is of type EventError and the ErrorCode is of type ResourceExhausted
func IsResourceExhausted(err error) bool {
e, ok := err.(*EventError)
if ok {
return e.Code == ResourceExhausted
}
return false
return errors.Is(err, &EventError{Code: ResourceExhausted})
}

// Checks if the error is of type EventError and the ErrorCode is of type EventAlreadyInTerminalStateError
func IsEventAlreadyInTerminalStateError(err error) bool {
// TODO: don't rely on the specific type here as it could be wrapped in another object.
e, ok := err.(*EventError)
if ok {
return e.Code == EventAlreadyInTerminalStateError
}
return false
return errors.Is(err, &EventError{Code: EventAlreadyInTerminalStateError})
}

0 comments on commit 2f38a97

Please sign in to comment.