MCPcopy Index your code
hub / github.com/tailscale/tailscale / WriteHTTPError

Function WriteHTTPError

tsweb/tsweb.go:894–920  ·  view source on GitHub ↗

WriteHTTPError is the default error response formatter.

(w http.ResponseWriter, r *http.Request, e HTTPError)

Source from the content-addressed store, hash-verified

892
893// WriteHTTPError is the default error response formatter.
894func WriteHTTPError(w http.ResponseWriter, r *http.Request, e HTTPError) {
895 // Don't write a response if we've hit a cancellation/abort.
896 if r.Context().Err() != nil || errors.Is(e.Err, http.ErrAbortHandler) {
897 return
898 }
899
900 // Default headers set by http.Error.
901 h := w.Header()
902 h.Set("Content-Type", "text/plain; charset=utf-8")
903 h.Set("X-Content-Type-Options", "nosniff")
904
905 // Custom headers from the error.
906 maps.Copy(h, e.Header)
907
908 // Write the msg back to the user.
909 w.WriteHeader(e.Code)
910 fmt.Fprint(w, e.Msg)
911
912 // If it's a plaintext message, add line breaks and RequestID.
913 if strings.HasPrefix(h.Get("Content-Type"), "text/plain") {
914 io.WriteString(w, "\n")
915 if id := RequestIDFromContext(r.Context()); id != "" {
916 io.WriteString(w, id.String())
917 io.WriteString(w, "\n")
918 }
919 }
920}
921
922// HTTPError is an error with embedded HTTP response information.
923//

Callers 1

handleJSONFunction · 0.92

Calls 10

RequestIDFromContextFunction · 0.85
WriteStringMethod · 0.80
ContextMethod · 0.65
SetMethod · 0.65
GetMethod · 0.65
StringMethod · 0.65
ErrMethod · 0.45
IsMethod · 0.45
HeaderMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…