MCPcopy
hub / github.com/pocketbase/pocketbase / ErrorHandler

Function ErrorHandler

tools/router/router.go:160–183  ·  view source on GitHub ↗
(resp http.ResponseWriter, req *http.Request, err error)

Source from the content-addressed store, hash-verified

158}
159
160func ErrorHandler(resp http.ResponseWriter, req *http.Request, err error) {
161 if err == nil {
162 return
163 }
164
165 if ok, _ := getWritten(resp); ok {
166 return // a response was already written (aka. already handled)
167 }
168
169 header := resp.Header()
170 if header.Get("Content-Type") == "" {
171 header.Set("Content-Type", "application/json")
172 }
173
174 apiErr := ToApiError(err)
175
176 resp.WriteHeader(apiErr.Status)
177
178 if req.Method != http.MethodHead {
179 if jsonErr := json.NewEncoder(resp).Encode(apiErr); jsonErr != nil {
180 log.Println(jsonErr) // truly rare case, log to stderr only for dev purposes
181 }
182 }
183}
184
185// -------------------------------------------------------------------
186

Callers 1

loadMuxMethod · 0.85

Calls 6

getWrittenFunction · 0.85
EncodeMethod · 0.80
ToApiErrorFunction · 0.70
GetMethod · 0.65
SetMethod · 0.65
WriteHeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…