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

Add error HTTP status codes and fix error json marshal #496

Merged
merged 3 commits into from
Sep 20, 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
8 changes: 7 additions & 1 deletion signature-aggregator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ The successful `HTTP 200` response format is
}
```

Unsuccessful responses will include an explanatory `application/json` encoded message in the body of the response.
Unsuccessful responses will include an explanatory `application/json` encoded `error` message in the body of the response along with an appropriate `4xx` or `5xx` status code for user input errors or server side errors respectively e.g.:

```json
{
"error": "Could not decode message"
}
```

## Sample workflow
If you want to manually test a locally running service pointed to the Fuji testnet you can do so with the following steps.
Expand Down
31 changes: 21 additions & 10 deletions signature-aggregator/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type AggregateSignatureResponse struct {
SignedMessage string `json:"signed-message"`
}

type AggregateSignatureErrorResponse struct {
Error string `json:"error"`
}

func HandleAggregateSignaturesByRawMsgRequest(
logger logging.Logger,
metrics *metrics.SignatureAggregatorMetrics,
Expand All @@ -61,18 +65,24 @@ func HandleAggregateSignaturesByRawMsgRequest(
func writeJSONError(
logger logging.Logger,
w http.ResponseWriter,
httpStatusCode int,
errorMsg string,
) {
resp, err := json.Marshal(struct{ error string }{error: errorMsg})
resp, err := json.Marshal(
AggregateSignatureErrorResponse{
Error: errorMsg,
},
)
if err != nil {
msg := "Error marshalling JSON error response"
logger.Error(msg, zap.Error(err))
resp = []byte(msg)
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httpStatusCode)

w.Write(resp)
_, err = w.Write(resp)
if err != nil {
logger.Error("Error writing error response", zap.Error(err))
}
Expand All @@ -92,7 +102,7 @@ func signatureAggregationAPIHandler(
if err != nil {
msg := "Could not decode request body"
logger.Warn(msg, zap.Error(err))
writeJSONError(logger, w, msg)
writeJSONError(logger, w, http.StatusBadRequest, msg)
return
}
var decodedMessage []byte
Expand All @@ -106,14 +116,14 @@ func signatureAggregationAPIHandler(
zap.String("msg", req.Message),
zap.Error(err),
)
writeJSONError(logger, w, msg)
writeJSONError(logger, w, http.StatusBadRequest, msg)
return
}
message, err := types.UnpackWarpMessage(decodedMessage)
if err != nil {
msg := "Error unpacking warp message"
logger.Warn(msg, zap.Error(err))
writeJSONError(logger, w, msg)
writeJSONError(logger, w, http.StatusBadRequest, msg)
return
}

Expand All @@ -127,14 +137,15 @@ func signatureAggregationAPIHandler(
zap.String("justification", req.Justification),
zap.Error(err),
)
writeJSONError(logger, w, msg)
writeJSONError(logger, w, http.StatusBadRequest, msg)
return
}

if utils.IsEmptyOrZeroes(message.Bytes()) && utils.IsEmptyOrZeroes(justification) {
writeJSONError(
logger,
w,
http.StatusBadRequest,
"Must provide either message or justification",
)
return
Expand All @@ -146,7 +157,7 @@ func signatureAggregationAPIHandler(
} else if req.QuorumPercentage > 100 {
msg := "Invalid quorum number"
logger.Warn(msg, zap.Uint64("quorum-num", req.QuorumPercentage))
writeJSONError(logger, w, msg)
writeJSONError(logger, w, http.StatusBadRequest, msg)
return
}
var signingSubnetID ids.ID
Expand All @@ -161,7 +172,7 @@ func signatureAggregationAPIHandler(
zap.Error(err),
zap.String("input", req.SigningSubnetID),
)
writeJSONError(logger, w, msg)
writeJSONError(logger, w, http.StatusBadRequest, msg)
return
}
}
Expand All @@ -175,7 +186,7 @@ func signatureAggregationAPIHandler(
if err != nil {
msg := "Failed to aggregate signatures"
logger.Warn(msg, zap.Error(err))
writeJSONError(logger, w, msg)
writeJSONError(logger, w, http.StatusInternalServerError, msg)
return
}
resp, err := json.Marshal(
Expand All @@ -189,7 +200,7 @@ func signatureAggregationAPIHandler(
if err != nil {
msg := "Failed to marshal response"
logger.Error(msg, zap.Error(err))
writeJSONError(logger, w, msg)
writeJSONError(logger, w, http.StatusInternalServerError, msg)
return
}
w.Header().Set("Content-Type", "application/json")
Expand Down