Skip to content

fix(redirect): throw TypeError instead of deprecating on missing/invalid url - #7103

Closed
zakiscoding wants to merge 1 commit into
expressjs:masterfrom
zakiscoding:feat/issue-6941-redirect-undefined-fix
Closed

fix(redirect): throw TypeError instead of deprecating on missing/invalid url#7103
zakiscoding wants to merge 1 commit into
expressjs:masterfrom
zakiscoding:feat/issue-6941-redirect-undefined-fix

Conversation

@zakiscoding

Copy link
Copy Markdown

Fixes#6941

Instead of sending a malformed Location header with 'undefined' or deprecated values, res.redirect() now throws a TypeError immediately when called without a valid url argument or with invalid argument types.

This is a breaking change for v6.x as indicated by the 'deprecate' label on the issue.

BREAKING CHANGE: res.redirect() now throws TypeError instead of deprecating:

  • res.redirect() with no arguments
  • res.redirect(undefined)
  • res.redirect(url) where url is not a string
  • res.redirect(status) where status is not a number

…lid url
Fixesexpressjs#6941
Instead of sending a malformed Location header with 'undefined' or deprecated values, res.redirect() now throws a TypeError immediately when called without a valid url argument or with invalid argument types.
This is a breaking change for v6.x as indicated by the 'deprecate' label on the issue.
BREAKING CHANGE: res.redirect() now throws TypeError instead of deprecating:
- res.redirect() with no arguments
- res.redirect(undefined)
- res.redirect(url) where url is not a string
- res.redirect(status) where status is not a number

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Express’s res.redirect() argument handling to immediately throw TypeErrors for missing or invalid inputs, preventing malformed Location headers like Location: undefined (Issue #6941).

Changes:

  • Replace deprecation warnings in res.redirect() with immediate TypeError throws for invalid/missing arguments.
  • Add more specific error messages for invalid url and status argument types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment threadlib/response.js
Comment on lines 823 to 825
if (!address) {
deprecate('Provide a url argument');
throw new TypeError('Provide a url argument to res.redirect()');
}

CopilotAIMar 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!address) will treat empty strings and other falsy-but-provided values (e.g. 0 when called as res.redirect(301, 0)) as “missing url” and throw the wrong TypeError message. Consider checking explicitly for missing values (e.g. arguments.length === 0 / address == null) and, if desired, handling '' as its own invalid-url case with a clearer error.

Copilot uses AI. Check for mistakes.
Comment threadlib/response.js
Comment on lines 827 to +832
if (typeof address !== 'string') {
deprecate('Url must be a string');
throw new TypeError('The "url" argument must be a string');
}

if (typeof status !== 'number') {
deprecate('Status must be a number');
throw new TypeError('The "status" argument must be a number');

CopilotAIMar 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new redirect TypeError messages use a different format (e.g. capitalized “The "url" argument…”) than other argument validation errors in this file (e.g. path must be a string to res.sendFile). Aligning the wording/format makes errors more consistent and easier to search/maintain across the codebase.

Copilot uses AI. Check for mistakes.
Comment threadlib/response.js
Comment on lines 823 to 825
if (!address) {
deprecate('Provide a url argument');
throw new TypeError('Provide a url argument to res.redirect()');
}

CopilotAIMar 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change introduces new throwing behavior for invalid/missing redirect arguments, but the existing redirect test suite (test/res.redirect.js) doesn’t appear to assert these error cases. Adding tests for the new TypeError conditions (missing url, non-string url, non-number status) would prevent regressions and clarify the breaking-change contract.

Copilot uses AI. Check for mistakes.
@krzysdz

Copy link
Copy Markdown
Contributor

Duplicate of#6404

@krzysdzkrzysdz marked this as a duplicate of #6404Mar 12, 2026
@krzysdzkrzysdz closed this Mar 12, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

res.redirect(undefined) sends invalid Location: undefined header

3 participants

@zakiscoding@krzysdz