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)
| 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 |
| 192 | func (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 | |
| 214 | func (i *responsesInterceptionBase) requestOptions(respCopy *responseCopier) []option.RequestOption { |
| 215 | opts := []option.RequestOption{ |