MCPcopy
hub / github.com/go-kit/kit / DefaultErrorEncoder

Function DefaultErrorEncoder

transport/http/jsonrpc/server.go:183–210  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

181// response error.
182// If the error implements Headerer, the given headers will be set.
183func 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

Callers

nothing calls this directly

Calls 7

SetMethod · 0.65
HeadersMethod · 0.65
ErrorCodeMethod · 0.65
GetMethod · 0.45
ErrorMethod · 0.45
WriteHeaderMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…