Skip to content

Commit

Permalink
JSON error handling #22
Browse files Browse the repository at this point in the history
  • Loading branch information
szynwelski committed Jul 13, 2023
1 parent b7f5931 commit 9dc5bb1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
10 changes: 5 additions & 5 deletions x/sequencer/api/data_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func (h dataItemHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Parse DataItem from request body
err := msg.DataItem.UnmarshalFromReader(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse data item: %s", err.Error()), http.StatusBadRequest)
BadRequestError(w, err, "parse data item error")
return
}

// Wrap message with Cosmos transaction, validate and broadcast transaction
response, err := types.BroadcastDataItem(h.ctx, msg)
if err != nil {
http.Error(w, "failed to broadcast transaction", http.StatusInternalServerError)
InternalServerErrorString(w, "failed to broadcast transaction", "broadcast transaction error")
return
}
if response.Code != 0 {
http.Error(w, "failed to broadcast transaction: " + response.RawLog, http.StatusInternalServerError)
InternalServerErrorString(w, response.RawLog, "broadcast transaction error")
return
}

Expand All @@ -51,13 +51,13 @@ func (h dataItemHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
DataItemId: msg.DataItem.Id.Base64(),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
InternalServerError(w, err, "response encoding error")
return
}

_, err = fmt.Fprintf(w, "%s", jsonResponse)
if err != nil {
http.Error(w, "failed to write response", http.StatusInternalServerError)
InternalServerError(w, err, "response writing error")
return
}
w.Header().Set("Content-Type", "application/json")
Expand Down
54 changes: 54 additions & 0 deletions x/sequencer/api/json_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package api

import (
"encoding/json"
"net/http"
)

type httpStatus struct {
Code int `json:"code"`
Text string `json:"text"`
}

type httpJsonError struct {
ErrorType string `json:"type"`
ErrorMessage string `json:"message"`
Status httpStatus `json:"status"`
}

// Writes a bad request error in the form of JSON to the HTTP response
func BadRequestError(w http.ResponseWriter, err error, errorType string) {
jsonError := createJsonError(err.Error(), errorType, http.StatusBadRequest)
writeError(w, jsonError)
}

// Writes a internal server error in the form of JSON to the HTTP response (takes an error as a string)
func InternalServerErrorString(w http.ResponseWriter, err string, errorType string) {
jsonError := createJsonError(err, errorType, http.StatusBadRequest)
writeError(w, jsonError)
}

// Writes a internal server error in the form of JSON to the HTTP response
func InternalServerError(w http.ResponseWriter, err error, errorType string) {
InternalServerErrorString(w, err.Error(), errorType)
}

func createJsonError(err string, errorType string, code int) httpJsonError {
return httpJsonError{
ErrorType: errorType,
ErrorMessage: err,
Status: httpStatus{
Code: code,
Text: http.StatusText(code),
},
}
}

func writeError(w http.ResponseWriter, jsonError httpJsonError) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(jsonError.Status.Code)
if err := json.NewEncoder(w).Encode(jsonError); err != nil {
panic(err)
}
}
10 changes: 5 additions & 5 deletions x/sequencer/api/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,32 @@ func (h nonceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var request NonceRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
BadRequestError(w, err, "request decoding error")
return
}

err = h.validate.Struct(request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
BadRequestError(w, err, "invalid request")
return
}

publicKey, err := getPublicKey(request)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
BadRequestError(w, err, "public key problem")
return
}

response := getAddressWithNonce(h.ctx, publicKey)
jsonResponse, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
InternalServerError(w, err, "response encoding error")
return
}

_, err = fmt.Fprintf(w, "%s", jsonResponse)
if err != nil {
http.Error(w, "failed to write response", http.StatusInternalServerError)
InternalServerError(w, err, "response writing error")
return
}
w.Header().Set("Content-Type", "application/json")
Expand Down

0 comments on commit 9dc5bb1

Please sign in to comment.