I'm building a JSON API in Go and I'd like to return error responses as json.
Example response:
{
"error": "Invalid request syntax"
}
I thought that I could create a wrapper struct that implements the error interface, and then use Go's json marshaler as a clean way to get the json representation of the error:
type JsonErr struct {
Err error `json:"error"`
}
func (t JsonErr) Error() string {
return t.Err.Error()
}
This will just marshal a JsonErr as {"error":{}}
, is there a way of using the default Go json marshaler to encode this struct, or do I need to write a quick custom MarshalJson for JsonErr structs?