MCPcopy Create free account
hub / github.com/coder/aibridge / sendCustomErr

Method sendCustomErr

intercept/responses/base.go:129–149  ·  view source on GitHub ↗

sendCustomErr sends custom responses.Error error to the client it should only be called before any data is sent back to the client

(ctx context.Context, w http.ResponseWriter, code int, err error)

Source from the content-addressed store, hash-verified

127// sendCustomErr sends custom responses.Error error to the client
128// it should only be called before any data is sent back to the client
129func (i *responsesInterceptionBase) sendCustomErr(ctx context.Context, w http.ResponseWriter, code int, err error) {
130 // Same JSON shape as responses.Error but using a plain struct because
131 // responses.Error embeds *http.Request whose GetBody func field
132 // is not JSON-marshalable (SA1026).
133 respErr := struct {
134 Code string `json:"code"`
135 Message string `json:"message"`
136 }{
137 Code: strconv.Itoa(code),
138 Message: err.Error(),
139 }
140 if b, err := json.Marshal(respErr); err != nil {
141 i.logger.Warn(ctx, "failed to marshal custom error: ", slog.Error(err))
142 } else {
143 w.Header().Set("Content-Type", "application/json")
144 w.WriteHeader(code)
145 if _, err := w.Write(b); err != nil {
146 i.logger.Warn(ctx, "failed to send custom error: ", slog.Error(err))
147 }
148 }
149}
150
151func (i *responsesInterceptionBase) requestOptions(respCopy *responseCopier) []option.RequestOption {
152 opts := []option.RequestOption{

Callers 3

validateRequestMethod · 0.95
ProcessRequestMethod · 0.80
ProcessRequestMethod · 0.80

Calls 4

HeaderMethod · 0.80
ErrorMethod · 0.45
WriteHeaderMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected