denoland/deno · error · RangeError
Invalid redirect status code
Error message
Invalid redirect status code: ${status} What it means
Response.redirect(url, status) requires status to be a genuine redirect status: 301, 302, 303, 307, or 308. The static method parses the url, checks redirectStatus(status), and throws RangeError 'Invalid redirect status code: N' for anything else — including 300 (Multiple Choices, not usable with Location semantics), 304, 200, or 201.
Solutions
- Use one of 301, 302, 303, 307, 308 (302 default; 307 preserves method)
- Restrict user-configurable redirect codes to that allow-list
- For non-redirect responses use new Response(body, { status, headers: { Location: url } }) instead
Example fix
// before
Response.redirect("https://example.com/new", 300); // throws RangeError
// after
Response.redirect("https://example.com/new", 302);
// or method-preserving:
Response.redirect("https://example.com/new", 307); Defensive patterns
Strategy: validation
Validate before calling
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]); if (!REDIRECT_STATUSES.has(status)) status = 302; return Response.redirect(targetUrl, status);
Type guard
const REDIRECTS = new Set([301, 302, 303, 307, 308]);
function isRedirectStatus(s: number): s is 301 | 302 | 303 | 307 | 308 {
return REDIRECTS.has(s);
} Try / catch
try {
return Response.redirect(url, status);
} catch (err) {
if (err instanceof RangeError && err.message.includes("redirect status")) {
return Response.redirect(url, 302); // safe default
}
throw err;
} Prevention
- Restrict configurable redirect codes to 301/302/303/307/308
- Use 307/308 when the original method must be preserved on redirect
- Don't reach for 300/304 with Location — they are not redirect() statuses
When it happens
Trigger: Response.redirect("https://example.com/new", 300); Response.redirect(url, 200); a status passed from config that only 'looks like' a redirect (3xx such as 304).
Common situations: Assuming any 3xx works with Location; making the redirect status configurable without restricting it; mixing up temporary (307/302) and permanent (301/308) semantics and guessing a wrong number like 305.
Related errors
- Response with null body status cannot have body
- The status provided ( ) is not equal to 101 and outside the…
- Body is unusable
- Cannot read url: request closed
- Invalid status text
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/0f003aa98ba5977c.
Report an issue: GitHub.
Appendix: source
Thrown at ext/fetch/23_response.js:680
initializeResponseBase(response, inner, "immutable");
maybeSetServeNativeFromInner(response);
return response;
}
/**
* @param {string} url
* @param {number} status
* @returns {Response}
*/
static redirect(url, status = 302) {
const prefix = "Failed to execute 'Response.redirect'";
url = webidlConvertersUSVString(url, prefix, "Argument 1");
status = webidlConvertersUnsignedShort(status, prefix, "Argument 2");
const baseURL = getLocationHref();
const parsedURL = new URL(url, baseURL);
if (!redirectStatus(status)) {
throw new RangeError(`Invalid redirect status code: ${status}`);
}
const inner = newInnerResponse(status);
inner.type = "default";
ArrayPrototypePush(inner.headerList, ["Location", parsedURL.href]);
const response = webidl.createBranded(Response);
initializeResponseBase(response, inner, "immutable");
maybeSetServeNativeFromInner(response);
return response;
}
/**
* @param {any} data
* @param {ResponseInit} init
* @returns {Response}
*/
static json(data = undefined, init = undefined) {
const prefix = "Failed to execute 'Response.json'";
data = webidlConvertersAny(data);View on GitHub (pinned to 9ad36f7a2c)