Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): failover error bugs #858

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ func NewErrorDeadlineExceeded(msg string) error {
// 1. Failed to put the blob in the disperser's queue (disperser is down)
// 2. Timed out before getting confirmed onchain (batcher is down)
// 3. Insufficient signatures (eigenda network is down)
//
// One can check if an error is an ErrorFailover by using errors.Is:
//
// failoverErr := NewErrorFailover(someOtherErr)
// if !errors.Is(wrappedFailoverErr, &ErrorFailover{}) {
// // do something...
// }
type ErrorFailover struct {
Err error
}
Expand All @@ -90,9 +97,28 @@ func NewErrorFailover(err error) *ErrorFailover {
}

func (e *ErrorFailover) Error() string {
if e.Err == nil {
return "Failover"
}
return fmt.Sprintf("Failover: %s", e.Err.Error())
}

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

// Is only checks the type of the error, not the underlying error.
// This is because we want to be able to check that an error is an ErrorFailover,
// even when wrapped. This can now be done with errors.Is.
//
// baseErr := fmt.Errorf("some error")
// failoverErr := NewErrorFailover(baseErr)
// wrappedFailoverErr := fmt.Errorf("some extra context: %w", failoverErr)
//
// if !errors.Is(wrappedFailoverErr, &ErrorFailover{}) {
// // do something...
// }
func (e *ErrorFailover) Is(target error) bool {
_, ok := target.(*ErrorFailover)
bxue-l2 marked this conversation as resolved.
Show resolved Hide resolved
return ok
}
46 changes: 46 additions & 0 deletions api/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package api

import (
"errors"
"fmt"
"testing"
)

func TestErrorFailoverErrorsIs(t *testing.T) {
baseErr := fmt.Errorf("base error")
failoverErr := NewErrorFailover(baseErr)
otherFailoverErr := NewErrorFailover(fmt.Errorf("some other error"))
wrappedFailoverErr := fmt.Errorf("wrapped: %w", failoverErr)

if !errors.Is(failoverErr, failoverErr) {
t.Error("should match itself")
}

if !errors.Is(failoverErr, baseErr) {
t.Error("should match base error")
}

if errors.Is(failoverErr, fmt.Errorf("some other error")) {
t.Error("should not match other errors")
}

if !errors.Is(failoverErr, otherFailoverErr) {
t.Error("should match other failover error")
}

if !errors.Is(failoverErr, &ErrorFailover{}) {
t.Error("should match ErrorFailover type")
}

if !errors.Is(wrappedFailoverErr, &ErrorFailover{}) {
t.Error("should match ErrorFailover type even when wrapped")
}

}

func TestErrorFailoverZeroValue(t *testing.T) {
var failoverErr ErrorFailover
if failoverErr.Error() != "Failover" {
t.Error("should return 'Failover' for zero value")
}
}
Loading