MCPcopy Index your code
hub / github.com/coder/coder / sendCustomErr

Method sendCustomErr

aibridge/intercept/responses/base.go:192–212  ·  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

190// sendCustomErr sends custom responses.Error error to the client
191// it should only be called before any data is sent back to the client
192func (i *responsesInterceptionBase) sendCustomErr(ctx context.Context, w http.ResponseWriter, code int, err error) {
193 // Same JSON shape as responses.Error but using a plain struct because
194 // responses.Error embeds *http.Request whose GetBody func field
195 // is not JSON-marshalable (SA1026).
196 respErr := struct {
197 Code string `json:"code"`
198 Message string `json:"message"`
199 }{
200 Code: strconv.Itoa(code),
201 Message: err.Error(),
202 }
203 if b, err := json.Marshal(respErr); err != nil {
204 i.logger.Warn(ctx, "failed to marshal custom error: ", slog.Error(err))
205 } else {
206 w.Header().Set("Content-Type", "application/json")
207 w.WriteHeader(code)
208 if _, err := w.Write(b); err != nil {
209 i.logger.Warn(ctx, "failed to send custom error: ", slog.Error(err))
210 }
211 }
212}
213
214func (i *responsesInterceptionBase) requestOptions(respCopy *responseCopier) []option.RequestOption {
215 opts := []option.RequestOption{

Callers 3

validateRequestMethod · 0.95
ProcessRequestMethod · 0.80
ProcessRequestMethod · 0.80

Calls 6

SetMethod · 0.65
WriteMethod · 0.65
ErrorMethod · 0.45
MarshalMethod · 0.45
HeaderMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected