MCPcopy Create free account
hub / github.com/prometheus/prometheus / respondError

Method respondError

web/api/v1/api.go:2200–2230  ·  view source on GitHub ↗
(w http.ResponseWriter, apiErr *apiError, data any)

Source from the content-addressed store, hash-verified

2198}
2199
2200func (api *API) respondError(w http.ResponseWriter, apiErr *apiError, data any) {
2201 json := jsoniter.ConfigCompatibleWithStandardLibrary
2202 b, err := json.Marshal(&Response{
2203 Status: statusError,
2204 ErrorType: apiErr.typ.str,
2205 Error: apiErr.err.Error(),
2206 Data: data,
2207 })
2208 if err != nil {
2209 api.logger.Error("error marshaling json response", "err", err)
2210 http.Error(w, err.Error(), http.StatusInternalServerError)
2211 return
2212 }
2213
2214 var code int
2215 if api.overrideErrorCode != nil {
2216 if newCode, override := api.overrideErrorCode(apiErr.typ.num, apiErr.err); override {
2217 code = newCode
2218 } else {
2219 code = getDefaultErrorCode(apiErr.typ)
2220 }
2221 } else {
2222 code = getDefaultErrorCode(apiErr.typ)
2223 }
2224
2225 w.Header().Set("Content-Type", "application/json")
2226 w.WriteHeader(code)
2227 if n, err := w.Write(b); err != nil {
2228 api.logger.Error("error writing response", "bytesWritten", n, "err", err)
2229 }
2230}
2231
2232func getDefaultErrorCode(errType errorType) int {
2233 switch errType {

Callers 9

RegisterMethod · 0.95
serveWALReplayStatusMethod · 0.95
respondMethod · 0.95
newSearchRequestMethod · 0.95
searchMetricNamesMethod · 0.95
searchLabelValuesMethod · 0.95
TestRespondErrorFunction · 0.95
streamSearchResultsFunction · 0.80

Calls 7

getDefaultErrorCodeFunction · 0.85
HeaderMethod · 0.80
WriteHeaderMethod · 0.80
ErrorMethod · 0.65
SetMethod · 0.65
WriteMethod · 0.65
MarshalMethod · 0.45

Tested by 1

TestRespondErrorFunction · 0.76