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: custom error wrapped around error and commitment meta #134

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 33 additions & 0 deletions server/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package server

import (
"fmt"

"github.com/Layr-Labs/eigenda-proxy/commitments"
)

// HandlerError is an interface that can be either a simple error or a MetaError
type HandlerError interface {
error
isHandlerError() // marker method
}
samlaf marked this conversation as resolved.
Show resolved Hide resolved

// MetaError includes both an error and commitment metadata
type MetaError struct {
Err error
Meta commitments.CommitmentMeta
}

func (me MetaError) Error() string {
return fmt.Sprintf("Error: %s (Mode: %s, CertVersion: %b)",
me.Err.Error(),
me.Meta.Mode,
me.Meta.CertVersion)
}

func (MetaError) isHandlerError() {}
samlaf marked this conversation as resolved.
Show resolved Hide resolved

// NewMetaError creates a new MetaError
func NewMetaError(err error, meta commitments.CommitmentMeta) HandlerError {
return MetaError{Err: err, Meta: meta}
}
45 changes: 36 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,18 @@ func WithMetrics(handleFn func(http.ResponseWriter, *http.Request) (commitments.
recordDur := m.RecordRPCServerRequest(r.Method)

meta, err := handleFn(w, r)
if err != nil {
var metaErr MetaError
if errors.As(err, &metaErr) {
recordDur(w.Header().Get("status"), string(metaErr.Meta.Mode), string(metaErr.Meta.CertVersion))
} else {
recordDur(w.Header().Get("status"), string("NoCommitmentMode"), string("NoCertVersion"))
}
return err
}
// we assume that every route will set the status header
recordDur(w.Header().Get("status"), string(meta.Mode), string(meta.CertVersion))
return err
return nil
}
}

Expand Down Expand Up @@ -155,14 +164,17 @@ func (svr *Server) HandleGet(w http.ResponseWriter, r *http.Request) (commitment
if err != nil {
err = fmt.Errorf("invalid commitment mode: %w", err)
svr.WriteBadRequest(w, err)
return meta, err
return commitments.CommitmentMeta{}, err
}
key := path.Base(r.URL.Path)
comm, err := commitments.StringToDecodedCommitment(key, meta.Mode)
if err != nil {
err = fmt.Errorf("failed to decode commitment from key %v (commitment mode %v): %w", key, meta.Mode, err)
svr.WriteBadRequest(w, err)
return meta, err
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

input, err := svr.router.Get(r.Context(), comm, meta.Mode)
Expand All @@ -173,7 +185,10 @@ func (svr *Server) HandleGet(w http.ResponseWriter, r *http.Request) (commitment
} else {
svr.WriteInternalError(w, err)
}
return meta, err
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

svr.WriteResponse(w, input)
Expand All @@ -189,14 +204,17 @@ func (svr *Server) HandlePut(w http.ResponseWriter, r *http.Request) (commitment
if err != nil {
err = fmt.Errorf("invalid commitment mode: %w", err)
svr.WriteBadRequest(w, err)
return meta, err
return commitments.CommitmentMeta{}, err
}

input, err := io.ReadAll(r.Body)
if err != nil {
err = fmt.Errorf("failed to read request body: %w", err)
svr.WriteBadRequest(w, err)
return meta, err
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

key := path.Base(r.URL.Path)
Expand All @@ -207,22 +225,31 @@ func (svr *Server) HandlePut(w http.ResponseWriter, r *http.Request) (commitment
if err != nil {
err = fmt.Errorf("failed to decode commitment from key %v (commitment mode %v): %w", key, meta.Mode, err)
svr.WriteBadRequest(w, err)
return meta, err
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}
}

commitment, err := svr.router.Put(r.Context(), meta.Mode, comm, input)
if err != nil {
err = fmt.Errorf("put request failed with commitment %v (commitment mode %v): %w", comm, meta.Mode, err)
svr.WriteInternalError(w, err)
return meta, err
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

responseCommit, err := commitments.EncodeCommitment(commitment, meta.Mode)
if err != nil {
err = fmt.Errorf("failed to encode commitment %v (commitment mode %v): %w", commitment, meta.Mode, err)
svr.WriteInternalError(w, err)
return meta, err
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

svr.log.Info(fmt.Sprintf("write commitment: %x\n", comm))
Expand Down
13 changes: 6 additions & 7 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestGetHandler(t *testing.T) {
expectedCode: http.StatusInternalServerError,
expectedBody: "",
expectError: true,
expectedCommitmentMeta: commitments.CommitmentMeta{Mode: commitments.OptimismGeneric, CertVersion: 0},
expectedCommitmentMeta: commitments.CommitmentMeta{},
},
{
name: "Success - OP Keccak256",
Expand All @@ -107,7 +107,7 @@ func TestGetHandler(t *testing.T) {
expectedCode: http.StatusInternalServerError,
expectedBody: "",
expectError: true,
expectedCommitmentMeta: commitments.CommitmentMeta{Mode: commitments.OptimismAltDA, CertVersion: 0},
expectedCommitmentMeta: commitments.CommitmentMeta{},
},
{
name: "Success - OP Alt-DA",
Expand Down Expand Up @@ -217,11 +217,10 @@ func TestPutHandler(t *testing.T) {
mockBehavior: func() {
mockRouter.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("internal error"))
},
expectedCode: http.StatusInternalServerError,
expectedBody: "",
expectError: true,
// certification version is the third byte of the body, in this case it's "m"
expectedCommitmentMeta: commitments.CommitmentMeta{Mode: commitments.OptimismAltDA, CertVersion: 0},
expectedCode: http.StatusInternalServerError,
expectedBody: "",
expectError: true,
expectedCommitmentMeta: commitments.CommitmentMeta{},
},
{
name: "Success OP Mode Alt-DA",
Expand Down
Loading