NewJSONErrorResponse builds an *http.Response with a JSON body and optional Retry-After header. Used to synthesize bridge-side error responses (e.g. key-pool exhaustion, marshaling fallbacks). Retry-After is set to whole seconds (rounded up) when retryAfter is positive, and omitted otherwise.
(status int, retryAfter time.Duration, body []byte)
| 16 | // fallbacks). Retry-After is set to whole seconds (rounded up) |
| 17 | // when retryAfter is positive, and omitted otherwise. |
| 18 | func NewJSONErrorResponse(status int, retryAfter time.Duration, body []byte) *http.Response { |
| 19 | h := http.Header{} |
| 20 | h.Set("Content-Type", "application/json") |
| 21 | if retryAfter > 0 { |
| 22 | h.Set("Retry-After", strconv.Itoa(int(math.Ceil(retryAfter.Seconds())))) |
| 23 | } |
| 24 | return &http.Response{ |
| 25 | Status: fmt.Sprintf("%d %s", status, http.StatusText(status)), |
| 26 | StatusCode: status, |
| 27 | Proto: "HTTP/1.1", |
| 28 | ProtoMajor: 1, |
| 29 | ProtoMinor: 1, |
| 30 | Header: h, |
| 31 | Body: io.NopCloser(bytes.NewReader(body)), |
| 32 | ContentLength: int64(len(body)), |
| 33 | } |
| 34 | } |