popojargo commented on a change in pull request #1137: Validate regex on client
side.
URL: https://github.com/apache/couchdb-fauxton/pull/1137#discussion_r225313780
##########
File path: app/addons/documents/mango/components/MangoQueryEditor.js
##########
@@ -130,16 +131,54 @@ export default class MangoQueryEditor extends Component {
notifyOnQueryError() {
if (this.editorHasErrors()) {
FauxtonAPI.addNotification({
- msg: 'Please fix the Javascript errors and try again.',
+ msg: 'Please fix the JSON errors and try again.',
type: 'error',
clear: true
});
return true;
}
+
return false;
}
+ getJsonIfValid(json) {
+ try {
+ return JSON.parse(json);
+ } catch (e) {
+ FauxtonAPI.addNotification({
+ msg: 'Please fix the JSON errors and try again. Error: ' + e.message,
+ type: 'error',
+ clear: true
+ });
+ }
+ }
+
+ isRegexValid(selector = {}) {
+ const regexes = Helper.getSelectorRegexes(selector);
+ const errors = _.reduce(regexes, (acc, val) => {
+ try {
+ new RegExp(val);
Review comment:
Adding a custom highlighter for the $regex key-value pairs in the editor
looks complicated to me. Although, even with a good highlighter, the error will
still be vague.
So I guess the first thing could be to find a way to have more expressive
error messages.
My two suggestions:
- Reformat the Regex error message (display the letters as char, not
charCode).
- On a query error, we can check the server error and display an appropriate
error message if it's caused by regex errors (I can reverse engineer the
request and detect Regex errors with the JavaScript regex engine.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services