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

R4R: Introduce RESTful error responses #3485

Merged
merged 1 commit into from
Feb 4, 2019
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
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ BREAKING CHANGES
* Gaia REST API (`gaiacli advanced rest-server`)
* [\#3284](https://github.com/cosmos/cosmos-sdk/issues/3284) Rename the `name`
field to `from` in the `base_req` body.
* [\#3485](https://github.com/cosmos/cosmos-sdk/pull/3485) Error responses are now JSON objects.

* Gaia CLI (`gaiacli`)
- [#3399](https://github.com/cosmos/cosmos-sdk/pull/3399) Add `gaiad validate-genesis` command to facilitate checking of genesis files
Expand Down
15 changes: 14 additions & 1 deletion client/utils/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ type GasEstimateResponse struct {
//-----------------------------------------------------------------------------
// Basic HTTP utilities

// ErrorResponse defines the attributes of a JSON error response.
type ErrorResponse struct {
Code int `json:"code,omitempty"`
alessio marked this conversation as resolved.
Show resolved Hide resolved
Message string `json:"message"`
}

// NewErrorResponse creates a new ErrorResponse instance.
func NewErrorResponse(code int, msg string) ErrorResponse {
return ErrorResponse{Code: code, Message: msg}
}

// WriteErrorResponse prepares and writes a HTTP error
// given a status code and an error message.
func WriteErrorResponse(w http.ResponseWriter, status int, err string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write([]byte(err))
w.Write(codec.Cdc.MustMarshalJSON(NewErrorResponse(0, err)))
alessio marked this conversation as resolved.
Show resolved Hide resolved
}

// WriteSimulationResponse prepares and writes an HTTP
Expand Down Expand Up @@ -353,6 +365,7 @@ func WriteGenerateStdTxResponse(
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(output)
return
}