Skip to content

Commit

Permalink
feat: support proper error handling as per EIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kamikazechaser committed Dec 16, 2024
1 parent b5462b3 commit e367eb1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions internal/api/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func (a *API) ccipHandler(w http.ResponseWriter, req bunrouter.Request) error {
)

if err := resolveFunc.DecodeArgs(w3.B(r.Data), &encodedName, &innerData); err != nil {
return err
return httputil.JSON(w, http.StatusBadRequest, CCIPErrResponse{
Message: "Could not decode data.",
})
}

ensName := ens.DecodeENSName(encodedName)
Expand All @@ -61,18 +63,29 @@ func (a *API) ccipHandler(w http.ResponseWriter, req bunrouter.Request) error {

value, err := decodeInnerData(hexutil.Encode(innerData))
if err != nil {
return err
if err == ErrUnsupportedFunction {
return httputil.JSON(w, http.StatusBadRequest, CCIPErrResponse{
Message: "Unsupported function.",
})
}

return httputil.JSON(w, http.StatusBadRequest, CCIPErrResponse{
Message: "Bad data.",
})
}
a.logg.Debug("inner data return value", "value", value.Hex())

encodedNameHash, err := ens.NameHash(ensName)
if err != nil {
return err
return httputil.JSON(w, http.StatusBadRequest, CCIPErrResponse{
Message: "Could not determine encoded name hash.",
})
}

if !bytes.Equal(encodedNameHash[:], value.Bytes()) {
a.logg.Error("name validation failed", "name", ensName)
return ErrNameValidation
return httputil.JSON(w, http.StatusBadRequest, CCIPErrResponse{
Message: "Could not validate name.",
})
}

// TODO: Offchain lookup is performed here
Expand All @@ -85,7 +98,9 @@ func (a *API) ccipHandler(w http.ResponseWriter, req bunrouter.Request) error {
w3.A(testResolvedAddress),
)
if err != nil {
return err
return httputil.JSON(w, http.StatusInternalServerError, CCIPErrResponse{
Message: "Could not sign payload.",
})
}
a.logg.Debug("signed payload", "payload", payload)

Expand Down
2 changes: 1 addition & 1 deletion internal/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type (
}

CCIPErrResponse struct {
Data string `json:"message"`
Message string `json:"message"`
}

RegisterRequest struct {
Expand Down

0 comments on commit e367eb1

Please sign in to comment.