Skip to content

Commit

Permalink
Merge pull request #2238 from ecordell/fix-crdberror
Browse files Browse the repository at this point in the history
crdb pool: handle cases with nil errors
  • Loading branch information
ecordell authored Feb 7, 2025
2 parents f678be6 + 8f8e832 commit b1b2ce0
Show file tree
Hide file tree
Showing 3 changed files with 700 additions and 3 deletions.
29 changes: 26 additions & 3 deletions internal/datastore/crdb/pool/sqlerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ type MaxRetryError struct {

func (e *MaxRetryError) Error() string {
if e.MaxRetries == 0 {
return "retries disabled: " + e.LastErr.Error()
if e.LastErr != nil {
return "retries disabled: " + e.LastErr.Error()
}
return "retries disabled"
}
if e.LastErr == nil {
return fmt.Sprintf("max retries reached (%d)", e.MaxRetries)
}
return fmt.Sprintf("max retries reached (%d): %s", e.MaxRetries, e.LastErr.Error())
}
Expand All @@ -53,10 +59,19 @@ type ResettableError struct {
Err error
}

func (e *ResettableError) Error() string { return "resettable error" + ": " + e.Err.Error() }
func (e *ResettableError) Error() string {
if e.Err == nil {
return "resettable error"
}
return "resettable error" + ": " + e.Err.Error()
}

func (e *ResettableError) Unwrap() error { return e.Err }

func (e *ResettableError) GRPCStatus() *status.Status {
if e.Err == nil {
return status.New(codes.Unavailable, "resettable error")
}
return spiceerrors.WithCodeAndDetails(
e.Unwrap(),
codes.Unavailable, // return unavailable so clients are know it's ok to retry
Expand All @@ -68,10 +83,18 @@ type RetryableError struct {
Err error
}

func (e *RetryableError) Error() string { return "retryable error" + ": " + e.Err.Error() }
func (e *RetryableError) Error() string {
if e.Err == nil {
return "retryable error"
}
return "retryable error" + ": " + e.Err.Error()
}
func (e *RetryableError) Unwrap() error { return e.Err }

func (e *RetryableError) GRPCStatus() *status.Status {
if e.Err == nil {
return status.New(codes.Unavailable, "resettable error")
}
return spiceerrors.WithCodeAndDetails(
e.Unwrap(),
codes.Unavailable, // return unavailable so clients are know it's ok to retry
Expand Down
Loading

0 comments on commit b1b2ce0

Please sign in to comment.