Skip to content

Improve error when redirecting with custom schemes#1002

Merged
florimondmanca merged 1 commit into
encode:masterfrom
jcugat:redirect-custom-scheme
May 28, 2020
Merged

Improve error when redirecting with custom schemes#1002
florimondmanca merged 1 commit into
encode:masterfrom
jcugat:redirect-custom-scheme

Conversation

@jcugat

@jcugat jcugat commented May 27, 2020

Copy link
Copy Markdown
Contributor

Fixes #822

I used Scheme "blah" not supported as the error message instead of URL scheme must be "http" or "https". used in other parts of the code. It makes more sense to show the actual invalid scheme since it's coming from the server and not something that the user specified.

I also tried the same test with allow_redirects=False but unfortunately when the response object is built, it needs a port which we can't derive from the custom scheme. Not sure how to solve this without changing the port definition of Url to allow a None port, ideas?

Comment thread httpx/_client.py
url = URL(location, allow_relative=True)

# Check that we can handle the scheme
if url.scheme and url.scheme not in ("http", "https"):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In these kinds of cases, if it's a single-liner I'd often prefer not to introduce indirection. It's as clear as it can possibly be, and it's not a complicated expression.

@jcugat
jcugat force-pushed the redirect-custom-scheme branch from 9160631 to c57f992 Compare May 27, 2020 19:32

@florimondmanca florimondmanca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Sounds like an about right fix to me, waiting for an overhaul of scheme handling #954 and possibly mounting transports #977.

Happy to have someone else's opinion on this though :-)

@yeraydiazdiaz yeraydiazdiaz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks @jcugat!

@florimondmanca
florimondmanca merged commit 093cb42 into encode:master May 28, 2020
@jcugat
jcugat deleted the redirect-custom-scheme branch May 28, 2020 11:23
@lovelydinosaur lovelydinosaur mentioned this pull request May 29, 2020
Kludex referenced this pull request in pydantic/httpx2 May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyError occurs when redirect to custom scheme(ex. market://)

4 participants