Skip to content

Commit f446ca1

Browse files
schlesseratosite
andauthored
Improve error message on regex check (#131)
Improve error message on regex check Co-authored-by: tosite <n.teshima@pepabo.com>
2 parents d53ae07 + 70e8255 commit f446ca1

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

features/search-replace.feature

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,10 @@ Feature: Do global search/replace
195195
Scenario: Regex search/replace with a incorrect `--regex-flags`
196196
Given a WP install
197197
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-flags='kppr'`
198-
Then STDERR should contain:
199-
"""
200-
(Hello)\s(world)
201-
"""
202-
And STDERR should contain:
198+
Then STDERR should be:
203199
"""
204-
kppr
200+
Error: The regex pattern '(Hello)\s(world)' with default delimiter 'chr(1)' and flags 'kppr' fails.
201+
preg_match(): Unknown modifier 'k'.
205202
"""
206203
And the return code should be 1
207204

@@ -403,35 +400,68 @@ Feature: Do global search/replace
403400
Then STDERR should be:
404401
"""
405402
Error: The regex '1HTTP://EXAMPLE.COM1i' fails.
403+
preg_match(): Delimiter must not be alphanumeric or backslash.
406404
"""
407405
And the return code should be 1
408406

409407
When I try `wp search-replace 'regex error)' '' --regex`
410-
Then STDERR should be:
408+
Then STDERR should contain:
411409
"""
412410
Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and no flags fails.
413411
"""
412+
And STDERR should contain:
413+
"""
414+
preg_match(): Compilation failed:
415+
"""
416+
And STDERR should contain:
417+
"""
418+
at offset 11
419+
"""
414420
And the return code should be 1
415421

416422
When I try `wp search-replace 'regex error)' '' --regex --regex-flags=u`
417-
Then STDERR should be:
423+
Then STDERR should contain:
418424
"""
419425
Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and flags 'u' fails.
420426
"""
427+
And STDERR should contain:
428+
"""
429+
preg_match(): Compilation failed:
430+
"""
431+
And STDERR should contain:
432+
"""
433+
at offset 11
434+
"""
421435
And the return code should be 1
422436

423437
When I try `wp search-replace 'regex error)' '' --regex --regex-delimiter=/`
424-
Then STDERR should be:
438+
Then STDERR should contain:
425439
"""
426440
Error: The regex '/regex error)/' fails.
427441
"""
442+
And STDERR should contain:
443+
"""
444+
preg_match(): Compilation failed:
445+
"""
446+
And STDERR should contain:
447+
"""
448+
at offset 11
449+
"""
428450
And the return code should be 1
429451

430452
When I try `wp search-replace 'regex error)' '' --regex --regex-delimiter=/ --regex-flags=u`
431-
Then STDERR should be:
453+
Then STDERR should contain:
432454
"""
433455
Error: The regex '/regex error)/u' fails.
434456
"""
457+
And STDERR should contain:
458+
"""
459+
preg_match(): Compilation failed:
460+
"""
461+
And STDERR should contain:
462+
"""
463+
at offset 11
464+
"""
435465
And the return code should be 1
436466

437467
Scenario: Formatting as count-only

src/Search_Replace_Command.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@ public function __invoke( $args, $assoc_args ) {
203203

204204
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Preventing a warning when testing the regex.
205205
if ( false === @preg_match( $search_regex, '' ) ) {
206+
$error = error_get_last();
207+
$preg_error_message = ( ! empty( $error ) && array_key_exists( 'message', $error ) ) ? "\n{$error['message']}." : '';
206208
if ( $default_regex_delimiter ) {
207209
$flags_msg = $this->regex_flags ? "flags '$this->regex_flags'" : 'no flags';
208210
$msg = "The regex pattern '$old' with default delimiter 'chr(1)' and {$flags_msg} fails.";
209211
} else {
210212
$msg = "The regex '$search_regex' fails.";
211213
}
212-
WP_CLI::error( $msg );
214+
WP_CLI::error( $msg . $preg_error_message );
213215
}
214216
}
215217

0 commit comments

Comments
 (0)