{"record":{"id":"0f003aa98ba5977c","repo":"denoland/deno","slug":"invalid-redirect-status-code-status","errorCode":null,"errorMessage":"Invalid redirect status code: ${status}","messagePattern":"Invalid redirect status code: (.+?)","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/fetch/23_response.js","lineNumber":680,"sourceCode":"    initializeResponseBase(response, inner, \"immutable\");\n    maybeSetServeNativeFromInner(response);\n    return response;\n  }\n\n  /**\n   * @param {string} url\n   * @param {number} status\n   * @returns {Response}\n   */\n  static redirect(url, status = 302) {\n    const prefix = \"Failed to execute 'Response.redirect'\";\n    url = webidlConvertersUSVString(url, prefix, \"Argument 1\");\n    status = webidlConvertersUnsignedShort(status, prefix, \"Argument 2\");\n\n    const baseURL = getLocationHref();\n    const parsedURL = new URL(url, baseURL);\n    if (!redirectStatus(status)) {\n      throw new RangeError(`Invalid redirect status code: ${status}`);\n    }\n    const inner = newInnerResponse(status);\n    inner.type = \"default\";\n    ArrayPrototypePush(inner.headerList, [\"Location\", parsedURL.href]);\n    const response = webidl.createBranded(Response);\n    initializeResponseBase(response, inner, \"immutable\");\n    maybeSetServeNativeFromInner(response);\n    return response;\n  }\n\n  /**\n   * @param {any} data\n   * @param {ResponseInit} init\n   * @returns {Response}\n   */\n  static json(data = undefined, init = undefined) {\n    const prefix = \"Failed to execute 'Response.json'\";\n    data = webidlConvertersAny(data);","sourceCodeStart":662,"sourceCodeEnd":698,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fetch/23_response.js#L662-L698","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nResponse.redirect(\"https://example.com/new\", 300); // throws RangeError\n\n// after\nResponse.redirect(\"https://example.com/new\", 302);\n// or method-preserving:\nResponse.redirect(\"https://example.com/new\", 307);","handlingStrategy":"validation","validationCode":"const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);\nif (!REDIRECT_STATUSES.has(status)) status = 302;\nreturn Response.redirect(targetUrl, status);","typeGuard":"const REDIRECTS = new Set([301, 302, 303, 307, 308]);\nfunction isRedirectStatus(s: number): s is 301 | 302 | 303 | 307 | 308 {\n  return REDIRECTS.has(s);\n}","tryCatchPattern":"try {\n  return Response.redirect(url, status);\n} catch (err) {\n  if (err instanceof RangeError && err.message.includes(\"redirect status\")) {\n    return Response.redirect(url, 302); // safe default\n  }\n  throw err;\n}","preventionTips":["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"],"tags":["fetch","response","redirect","status-code"],"backgroundTag":"invalid-redirect-status","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}