mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-06 20:13:59 -05:00
**What type of PR is this?** Tooling **What does this PR do? Why is it needed?** Renames `httperror` analyzer to `httpwriter` and extends it to the following functions: - `WriteError` - `WriteJson` - `WriteSsz` _**NOTE: The PR is currently red because the fix in https://github.com/OffchainLabs/prysm/pull/16175 must be merged first**_ **Acknowledgements** - [x] I have read [CONTRIBUTING.md](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md). - [x] I have included a uniquely named [changelog fragment file](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md#maintaining-changelogmd). - [x] I have added a description with sufficient context for reviewers to understand this PR. - [x] I have tested that my changes work as expected and I added a testing plan to the PR description (if applicable).
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package httputil
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/OffchainLabs/prysm/v7/api"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type HasStatusCode interface {
|
|
StatusCode() int
|
|
}
|
|
|
|
// DefaultJsonError is a JSON representation of a simple error value, containing only a message and an error code.
|
|
type DefaultJsonError struct {
|
|
Message string `json:"message"`
|
|
Code int `json:"code"`
|
|
}
|
|
|
|
func (e *DefaultJsonError) StatusCode() int {
|
|
return e.Code
|
|
}
|
|
|
|
func (e *DefaultJsonError) Error() string {
|
|
return fmt.Sprintf("HTTP request unsuccessful (%d: %s)", e.Code, e.Message)
|
|
}
|
|
|
|
// WriteJson writes the response message in JSON format.
|
|
func WriteJson(w http.ResponseWriter, v any) {
|
|
w.Header().Set("Content-Type", api.JsonMediaType)
|
|
w.WriteHeader(http.StatusOK)
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
log.WithError(err).Error("Could not write response message")
|
|
}
|
|
}
|
|
|
|
// WriteSsz writes the response message in ssz format
|
|
func WriteSsz(w http.ResponseWriter, respSsz []byte) {
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(respSsz)))
|
|
w.Header().Set("Content-Type", api.OctetStreamMediaType)
|
|
w.WriteHeader(http.StatusOK)
|
|
if _, err := io.Copy(w, io.NopCloser(bytes.NewReader(respSsz))); err != nil {
|
|
log.WithError(err).Error("Could not write response message")
|
|
}
|
|
}
|
|
|
|
// WriteError writes the error by manipulating headers and the body of the final response.
|
|
func WriteError(w http.ResponseWriter, errJson HasStatusCode) {
|
|
j, err := json.Marshal(errJson)
|
|
if err != nil {
|
|
log.WithError(err).Error("Could not marshal error message")
|
|
return
|
|
}
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(j)))
|
|
w.Header().Set("Content-Type", api.JsonMediaType)
|
|
w.WriteHeader(errJson.StatusCode())
|
|
if _, err := io.Copy(w, io.NopCloser(bytes.NewReader(j))); err != nil {
|
|
log.WithError(err).Error("Could not write error message")
|
|
}
|
|
}
|