Commit 3a3013c8 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge branch 'DBAL-490' into 2.3

parents 565facd9 02a94151
......@@ -40,13 +40,35 @@ class DBALException extends \Exception
{
$msg = "An exception occurred while executing '".$sql."'";
if ($params) {
$msg .= " with params ".json_encode($params);
$msg .= " with params " . self::formatParameters($params);
}
$msg .= ":\n\n".$driverEx->getMessage();
return new self($msg, 0, $driverEx);
}
/**
* Returns a human-readable representation of an array of parameters.
* This properly handles binary data by returning a hex representation.
*
* @param array $params
*
* @return string
*/
private static function formatParameters(array $params)
{
return '[' . implode(', ', array_map(function($param) {
$json = @json_encode($param);
if (! is_string($json) || $json == 'null' && is_string($param)) {
// JSON encoding failed, this is not a UTF-8 string.
return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
}
return $json;
}, $params)) . ']';
}
public static function invalidWrapperClass($wrapperClass)
{
return new self("The given 'wrapperClass' ".$wrapperClass." has to be a ".
......
<?php
namespace Doctrine\Tests\DBAL;
use Doctrine\DBAL\DBALException;
class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase
{
public function testDriverExceptionDuringQueryAcceptsBinaryData()
{
$e = DBALException::driverExceptionDuringQuery(new \Exception, '', array('ABC', chr(128)));
$this->assertContains('with params ["ABC", "\x80"]', $e->getMessage());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment