Skip to content

Add a 'Did you mean ...' error message in case of an unrecognized API property #150

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

Merged
merged 3 commits into from
Sep 14, 2017

Conversation

Lyrkan
Copy link
Collaborator

@Lyrkan Lyrkan commented Aug 30, 2017

This PR aims to improve the error message displayed when an user makes a typo in the name of one of the API methods he's trying to use.

Since pictures speak louder than words:

Before
2017-08-30_19-01-27

After
2017-08-30_19-03-44

Note that the error is triggered when trying to access the property, not when the method is called (since it doesn't exist), hence the "is not a recognized property" message. I'm aware that the use of the "property" word in it may not be ideal, but using "method" instead feels wrong too since we could theoretically have other things than methods in the API object.

@stof
Copy link
Member

stof commented Aug 31, 2017

@Lyrkan what about saying unrecognized property or method ?

@Lyrkan
Copy link
Collaborator Author

Lyrkan commented Aug 31, 2017

I was so focused on trying to find a single word that I didn't even think of using both of them :)

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

Wow! I LOVE this! It's exactly the helpful, teaching way that I want Encore to work. I added 2 minor comments.

index.js Outdated
@@ -631,6 +633,21 @@ const publicApiProxy = new Proxy(publicApi, {
process.exit(1); // eslint-disable-line
}
};
} else if (typeof target[prop] === 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

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

nit pick: since we return in the first if, this doesn't need to be a nested else if, it can just be a fresh, un-nested if.

similarProperty = apiProperty;
minDistance = distance;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

If you type something totally crazy that is completely not close to anything, it will suggest something, right? In Symfony's core, we use "3" as the maximum distance (if it's more than 3, we don't recommend anything): https://github.com/symfony/symfony/blob/1bb2bc322bc1c2d0e6e70c2a0fa9fc0be90757fd/src/Symfony/Component/ExpressionLanguage/SyntaxError.php#L34.

It probably makes sense to do the same thing here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

2017-09-13_23-01-01

2017-09-13_23-02-08

Copy link
Member

Choose a reason for hiding this comment

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

to be exact, we don't use 3 most of the time. We use strlen(input) / 3 (we have a few cases using 3)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, thanks! I've just changed that at sha: 2e216a4... and indeed, it is a bit more "forgiving" in appropriate situations.

@weaverryan
Copy link
Member

<3 this feature. Thank you @Lyrkan!

@weaverryan weaverryan merged commit a16e1af into symfony:master Sep 14, 2017
weaverryan added a commit that referenced this pull request Sep 14, 2017
…ecognized API property (Lyrkan)

This PR was squashed before being merged into the master branch (closes #150).

Discussion
----------

Add a 'Did you mean ...' error message in case of an unrecognized API property

This PR aims to improve the error message displayed when an user makes a typo in the name of one of the API methods he's trying to use.

Since pictures speak louder than words:

**Before**
![2017-08-30_19-01-27](https://user-images.githubusercontent.com/850046/29885253-724d45a0-8db6-11e7-8ec5-9e63fa9d786e.png)

**After**
![2017-08-30_19-03-44](https://user-images.githubusercontent.com/850046/29885266-7a0f3d0c-8db6-11e7-9af8-d9aad52917c2.png)

Note that the error is triggered when trying to access the property, not when the method is called (since it doesn't exist), hence the "is not a recognized property" message. I'm aware that the use of the "property" word in it may not be ideal, but using "method" instead feels wrong too since we could theoretically have other things than methods in the API object.

Commits
-------

a16e1af Only add the "Did you mean" message if the levenshtein distance is less than 3
1ceabc1 Add "or method" to the "Did you mean ..." error message
d592481 Add a 'Did you mean ...' error message for unrecognized API methods
@Lyrkan Lyrkan deleted the did-you-mean branch September 14, 2017 19:25
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.

3 participants