Skip to content

Commit 3a251f9

Browse files
author
epriestley
committed
Improve error message when user encounters a table/column schema error
Summary: These are because they forgot to upgrade_schema.php like 99% of the time. Test Plan: Hit such an error, got a better error message than before. Reviewers: btrahan, jungejason Reviewed By: jungejason CC: aran, epriestley Differential Revision: https://secure.phabricator.com/D1786
1 parent 1e4e3d1 commit 3a251f9

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

src/__phutil_library_map__.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
'AphrontQueryObjectMissingException' => 'storage/exception/objectmissing',
7878
'AphrontQueryParameterException' => 'storage/exception/parameter',
7979
'AphrontQueryRecoverableException' => 'storage/exception/recoverable',
80+
'AphrontQuerySchemaException' => 'storage/exception/schema',
8081
'AphrontRedirectException' => 'aphront/exception/redirect',
8182
'AphrontRedirectResponse' => 'aphront/response/redirect',
8283
'AphrontReloadResponse' => 'aphront/response/reload',
@@ -961,6 +962,7 @@
961962
'AphrontQueryObjectMissingException' => 'AphrontQueryException',
962963
'AphrontQueryParameterException' => 'AphrontQueryException',
963964
'AphrontQueryRecoverableException' => 'AphrontQueryException',
965+
'AphrontQuerySchemaException' => 'AphrontQueryException',
964966
'AphrontRedirectException' => 'AphrontException',
965967
'AphrontRedirectResponse' => 'AphrontResponse',
966968
'AphrontReloadResponse' => 'AphrontRedirectResponse',

src/storage/connection/mysql/AphrontMySQLDatabaseConnection.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,27 +293,32 @@ private function throwQueryException($connection) {
293293
$error = mysql_error($connection);
294294
}
295295

296+
$exmsg = "#{$errno}: {$error}";
297+
296298
switch ($errno) {
297299
case 2013: // Connection Dropped
298300
case 2006: // Gone Away
299-
throw new AphrontQueryConnectionLostException("#{$errno}: {$error}");
301+
throw new AphrontQueryConnectionLostException($exmsg);
300302
case 1213: // Deadlock
301303
case 1205: // Lock wait timeout exceeded
302-
throw new AphrontQueryRecoverableException("#{$errno}: {$error}");
304+
throw new AphrontQueryRecoverableException($exmsg);
303305
case 1062: // Duplicate Key
304306
// NOTE: In some versions of MySQL we get a key name back here, but
305307
// older versions just give us a key index ("key 2") so it's not
306308
// portable to parse the key out of the error and attach it to the
307309
// exception.
308-
throw new AphrontQueryDuplicateKeyException("{$errno}: {$error}");
310+
throw new AphrontQueryDuplicateKeyException($exmsg);
309311
case 1044: // Access denied to database
310312
case 1045: // Access denied (auth)
311313
case 1142: // Access denied to table
312314
case 1143: // Access denied to column
313-
throw new AphrontQueryAccessDeniedException("#{$errno}: {$error}");
315+
throw new AphrontQueryAccessDeniedException($exmsg);
316+
case 1146: // No such table
317+
case 1154: // Unknown column "..." in field list
318+
throw new AphrontQuerySchemaException($exmsg);
314319
default:
315320
// TODO: 1064 is syntax error, and quite terrible in production.
316-
throw new AphrontQueryException("#{$errno}: {$error}");
321+
throw new AphrontQueryException($exmsg);
317322
}
318323
}
319324

src/storage/connection/mysql/__init__.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
phutil_require_module('phabricator', 'storage/exception/connectionlost');
1616
phutil_require_module('phabricator', 'storage/exception/duplicatekey');
1717
phutil_require_module('phabricator', 'storage/exception/recoverable');
18+
phutil_require_module('phabricator', 'storage/exception/schema');
1819

1920
phutil_require_module('phutil', 'error');
2021
phutil_require_module('phutil', 'serviceprofiler');
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2012 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @group storage
21+
*/
22+
final class AphrontQuerySchemaException extends AphrontQueryException {
23+
24+
public function __construct($message) {
25+
$message .=
26+
"\n\n".
27+
"NOTE: This usually indicates that the MySQL schema has not been ".
28+
"properly upgraded. Run scripts/sql/upgrade_schema.php to ensure your ".
29+
"schema is up to date.";
30+
31+
parent::__construct($message);
32+
}
33+
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phabricator', 'storage/exception/base');
10+
11+
12+
phutil_require_source('AphrontQuerySchemaException.php');

0 commit comments

Comments
 (0)