From b26674539c0ec07cb17c419646c7f6c45fc7f1c7 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sat, 2 Feb 2019 13:20:53 -0800 Subject: [PATCH] Implement new ErrorResponse --- PENDING.md | 1 + client/utils/rest.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/PENDING.md b/PENDING.md index 042b9dd1163a..bb5e7a4c6b21 100644 --- a/PENDING.md +++ b/PENDING.md @@ -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 diff --git a/client/utils/rest.go b/client/utils/rest.go index 2926a555133d..098a21298a4c 100644 --- a/client/utils/rest.go +++ b/client/utils/rest.go @@ -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"` + 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))) } // WriteSimulationResponse prepares and writes an HTTP @@ -353,6 +365,7 @@ func WriteGenerateStdTxResponse( return } + w.Header().Set("Content-Type", "application/json") w.Write(output) return }