Skip to content

Commit

Permalink
Move formatError into Query\Utilities
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed May 30, 2020
1 parent 761045b commit 585304b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 59 deletions.
56 changes: 0 additions & 56 deletions libraries/classes/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1946,62 +1946,6 @@ public function getTriggers(string $db, string $table = '', $delimiter = '//')
return $result;
}

/**
* Formats database error message in a friendly way.
* This is needed because some errors messages cannot
* be obtained by mysql_error().
*
* @param int $error_number Error code
* @param string $error_message Error message as returned by server
*
* @return string HML text with error details
*/
public static function formatError(int $error_number, string $error_message): string
{
$error_message = htmlspecialchars($error_message);

$error = '#' . ((string) $error_number);
$separator = ' &mdash; ';

if ($error_number == 2002) {
$error .= ' - ' . $error_message;
$error .= $separator;
$error .= __(
'The server is not responding (or the local server\'s socket'
. ' is not correctly configured).'
);
} elseif ($error_number == 2003) {
$error .= ' - ' . $error_message;
$error .= $separator . __('The server is not responding.');
} elseif ($error_number == 1698) {
$error .= ' - ' . $error_message;
$error .= $separator . '<a href="' . Url::getFromRoute('/logout') . '" class="disableAjax">';
$error .= __('Logout and try as another user.') . '</a>';
} elseif ($error_number == 1005) {
if (strpos($error_message, 'errno: 13') !== false) {
$error .= ' - ' . $error_message;
$error .= $separator
. __(
'Please check privileges of directory containing database.'
);
} else {
/**
* InnoDB constraints, see
* https://dev.mysql.com/doc/refman/5.0/en/
* innodb-foreign-key-constraints.html
*/
$error .= ' - ' . $error_message .
' (<a href="' .
Url::getFromRoute('/server/engines/InnoDB/Status') .
'">' . __('Details…') . '</a>)';
}
} else {
$error .= ' - ' . $error_message;
}

return $error;
}

/**
* gets the current user with host
*
Expand Down
3 changes: 2 additions & 1 deletion libraries/classes/Dbal/DbiMysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use mysqli_result;
use mysqli_stmt;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Query\Utilities;
use stdClass;
use const E_USER_WARNING;
use const MYSQLI_ASSOC;
Expand Down Expand Up @@ -426,7 +427,7 @@ public function getError($mysqli)
// the call to getError()
$GLOBALS['errno'] = $error_number;

return $GLOBALS['dbi']->formatError($error_number, $error_message);
return Utilities::formatError($error_number, $error_message);
}

/**
Expand Down
59 changes: 59 additions & 0 deletions libraries/classes/Query/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace PhpMyAdmin\Query;

use PhpMyAdmin\Url;
use function htmlspecialchars;
use function strpos;
use function strtolower;

/**
Expand Down Expand Up @@ -56,4 +59,60 @@ public static function isSystemSchema(
|| $isMySqlSystemSchema
|| $schema_name === 'sys';
}

/**
* Formats database error message in a friendly way.
* This is needed because some errors messages cannot
* be obtained by mysql_error().
*
* @param int $error_number Error code
* @param string $error_message Error message as returned by server
*
* @return string HML text with error details
*/
public static function formatError(int $error_number, string $error_message): string
{
$error_message = htmlspecialchars($error_message);

$error = '#' . ((string) $error_number);
$separator = ' &mdash; ';

if ($error_number == 2002) {
$error .= ' - ' . $error_message;
$error .= $separator;
$error .= __(
'The server is not responding (or the local server\'s socket'
. ' is not correctly configured).'
);
} elseif ($error_number == 2003) {
$error .= ' - ' . $error_message;
$error .= $separator . __('The server is not responding.');
} elseif ($error_number == 1698) {
$error .= ' - ' . $error_message;
$error .= $separator . '<a href="' . Url::getFromRoute('/logout') . '" class="disableAjax">';
$error .= __('Logout and try as another user.') . '</a>';
} elseif ($error_number == 1005) {
if (strpos($error_message, 'errno: 13') !== false) {
$error .= ' - ' . $error_message;
$error .= $separator
. __(
'Please check privileges of directory containing database.'
);
} else {
/**
* InnoDB constraints, see
* https://dev.mysql.com/doc/refman/5.0/en/
* innodb-foreign-key-constraints.html
*/
$error .= ' - ' . $error_message .
' (<a href="' .
Url::getFromRoute('/server/engines/InnoDB/Status') .
'">' . __('Details…') . '</a>)';
}
} else {
$error .= ' - ' . $error_message;
}

return $error;
}
}
3 changes: 2 additions & 1 deletion test/classes/DatabaseInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use PhpMyAdmin\Database\DatabaseList;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\SystemDatabase;
use PhpMyAdmin\Tests\Stubs\DbiDummy;
use PhpMyAdmin\Util;
Expand Down Expand Up @@ -408,7 +409,7 @@ public function testFormatError($error_number, $error_message, $match): void
{
$this->assertStringContainsString(
$match,
DatabaseInterface::formatError($error_number, $error_message)
Utilities::formatError($error_number, $error_message)
);
}

Expand Down
2 changes: 1 addition & 1 deletion test/classes/Dbal/DbiDummyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function testFormatError($number, $message, $expected): void
$GLOBALS['server'] = 1;
$this->assertEquals(
$expected,
$GLOBALS['dbi']->formatError($number, $message)
Utilities::formatError($number, $message)
);
}

Expand Down

0 comments on commit 585304b

Please sign in to comment.