(upstream)
| 320 | } |
| 321 | |
| 322 | async function upstreamErrorResponse(upstream) { |
| 323 | const upstreamStatus = upstream?.status || 502; |
| 324 | const status = |
| 325 | upstreamStatus >= 400 && upstreamStatus < 500 ? upstreamStatus : 502; |
| 326 | const detail = upstream ? await readResponsePreview(upstream, 4096) : ""; |
| 327 | |
| 328 | return new Response( |
| 329 | renderErrorPage( |
| 330 | `Upstream error ${upstreamStatus}`, |
| 331 | detail || |
| 332 | "The Pollinations text API did not return generated HTML for this request.", |
| 333 | ), |
| 334 | { |
| 335 | status, |
| 336 | headers: { |
| 337 | "Content-Type": "text/html; charset=utf-8", |
| 338 | "Cache-Control": "no-store", |
| 339 | ...getCorsHeaders(), |
| 340 | }, |
| 341 | }, |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | async function readResponsePreview(response, maxChars) { |
| 346 | if (!response.body) return ""; |
no test coverage detected