Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

realm screen: Give early error when server URL is an email address. #4181

Merged
merged 3 commits into from
Jul 11, 2020

Conversation

agrawal-d
Copy link
Member

Show an error message when the user tries to enter an email in the
server URL input field, even before trying to connect to the
server.

Fixes: #4058

@agrawal-d agrawal-d requested a review from gnprice July 2, 2020 12:19
@@ -53,6 +54,15 @@ class RealmScreen extends PureComponent<Props, State> {

const { dispatch } = this.props;

if (new WhatwgURL(realm).username !== '') {
Copy link
Contributor

Choose a reason for hiding this comment

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

new WhatwgURL(realm) will throw an error if realm is an invalid URL: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL

Copy link
Contributor

@chrisbobbe chrisbobbe Jul 3, 2020

Choose a reason for hiding this comment

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

I just tested this by hardcoding something wrong for 'realm'. I got this:

Possible Unhandled Promise Rejection (id: 0):
TypeError: Invalid URL: asdf

I don't get a white-screen crash (the error was thrown from a React event handler), but the spinner does go on indefinitely.

I think the solution is to isolate new WhatwgUrl(realm) into its own try/catch block, and, on this error, do a setState similar to this one, with an appropriate error message saying it's an invalid URL.

Then the email can be checked below that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah! Initially, I did add a try-catch for the URL constructor. But then I saw that we already validate the URL in the login button using a regular expression - but I never considered that the form could be submitted by simply tapping the enter key from the keyboard, without needing to touch the button.

this.setState({
progress: false,
error:
'Please enter the server URL, not your email. You can enter your ceredentials in the next screen.',
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: "credentials" (also in messages_en.json)

@agrawal-d
Copy link
Member Author

Thanks for the review. I've made the suggested changes.

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

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

Thanks @agrawal-d ! Comments below.

src/start/RealmScreen.js Outdated Show resolved Hide resolved
Comment on lines 54 to 65

const { dispatch } = this.props;

let parsedRealm;
try {
parsedRealm = new WhatwgURL(realm);
} catch (err) {
this.setState({
progress: false,
error: 'Please enter a valid URL',
});
return;
}

if (parsedRealm.username !== '') {
this.setState({
progress: false,
error:
'Please enter the server URL, not your email. You can enter your credentials in the next screen.',
});
return;
}

try {
const serverSettings: ApiResponseServerSettings = await api.getServerSettings(realm);
dispatch(realmAdd(realm, new ZulipVersion(serverSettings.zulip_version)));
Copy link
Member

Choose a reason for hiding this comment

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

This function gets somewhat difficult to follow with all the stuff going on in it now. I think one reason is that the setState at the top is setting up the start of a story you basically have to read the whole function to see the different endings of.

One thing that would simplify it is to not set progress: true until after the realm-parsing is done and those errors haven't happened. That way those don't need to say progress: false, and the whole question of the progress state is limited to just where we're actually contacting the server, which is what it's for.

Relatedly, we can cut the line already that sets realm in that first setState. It's a no-op because we just read realm from the state. Looks like it's a legacy of an unfinished refactor in 252fed2 -- before then, this line actually set realm to a different value than it already had, and so was doing something useful.

Similarly to moving the setState but smaller: the const { dispatch } = … line can wait until just above the try.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, also, I moved the error:null to just above the try block too.

src/start/RealmScreen.js Outdated Show resolved Hide resolved
@agrawal-d
Copy link
Member Author

Thanks for the review, @gnprice. I've pushed some changes.

agrawal-d and others added 3 commits July 10, 2020 18:52
The 'ErrorMsg' component currently has a 'height' style property,
set to 'CONTROL_SIZE / 2'. However, because of this, longer error
messages do not wrap and get hidden.

So, remove this property.
We just got `realm` from the state a couple of lines ago, so setting
it back to the same value here has no useful effect.  It's confusing,
so remove it.

This appears to be the legacy of an unfinished refactor a couple of
years ago in 252fed2.  Before that, this was the place where we
invoked `fixRealm` (now `fixRealmUrl`) and set the `realm` state
property accordingly.  With that change, that responsibility moved to
within the new `SmartUrlInput`, and this line was edited into being a
no-op.
Show an error message when the user tries to enter an email in the
server URL input field, even before trying to connect to the
server.

Fixes: zulip#4058
@gnprice
Copy link
Member

gnprice commented Jul 11, 2020

Thanks @agrawal-d ! Merged.

I made one change first, inserting a commit to remove realm from the setState. The reason for that isn't really explained by the commit message for the main change, so it belongs in its own little commit in order to get its own commit message.

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.

Give early error when "Zulip server URL" is an email address
3 participants