Skip to content

Commit 9404019

Browse files
committed
Include reason in error messages
1 parent 8889a18 commit 9404019

File tree

2 files changed

+84
-82
lines changed

2 files changed

+84
-82
lines changed

features/search-replace.feature

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Feature: Do global search/replace
3737

3838
When I run `wp search-replace foo bar --include-columns=post_content`
3939
Then STDOUT should be a table containing rows:
40-
| Table | Column | Replacements | Type |
41-
| wp_posts | post_content | 0 | SQL |
40+
| Table | Column | Replacements | Type |
41+
| wp_posts | post_content | 0 | SQL |
4242

4343

4444
Scenario: Multisite search/replace
@@ -100,8 +100,8 @@ Feature: Do global search/replace
100100

101101
When I run `wp search-replace bar burrito wp_post\?`
102102
And STDOUT should be a table containing rows:
103-
| Table | Column | Replacements | Type |
104-
| wp_posts | post_title | 1 | SQL |
103+
| Table | Column | Replacements | Type |
104+
| wp_posts | post_title | 1 | SQL |
105105
And STDOUT should not contain:
106106
"""
107107
wp_options
@@ -134,9 +134,9 @@ Feature: Do global search/replace
134134

135135
When I run `wp search-replace fooz burrito wp_opt\* wp_postme\*`
136136
Then STDOUT should be a table containing rows:
137-
| Table | Column | Replacements | Type |
138-
| wp_options | option_value | 1 | PHP |
139-
| wp_postmeta | meta_key | 1 | SQL |
137+
| Table | Column | Replacements | Type |
138+
| wp_options | option_value | 1 | PHP |
139+
| wp_postmeta | meta_key | 1 | SQL |
140140
And STDOUT should not contain:
141141
"""
142142
wp_posts
@@ -213,23 +213,23 @@ Feature: Do global search/replace
213213

214214
When I run `wp theme mod get header_image_data`
215215
Then STDOUT should be a table containing rows:
216-
| key | value |
217-
| header_image_data | {"url":"https:\/\/subdomain.example.com\/foo.jpg"} |
216+
| key | value |
217+
| header_image_data | {"url":"https:\/\/subdomain.example.com\/foo.jpg"} |
218218

219219
When I run `wp search-replace subdomain.example.com example.com --no-recurse-objects`
220220
Then STDOUT should be a table containing rows:
221-
| Table | Column | Replacements | Type |
222-
| wp_options | option_value | 0 | PHP |
221+
| Table | Column | Replacements | Type |
222+
| wp_options | option_value | 0 | PHP |
223223

224224
When I run `wp search-replace subdomain.example.com example.com`
225225
Then STDOUT should be a table containing rows:
226-
| Table | Column | Replacements | Type |
227-
| wp_options | option_value | 1 | PHP |
226+
| Table | Column | Replacements | Type |
227+
| wp_options | option_value | 1 | PHP |
228228

229229
When I run `wp theme mod get header_image_data`
230230
Then STDOUT should be a table containing rows:
231-
| key | value |
232-
| header_image_data | {"url":"https:\/\/example.com\/foo.jpg"} |
231+
| key | value |
232+
| header_image_data | {"url":"https:\/\/example.com\/foo.jpg"} |
233233

234234
Scenario: Search and replace with quoted strings
235235
Given a WP install
@@ -245,13 +245,13 @@ Feature: Do global search/replace
245245

246246
When I run `wp search-replace '<a href="https://apple.com">Apple</a>' '<a href="https://google.com">Google</a>' --dry-run`
247247
Then STDOUT should be a table containing rows:
248-
| Table | Column | Replacements | Type |
249-
| wp_posts | post_content | 1 | SQL |
248+
| Table | Column | Replacements | Type |
249+
| wp_posts | post_content | 1 | SQL |
250250

251251
When I run `wp search-replace '<a href="https://apple.com">Apple</a>' '<a href="https://google.com">Google</a>'`
252252
Then STDOUT should be a table containing rows:
253-
| Table | Column | Replacements | Type |
254-
| wp_posts | post_content | 1 | SQL |
253+
| Table | Column | Replacements | Type |
254+
| wp_posts | post_content | 1 | SQL |
255255

256256
When I run `wp search-replace '<a href="https://google.com">Google</a>' '<a href="https://apple.com">Apple</a>' --dry-run`
257257
Then STDOUT should contain:
@@ -351,8 +351,8 @@ Feature: Do global search/replace
351351

352352
When I run `wp search-replace 'EXAMPLE.com' 'BAXAMPLE.com' wp_options --regex`
353353
Then STDOUT should be a table containing rows:
354-
| Table | Column | Replacements | Type |
355-
| wp_options | option_value | 0 | PHP |
354+
| Table | Column | Replacements | Type |
355+
| wp_options | option_value | 0 | PHP |
356356

357357
When I run `wp option get home`
358358
Then STDOUT should be:
@@ -362,8 +362,8 @@ Feature: Do global search/replace
362362

363363
When I run `wp search-replace 'EXAMPLE.com' 'BAXAMPLE.com' wp_options --regex --regex-flags=i`
364364
Then STDOUT should be a table containing rows:
365-
| Table | Column | Replacements | Type |
366-
| wp_options | option_value | 5 | PHP |
365+
| Table | Column | Replacements | Type |
366+
| wp_options | option_value | 5 | PHP |
367367

368368
When I run `wp option get home`
369369
Then STDOUT should be:
@@ -376,8 +376,8 @@ Feature: Do global search/replace
376376

377377
When I run `wp search-replace 'HTTPS://EXAMPLE.COM' 'https://example.jp/' wp_options --regex --regex-flags=i --regex-delimiter='#'`
378378
Then STDOUT should be a table containing rows:
379-
| Table | Column | Replacements | Type |
380-
| wp_options | option_value | 2 | PHP |
379+
| Table | Column | Replacements | Type |
380+
| wp_options | option_value | 2 | PHP |
381381

382382
When I run `wp option get home`
383383
Then STDOUT should be:
@@ -387,8 +387,8 @@ Feature: Do global search/replace
387387

388388
When I run `wp search-replace 'https://example.jp/' 'https://example.com/' wp_options --regex-delimiter='/'`
389389
Then STDOUT should be a table containing rows:
390-
| Table | Column | Replacements | Type |
391-
| wp_options | option_value | 2 | PHP |
390+
| Table | Column | Replacements | Type |
391+
| wp_options | option_value | 2 | PHP |
392392

393393
When I run `wp option get home`
394394
Then STDOUT should be:
@@ -548,12 +548,12 @@ Feature: Do global search/replace
548548
Success: Made 3 replacements.
549549
"""
550550
And STDOUT should be a table containing rows:
551-
| Table | Column | Replacements | Type |
552-
| wp_commentmeta | meta_key | 0 | SQL |
553-
| wp_options | option_value | 1 | PHP |
554-
| wp_postmeta | meta_value | 1 | SQL |
555-
| wp_posts | post_title | 1 | SQL |
556-
| wp_users | display_name | 0 | SQL |
551+
| Table | Column | Replacements | Type |
552+
| wp_commentmeta | meta_key | 0 | SQL |
553+
| wp_options | option_value | 1 | PHP |
554+
| wp_postmeta | meta_value | 1 | SQL |
555+
| wp_posts | post_title | 1 | SQL |
556+
| wp_users | display_name | 0 | SQL |
557557
And STDERR should be empty
558558

559559
When I run `wp search-replace baz1 baz2 --report`
@@ -562,12 +562,12 @@ Feature: Do global search/replace
562562
Success: Made 3 replacements.
563563
"""
564564
And STDOUT should be a table containing rows:
565-
| Table | Column | Replacements | Type |
566-
| wp_commentmeta | meta_key | 0 | SQL |
567-
| wp_options | option_value | 1 | PHP |
568-
| wp_postmeta | meta_value | 1 | SQL |
569-
| wp_posts | post_title | 1 | SQL |
570-
| wp_users | display_name | 0 | SQL |
565+
| Table | Column | Replacements | Type |
566+
| wp_commentmeta | meta_key | 0 | SQL |
567+
| wp_options | option_value | 1 | PHP |
568+
| wp_postmeta | meta_value | 1 | SQL |
569+
| wp_posts | post_title | 1 | SQL |
570+
| wp_users | display_name | 0 | SQL |
571571
And STDERR should be empty
572572

573573
When I run `wp search-replace baz2 baz3 --no-report`
@@ -595,12 +595,12 @@ Feature: Do global search/replace
595595
Success: Made 3 replacements.
596596
"""
597597
And STDOUT should be a table containing rows:
598-
| Table | Column | Replacements | Type |
599-
| wp_commentmeta | meta_key | 0 | SQL |
600-
| wp_options | option_value | 1 | PHP |
601-
| wp_postmeta | meta_value | 1 | SQL |
602-
| wp_posts | post_title | 1 | SQL |
603-
| wp_users | display_name | 0 | SQL |
598+
| Table | Column | Replacements | Type |
599+
| wp_commentmeta | meta_key | 0 | SQL |
600+
| wp_options | option_value | 1 | PHP |
601+
| wp_postmeta | meta_value | 1 | SQL |
602+
| wp_posts | post_title | 1 | SQL |
603+
| wp_users | display_name | 0 | SQL |
604604
And STDERR should be empty
605605

606606
When I run `wp search-replace baz4 baz5 --report-changed-only`
@@ -609,10 +609,10 @@ Feature: Do global search/replace
609609
Success: Made 3 replacements.
610610
"""
611611
And STDOUT should end with a table containing rows:
612-
| Table | Column | Replacements | Type |
613-
| wp_options | option_value | 1 | PHP |
614-
| wp_postmeta | meta_value | 1 | SQL |
615-
| wp_posts | post_title | 1 | SQL |
612+
| Table | Column | Replacements | Type |
613+
| wp_options | option_value | 1 | PHP |
614+
| wp_postmeta | meta_value | 1 | SQL |
615+
| wp_posts | post_title | 1 | SQL |
616616
And STDOUT should not contain:
617617
"""
618618
wp_commentmeta meta_key 0 SQL
@@ -653,8 +653,8 @@ Feature: Do global search/replace
653653
Success: Made 0 replacements.
654654
"""
655655
And STDOUT should end with a table containing rows:
656-
| Table | Column | Replacements | Type |
657-
| no_key | | skipped | |
656+
| Table | Column | Replacements | Type |
657+
| no_key | | skipped | |
658658
And STDERR should be empty
659659

660660
And I run `wp search-replace foo bar no_key --report-changed-only --all-tables`
@@ -729,9 +729,9 @@ Feature: Do global search/replace
729729
Success: 2 replacements to be made.
730730
"""
731731
And STDOUT should end with a table containing rows:
732-
| Table | Column | Replacements | Type |
733-
| wp_posts | post_content | 1 | SQL |
734-
| wp_posts | post_title | 1 | SQL |
732+
| Table | Column | Replacements | Type |
733+
| wp_posts | post_content | 1 | SQL |
734+
| wp_posts | post_title | 1 | SQL |
735735

736736
And STDOUT should contain:
737737
"""
@@ -882,9 +882,9 @@ Feature: Do global search/replace
882882
Success: 2 replacements to be made.
883883
"""
884884
And STDOUT should end with a table containing rows:
885-
| Table | Column | Replacements | Type |
886-
| wp_posts | post_content | 1 | PHP |
887-
| wp_posts | post_title | 1 | PHP |
885+
| Table | Column | Replacements | Type |
886+
| wp_posts | post_content | 1 | PHP |
887+
| wp_posts | post_title | 1 | PHP |
888888

889889
And STDOUT should contain:
890890
"""
@@ -1037,11 +1037,11 @@ Feature: Do global search/replace
10371037
And a test_db.sql file:
10381038
"""
10391039
CREATE TABLE `wp_123_test` (
1040-
`name` varchar(50),
1041-
`value` varchar(5000),
1042-
`created_at` datetime NOT NULL,
1043-
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
1044-
PRIMARY KEY (`name`)
1040+
`name` varchar(50),
1041+
`value` varchar(5000),
1042+
`created_at` datetime NOT NULL,
1043+
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
1044+
PRIMARY KEY (`name`)
10451045
) ENGINE=InnoDB;
10461046
INSERT INTO `wp_123_test` VALUES ('test_val','wp_123_test_value_X','2016-11-15 14:41:33','2016-11-15 21:41:33');
10471047
INSERT INTO `wp_123_test` VALUES ('123.','wp_123_test_value_X','2016-11-15 14:41:33','2016-11-15 21:41:33');
@@ -1114,7 +1114,7 @@ Feature: Do global search/replace
11141114
When I try `wp search-replace mysqli_result stdClass`
11151115
Then STDERR should contain:
11161116
"""
1117-
Warning: Skipping an inconvertible serialized object: "O:13:"mysqli_result":5:{s:13:"current_field";N;s:11:"field_count";N;s:7:"lengths";N;s:8:"num_rows";N;s:4:"type";N;}", replacements might not be complete.
1117+
Warning: Skipping an inconvertible serialized object: "O:13:"mysqli_result":5:{s:13:"current_field";N;s:11:"field_count";N;s:7:"lengths";N;s:8:"num_rows";N;s:4:"type";N;}", replacements might not be complete. Reason: Cannot assign null to property mysqli_result::$current_field of type int.
11181118
"""
11191119
And STDOUT should contain:
11201120
"""
@@ -1199,19 +1199,19 @@ Feature: Do global search/replace
11991199
index=1
12001200
while [[ $index -le 199 ]];
12011201
do
1202-
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc')," >> test_db.sql
1203-
index=`expr $index + 1`
1202+
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc')," >> test_db.sql
1203+
index=`expr $index + 1`
12041204
done
1205-
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc');" >> test_db.sql
1205+
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc');" >> test_db.sql
12061206
echo "CREATE TABLE \`wp_123_test_multikey\` (\`key1\` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT, \`key2\` INT(5) UNSIGNED NOT NULL, \`key3\` INT(5) UNSIGNED NOT NULL, \`text\` TEXT, PRIMARY KEY (\`key1\`,\`key2\`,\`key3\`) );" >> test_db.sql
12071207
echo "INSERT INTO \`wp_123_test_multikey\` (\`key2\`,\`key3\`,\`text\`) VALUES" >> test_db.sql
12081208
index=1
12091209
while [[ $index -le 204 ]];
12101210
do
1211-
echo "(0,0,'abc'),(1,1,'abc'),(2,2,'abc'),(3,3,'abc'),(4,4,'abc'),(5,0,'abc'),(6,1,'abc'),(7,2,'abc'),(8,3,'abc'),(9,4,'abc')," >> test_db.sql
1212-
index=`expr $index + 1`
1211+
echo "(0,0,'abc'),(1,1,'abc'),(2,2,'abc'),(3,3,'abc'),(4,4,'abc'),(5,0,'abc'),(6,1,'abc'),(7,2,'abc'),(8,3,'abc'),(9,4,'abc')," >> test_db.sql
1212+
index=`expr $index + 1`
12131213
done
1214-
echo "(0,0,'abc'),(1,1,'abc'),(2,2,'abc'),(3,3,'abc'),(4,4,'abc'),(5,0,'abc'),(6,1,'abc'),(7,2,'abc'),(8,3,'abc'),(9,4,'abc');" >> test_db.sql
1214+
echo "(0,0,'abc'),(1,1,'abc'),(2,2,'abc'),(3,3,'abc'),(4,4,'abc'),(5,0,'abc'),(6,1,'abc'),(7,2,'abc'),(8,3,'abc'),(9,4,'abc');" >> test_db.sql
12151215
"""
12161216
And I run `bash create_sql_file.sh`
12171217
And I run `wp db query "SOURCE test_db.sql;"`
@@ -1250,19 +1250,19 @@ Feature: Do global search/replace
12501250
index=1
12511251
while [[ $index -le 199 ]];
12521252
do
1253-
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc')," >> test_db.sql
1254-
index=`expr $index + 1`
1253+
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc')," >> test_db.sql
1254+
index=`expr $index + 1`
12551255
done
1256-
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc');" >> test_db.sql
1256+
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc');" >> test_db.sql
12571257
echo "CREATE TABLE \`wp_123_test_multikey\` (\`key1\` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT, \`key2\` INT(5) UNSIGNED NOT NULL, \`key3\` INT(5) UNSIGNED NOT NULL, \`text\` TEXT, PRIMARY KEY (\`key1\`,\`key2\`,\`key3\`) );" >> test_db.sql
12581258
echo "INSERT INTO \`wp_123_test_multikey\` (\`key2\`,\`key3\`,\`text\`) VALUES" >> test_db.sql
12591259
index=1
12601260
while [[ $index -le 204 ]];
12611261
do
1262-
echo "(0,0,'abc'),(1,1,'abc'),(2,2,'abc'),(3,3,'abc'),(4,4,'abc'),(5,0,'abc'),(6,1,'abc'),(7,2,'abc'),(8,3,'abc'),(9,4,'abc')," >> test_db.sql
1263-
index=`expr $index + 1`
1262+
echo "(0,0,'abc'),(1,1,'abc'),(2,2,'abc'),(3,3,'abc'),(4,4,'abc'),(5,0,'abc'),(6,1,'abc'),(7,2,'abc'),(8,3,'abc'),(9,4,'abc')," >> test_db.sql
1263+
index=`expr $index + 1`
12641264
done
1265-
echo "(0,0,'abc'),(1,1,'abc'),(2,2,'abc'),(3,3,'abc'),(4,4,'abc'),(5,0,'abc'),(6,1,'abc'),(7,2,'abc'),(8,3,'abc'),(9,4,'abc');" >> test_db.sql
1265+
echo "(0,0,'abc'),(1,1,'abc'),(2,2,'abc'),(3,3,'abc'),(4,4,'abc'),(5,0,'abc'),(6,1,'abc'),(7,2,'abc'),(8,3,'abc'),(9,4,'abc');" >> test_db.sql
12661266
"""
12671267
And I run `bash create_sql_file.sh`
12681268
And I run `wp db query "SOURCE test_db.sql;"`

src/WP_CLI/SearchReplacer.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
9191
$unserialized = is_string( $data ) ? @unserialize( $data ) : false;
9292
error_reporting( $error_reporting );
9393

94-
} catch ( \TypeError $e ) { // phpcs:ignore
94+
} catch ( \TypeError $exception ) { // phpcs:ignore
9595
\WP_CLI::warning(
9696
sprintf(
97-
'Skipping an inconvertible serialized object: "%s", replacements might not be complete.',
98-
$data
97+
'Skipping an inconvertible serialized object: "%s", replacements might not be complete. Reason: %s.',
98+
$data,
99+
$exception->getMessage()
99100
)
100101
);
101102

@@ -123,11 +124,12 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
123124
foreach ( $data as $key => $value ) {
124125
$data->$key = $this->run_recursively( $value, false, $recursion_level + 1, $visited_data );
125126
}
126-
} catch ( \Error $e ) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
127+
} catch ( \Error $exception ) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
127128
\WP_CLI::warning(
128129
sprintf(
129-
'Skipping an inconvertible serialized object: "%s", replacements might not be complete.',
130-
$data
130+
'Skipping an inconvertible serialized object: "%s", replacements might not be complete. Reason: %s.',
131+
$data,
132+
$exception->getMessage()
131133
)
132134
);
133135

@@ -165,7 +167,7 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
165167
if ( $serialised ) {
166168
return serialize( $data );
167169
}
168-
} catch ( Exception $error ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Deliberally empty.
170+
} catch ( Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Intentionally empty.
169171

170172
}
171173

0 commit comments

Comments
 (0)