DefaultErrorEncoder writes the error to the ResponseWriter, as a json-rpc error response, with an InternalError status code. The Error() string of the error will be used as the response error message. If the error implements ErrorCoder, the provided code will be set on the response error. If the err
(ctx context.Context, err error, w http.ResponseWriter)
| 181 | // response error. |
| 182 | // If the error implements Headerer, the given headers will be set. |
| 183 | func DefaultErrorEncoder(ctx context.Context, err error, w http.ResponseWriter) { |
| 184 | w.Header().Set("Content-Type", ContentType) |
| 185 | if headerer, ok := err.(httptransport.Headerer); ok { |
| 186 | for k := range headerer.Headers() { |
| 187 | w.Header().Set(k, headerer.Headers().Get(k)) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | e := Error{ |
| 192 | Code: InternalError, |
| 193 | Message: err.Error(), |
| 194 | } |
| 195 | if sc, ok := err.(ErrorCoder); ok { |
| 196 | e.Code = sc.ErrorCode() |
| 197 | } |
| 198 | |
| 199 | w.WriteHeader(http.StatusOK) |
| 200 | |
| 201 | var requestID *RequestID |
| 202 | if v := ctx.Value(requestIDKey); v != nil { |
| 203 | requestID = v.(*RequestID) |
| 204 | } |
| 205 | _ = json.NewEncoder(w).Encode(Response{ |
| 206 | ID: requestID, |
| 207 | JSONRPC: Version, |
| 208 | Error: &e, |
| 209 | }) |
| 210 | } |
| 211 | |
| 212 | // ErrorCoder is checked by DefaultErrorEncoder. If an error value implements |
| 213 | // ErrorCoder, the integer result of ErrorCode() will be used as the JSONRPC |