Skip to content

Commit

Permalink
chore(simple_client): Add explicit ServiceUnavailable error type
Browse files Browse the repository at this point in the history
  • Loading branch information
EthenNotEthan committed Dec 3, 2024
1 parent aeeb503 commit d5f1145
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions client/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"net/http"
)

var (
// 503 error type informing rollup to failover to other DA location
ErrServiceUnavailable = fmt.Errorf("eigenda service is unavailable")
)

// TODO: Add support for custom http client option
type Config struct {
URL string
Expand All @@ -23,18 +28,11 @@ type SimpleCommitmentClient struct {
httpClient *http.Client
}

func New(cfg *Config) *SimpleCommitmentClient {
return &SimpleCommitmentClient{
cfg,
http.DefaultClient,
}
}

// Health indicates if the server is operational; useful for event based awaits
// when integration testing
func (c *SimpleCommitmentClient) Health() error {
url := c.cfg.URL + "/health"
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
Expand All @@ -43,7 +41,6 @@ func (c *SimpleCommitmentClient) Health() error {
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received bad status code: %d", resp.StatusCode)
Expand Down Expand Up @@ -81,7 +78,8 @@ func (c *SimpleCommitmentClient) GetData(ctx context.Context, comm []byte) ([]by
return b, nil
}

// SetData writes raw byte data to DA and returns the respective certificate
// SetData writes raw byte data to DA and returns the associated certificate
// which should be verified within the proxy
func (c *SimpleCommitmentClient) SetData(ctx context.Context, b []byte) ([]byte, error) {
url := fmt.Sprintf("%s/put?commitment_mode=simple", c.cfg.URL)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b))
Expand All @@ -100,6 +98,10 @@ func (c *SimpleCommitmentClient) SetData(ctx context.Context, b []byte) ([]byte,
return nil, err
}

if resp.StatusCode == http.StatusServiceUnavailable {
return nil, ErrServiceUnavailable
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to store data: %v, err = %s", resp.StatusCode, string(b))
}
Expand Down

0 comments on commit d5f1145

Please sign in to comment.