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)
| 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 |
| 129 | func (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 | |
| 151 | func (i *responsesInterceptionBase) requestOptions(respCopy *responseCopier) []option.RequestOption { |
| 152 | opts := []option.RequestOption{ |
no test coverage detected