Skip to content

Commit

Permalink
Manually unwrap on trail.ToGRPC (#96)
Browse files Browse the repository at this point in the history
Manually unwrap and type-switch so we avoid the many inner loops of repeated
"IsError" calls.

I've snuck in a few other minor tweaks again.
  • Loading branch information
codingllama authored Aug 4, 2023
1 parent 39a23d1 commit a6ba0d5
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 198 deletions.
39 changes: 21 additions & 18 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import (
"os"
)

// traceDepth is the depth to be used by error constructors.
const traceDepth = 2

// NotFound returns new instance of not found error
func NotFound(message string, args ...interface{}) Error {
return newTrace(&NotFoundError{
Message: fmt.Sprintf(message, args...),
}, 2)
}, traceDepth)
}

// NotFoundError indicates that object has not been found
Expand Down Expand Up @@ -88,7 +91,7 @@ func IsNotFound(e error) bool {
func AlreadyExists(message string, args ...interface{}) Error {
return newTrace(&AlreadyExistsError{
Message: fmt.Sprintf(message, args...),
}, 2)
}, traceDepth)
}

// AlreadyExistsError indicates that there's a duplicate object that already
Expand Down Expand Up @@ -136,7 +139,7 @@ func IsAlreadyExists(e error) bool {
func BadParameter(message string, args ...interface{}) Error {
return newTrace(&BadParameterError{
Message: fmt.Sprintf(message, args...),
}, 2)
}, traceDepth)
}

// BadParameterError indicates that something is wrong with passed
Expand Down Expand Up @@ -181,7 +184,7 @@ func IsBadParameter(e error) bool {
func NotImplemented(message string, args ...interface{}) Error {
return newTrace(&NotImplementedError{
Message: fmt.Sprintf(message, args...),
}, 2)
}, traceDepth)
}

// NotImplementedError defines an error condition to describe the result
Expand Down Expand Up @@ -226,7 +229,7 @@ func IsNotImplemented(e error) bool {
func CompareFailed(message string, args ...interface{}) Error {
return newTrace(&CompareFailedError{
Message: fmt.Sprintf(message, args...),
}, 2)
}, traceDepth)
}

// CompareFailedError indicates a failed comparison (e.g. bad password or hash)
Expand Down Expand Up @@ -274,7 +277,7 @@ func IsCompareFailed(e error) bool {
func AccessDenied(message string, args ...interface{}) Error {
return newTrace(&AccessDeniedError{
Message: fmt.Sprintf(message, args...),
}, 2)
}, traceDepth)
}

// AccessDeniedError indicates denied access
Expand Down Expand Up @@ -325,35 +328,35 @@ func ConvertSystemError(err error) error {
if os.IsExist(innerError) {
return newTrace(&AlreadyExistsError{
Message: innerError.Error(),
}, 2)
}, traceDepth)
}
if os.IsNotExist(innerError) {
return newTrace(&NotFoundError{
Message: innerError.Error(),
}, 2)
}, traceDepth)
}
if os.IsPermission(innerError) {
return newTrace(&AccessDeniedError{
Message: innerError.Error(),
}, 2)
}, traceDepth)
}
switch realErr := innerError.(type) {
case *net.OpError:
return newTrace(&ConnectionProblemError{
Err: realErr,
}, 2)
}, traceDepth)
case *os.PathError:
message := fmt.Sprintf("failed to execute command %v error: %v", realErr.Path, realErr.Err)
return newTrace(&AccessDeniedError{
Message: message,
}, 2)
}, traceDepth)
case x509.SystemRootsError, x509.UnknownAuthorityError:
return newTrace(&TrustError{Err: innerError}, 2)
return newTrace(&TrustError{Err: innerError}, traceDepth)
}
if _, ok := innerError.(net.Error); ok {
return newTrace(&ConnectionProblemError{
Err: innerError,
}, 2)
}, traceDepth)
}
return err
}
Expand All @@ -363,7 +366,7 @@ func ConnectionProblem(err error, message string, args ...interface{}) Error {
return newTrace(&ConnectionProblemError{
Message: fmt.Sprintf(message, args...),
Err: err,
}, 2)
}, traceDepth)
}

// ConnectionProblemError indicates a network related problem
Expand Down Expand Up @@ -419,7 +422,7 @@ func IsConnectionProblem(e error) bool {
func LimitExceeded(message string, args ...interface{}) Error {
return newTrace(&LimitExceededError{
Message: fmt.Sprintf(message, args...),
}, 2)
}, traceDepth)
}

// LimitExceededError indicates rate limit or connection limit problem
Expand Down Expand Up @@ -464,7 +467,7 @@ func Trust(err error, message string, args ...interface{}) Error {
return newTrace(&TrustError{
Message: fmt.Sprintf(message, args...),
Err: err,
}, 2)
}, traceDepth)
}

// TrustError indicates trust-related validation error (e.g. untrusted cert)
Expand Down Expand Up @@ -522,7 +525,7 @@ func OAuth2(code, message string, query url.Values) Error {
Code: code,
Message: message,
Query: query,
}, 2)
}, traceDepth)
}

// OAuth2Error defined an error used in OpenID Connect Flow (OIDC)
Expand Down Expand Up @@ -589,7 +592,7 @@ func Retry(err error, message string, args ...interface{}) Error {
return newTrace(&RetryError{
Message: fmt.Sprintf(message, args...),
Err: err,
}, 2)
}, traceDepth)
}

// RetryError indicates a transient error type
Expand Down
2 changes: 1 addition & 1 deletion errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func TestGoErrorWrap_IsError_allTypes(t *testing.T) {
},
{
name: "RetryError",
instance: Retry(errors.New("underyling error"), "message"),
instance: Retry(errors.New("underlying error"), "message"),
isError: IsRetryError,
},
}
Expand Down
Loading

0 comments on commit a6ba0d5

Please sign in to comment.