Skip to content

Commit

Permalink
feat: Show new detailed 'help' message for regex errors
Browse files Browse the repository at this point in the history
  • Loading branch information
claremacrae committed Jul 17, 2023
1 parent f210d0d commit 733316c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Query/Filter/TextField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export abstract class TextField extends Field {
try {
matcher = RegexMatcher.validateAndConstruct(filterValue);
} catch (e) {
return FilterOrErrorMessage.fromError(line, errorMessageForException('Parsing regular expression', e));
const message =
errorMessageForException('Parsing regular expression', e) + `\n\n${RegexMatcher.helpMessage()}`;
return FilterOrErrorMessage.fromError(line, message);
}
if (matcher === null) {
return FilterOrErrorMessage.fromError(
Expand Down
30 changes: 29 additions & 1 deletion tests/Query/Filter/TextField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,35 @@ describe('should report regular expression errors to user ', () => {
const instruction = 'description regex matches /hello(/';
const filterOrError = new DescriptionField().createFilterOrErrorMessage(instruction);
expect(filterOrError.error).toEqual(
'Error: Parsing regular expression. The error message was: "SyntaxError: Invalid regular expression: /hello(/: Unterminated group"',
String.raw`Error: Parsing regular expression. The error message was: "SyntaxError: Invalid regular expression: /hello(/: Unterminated group"
See https://publish.obsidian.md/tasks/Queries/Regular+Expressions
Regular expressions must look like this:
/pattern/
or this:
/pattern/flags
Where:
- pattern: The 'regular expression' pattern to search for.
- flags: Optional characters that modify the search.
i => make the search case-insensitive
u => add Unicode support
Examples: /^Log/
/^Log/i
/File Name\.md/
/waiting|waits|waited/i
/\d\d:\d\d/
The following characters have special meaning in the pattern:
to find them literally, you must add a \ before them:
[\^$.|?*+()
CAUTION! Regular expression (or 'regex') searching is a powerful
but advanced feature that requires thorough knowledge in order to
use successfully, and not miss intended search results.
`,
);
});
});
Expand Down

0 comments on commit 733316c

Please sign in to comment.