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 1 commit
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}
}
72 changes: 37 additions & 35 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,20 @@
}

// WithMetrics is a middleware that records metrics for the route path.
func WithMetrics(handleFn func(http.ResponseWriter, *http.Request) (commitments.CommitmentMeta, HandlerError),
func WithMetrics(handleFn func(http.ResponseWriter, *http.Request) (commitments.CommitmentMeta, error),
m metrics.Metricer) func(http.ResponseWriter, *http.Request) error {
return func(w http.ResponseWriter, r *http.Request) error {
recordDur := m.RecordRPCServerRequest(r.Method)

meta, err := handleFn(w, r)
if err.Err != nil {
if err.Meta != nil {
recordDur(w.Header().Get("status"), string(err.Meta.Mode), string(err.Meta.CertVersion))
if err != nil {
// check if the error is a measured error
if meta, ok := err.(MetaError); ok {

Check failure on line 71 in server/server.go

View workflow job for this annotation

GitHub Actions / Linter

type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
recordDur(w.Header().Get("status"), string(meta.Meta.Mode), string(meta.Meta.CertVersion))
} else {
recordDur(w.Header().Get("status"), string("NoCommitmentModeSet"), string("NoCertVersionSet"))
recordDur(w.Header().Get("status"), string("NoCommitmentMode"), string("NoCertVersion"))
}
return err.Err
return err
}
// we assume that every route will set the status header
recordDur(w.Header().Get("status"), string(meta.Mode), string(meta.CertVersion))
Expand Down Expand Up @@ -158,19 +159,22 @@
// Note: even when an error is returned, the commitment meta is still returned,
// because it is needed for metrics (see the WithMetrics middleware).
// TODO: we should change this behavior and instead use a custom error that contains the commitment meta.
func (svr *Server) HandleGet(w http.ResponseWriter, r *http.Request) (commitments.CommitmentMeta, HandlerError) {
func (svr *Server) HandleGet(w http.ResponseWriter, r *http.Request) (commitments.CommitmentMeta, error) {
meta, err := ReadCommitmentMeta(r)
if err != nil {
err = fmt.Errorf("invalid commitment mode: %w", err)
svr.WriteBadRequest(w, err)
return commitments.CommitmentMeta{}, NewHandlerError(err, &meta)
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 commitments.CommitmentMeta{}, NewHandlerError(err, &meta)
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

input, err := svr.router.Get(r.Context(), comm, meta.Mode)
Expand All @@ -181,30 +185,36 @@
} else {
svr.WriteInternalError(w, err)
}
return commitments.CommitmentMeta{}, NewHandlerError(err, &meta)
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

svr.WriteResponse(w, input)
return meta, HandlerError{}
return meta, nil
}

// HandlePut handles the PUT request for commitments.
// Note: even when an error is returned, the commitment meta is still returned,
// because it is needed for metrics (see the WithMetrics middleware).
// TODO: we should change this behavior and instead use a custom error that contains the commitment meta.
func (svr *Server) HandlePut(w http.ResponseWriter, r *http.Request) (commitments.CommitmentMeta, HandlerError) {
func (svr *Server) HandlePut(w http.ResponseWriter, r *http.Request) (commitments.CommitmentMeta, error) {
meta, err := ReadCommitmentMeta(r)
if err != nil {
err = fmt.Errorf("invalid commitment mode: %w", err)
svr.WriteBadRequest(w, err)
return commitments.CommitmentMeta{}, NewHandlerError(err, &meta)
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 commitments.CommitmentMeta{}, NewHandlerError(err, &meta)
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

key := path.Base(r.URL.Path)
Expand All @@ -215,28 +225,37 @@
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 commitments.CommitmentMeta{}, NewHandlerError(err, &meta)
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 commitments.CommitmentMeta{}, NewHandlerError(err, &meta)
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 commitments.CommitmentMeta{}, NewHandlerError(err, &meta)
return commitments.CommitmentMeta{}, MetaError{
Err: err,
Meta: meta,
}
}

svr.log.Info(fmt.Sprintf("write commitment: %x\n", comm))
// write out encoded commitment
svr.WriteResponse(w, responseCommit)
return meta, HandlerError{}
return meta, nil
}

func (svr *Server) WriteResponse(w http.ResponseWriter, data []byte) {
Expand Down Expand Up @@ -371,20 +390,3 @@

return nil, fmt.Errorf("store not found")
}

type HandlerError struct {
Err error
Meta *commitments.CommitmentMeta
}

func (e *HandlerError) Error() string {
return fmt.Sprintf("HandlerError: %s", e.Err.Error())
}

// Helper function to create a new HandlerError
func NewHandlerError(err error, meta *commitments.CommitmentMeta) HandlerError {
return HandlerError{
Err: err,
Meta: meta,
}
}
8 changes: 4 additions & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ func TestGetHandler(t *testing.T) {

meta, err := server.HandleGet(rec, req)
if tt.expectError {
require.Error(t, err.Err)
require.Error(t, err)
} else {
require.NoError(t, err.Err)
require.NoError(t, err)
}

require.Equal(t, tt.expectedCode, rec.Code)
Expand Down Expand Up @@ -269,9 +269,9 @@ func TestPutHandler(t *testing.T) {

meta, err := server.HandlePut(rec, req)
if tt.expectError {
require.Error(t, err.Err)
require.Error(t, err)
} else {
require.NoError(t, err.Err)
require.NoError(t, err)
}
require.Equal(t, tt.expectedCode, rec.Code)
if !tt.expectError {
Expand Down
Loading