1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-16 02:54:25 +02:00

Translator: better error messages

This commit is contained in:
David Grudl
2015-11-02 16:47:35 +01:00
parent 654e345921
commit a9a45f0c4c
2 changed files with 22 additions and 28 deletions

View File

@@ -27,8 +27,8 @@ final class Translator
/** @var array */ /** @var array */
private $args; private $args;
/** @var bool */ /** @var string[] */
private $hasError; private $errors;
/** @var bool */ /** @var bool */
private $comment; private $comment;
@@ -76,7 +76,7 @@ final class Translator
$this->limit = NULL; $this->limit = NULL;
$this->offset = NULL; $this->offset = NULL;
$this->hasError = FALSE; $this->errors = [];
$commandIns = NULL; $commandIns = NULL;
$lastArr = NULL; $lastArr = NULL;
// shortcuts // shortcuts
@@ -164,8 +164,8 @@ final class Translator
$sql = implode(' ', $sql); $sql = implode(' ', $sql);
if ($this->hasError) { if ($this->errors) {
throw new Exception('SQL translate error', 0, $sql); throw new Exception('SQL translate error: ' . trim(reset($this->errors), '*'), 0, $sql);
} }
// apply limit // apply limit
@@ -281,15 +281,13 @@ final class Translator
if (is_array($v)) { if (is_array($v)) {
if (isset($proto)) { if (isset($proto)) {
if ($proto !== array_keys($v)) { if ($proto !== array_keys($v)) {
$this->hasError = TRUE; return $this->errors[] = '**Multi-insert array "' . $k . '" is different**';
return '**Multi-insert array "' . $k . '" is different.**';
} }
} else { } else {
$proto = array_keys($v); $proto = array_keys($v);
} }
} else { } else {
$this->hasError = TRUE; return $this->errors[] = '**Unexpected type ' . (is_object($v) ? get_class($v) : gettype($v)) . '**';
return '**Unexpected type ' . gettype($v) . '**';
} }
$pair = explode('%', $k, 2); // split into identifier & modifier $pair = explode('%', $k, 2); // split into identifier & modifier
@@ -333,8 +331,8 @@ final class Translator
// with modifier procession // with modifier procession
if ($modifier) { if ($modifier) {
if ($value !== NULL && !is_scalar($value) && !$value instanceof \DateTime && !$value instanceof \DateTimeInterface) { // array is already processed if ($value !== NULL && !is_scalar($value) && !$value instanceof \DateTime && !$value instanceof \DateTimeInterface) { // array is already processed
$this->hasError = TRUE; $type = is_object($value) ? get_class($value) : gettype($value);
return '**Unexpected type ' . gettype($value) . '**'; return $this->errors[] = "**Invalid combination of type $type and modifier %$modifier**";
} }
switch ($modifier) { switch ($modifier) {
@@ -430,12 +428,11 @@ final class Translator
case 'a': case 'a':
case 'l': case 'l':
case 'v': case 'v':
$this->hasError = TRUE; $type = gettype($value);
return '**Unexpected type ' . gettype($value) . '**'; return $this->errors[] = "**Invalid combination of type $type and modifier %$modifier**";
default: default:
$this->hasError = TRUE; return $this->errors[] = "**Unknown or unexpected modifier %$modifier**";
return "**Unknown or invalid modifier %$modifier**";
} }
} }
@@ -463,8 +460,8 @@ final class Translator
return (string) $value; return (string) $value;
} else { } else {
$this->hasError = TRUE; $type = is_object($value) ? get_class($value) : gettype($value);
return '**Unexpected ' . gettype($value) . '**'; return $this->errors[] = "**Unexpected $type**";
} }
} }
@@ -493,8 +490,7 @@ final class Translator
$cursor = & $this->cursor; $cursor = & $this->cursor;
if ($cursor >= count($this->args)) { if ($cursor >= count($this->args)) {
$this->hasError = TRUE; return $this->errors[] = '**Extra placeholder**';
return '**Extra placeholder**';
} }
$cursor++; $cursor++;
@@ -506,8 +502,7 @@ final class Translator
$cursor = & $this->cursor; $cursor = & $this->cursor;
if ($cursor >= count($this->args) && $mod !== 'else' && $mod !== 'end') { if ($cursor >= count($this->args) && $mod !== 'else' && $mod !== 'end') {
$this->hasError = TRUE; return $this->errors[] = "**Extra modifier %$mod**";
return "**Extra modifier %$mod**";
} }
if ($mod === 'if') { if ($mod === 'if') {
@@ -589,8 +584,7 @@ final class Translator
return $this->driver->escapeText(str_replace('""', '"', $matches[6])); return $this->driver->escapeText(str_replace('""', '"', $matches[6]));
} elseif ($matches[7]) { // string quote } elseif ($matches[7]) { // string quote
$this->hasError = TRUE; return $this->errors[] = '**Alone quote**';
return '**Alone quote**';
} }
if ($matches[8]) { // SQL identifier substitution if ($matches[8]) { // SQL identifier substitution

View File

@@ -77,8 +77,8 @@ Assert::same(
// invalid input // invalid input
$e = Assert::exception(function () use ($conn) { $e = Assert::exception(function () use ($conn) {
$conn->translate('SELECT %s', (object) [123], ', %m', 123); $conn->translate('SELECT %s', (object) [123], ', %m', 123);
}, 'Dibi\Exception', 'SQL translate error'); }, 'Dibi\Exception', 'SQL translate error: Invalid combination of type stdClass and modifier %s');
Assert::same('SELECT **Unexpected type object** , **Unknown or invalid modifier %m**', $e->getSql()); Assert::same('SELECT **Invalid combination of type stdClass and modifier %s** , **Unknown or unexpected modifier %m**', $e->getSql());
Assert::same( Assert::same(
reformat('SELECT * FROM [table] WHERE id=10 AND name=\'ahoj\''), reformat('SELECT * FROM [table] WHERE id=10 AND name=\'ahoj\''),
@@ -228,7 +228,7 @@ if ($config['system'] === 'postgre') {
$e = Assert::exception(function () use ($conn) { $e = Assert::exception(function () use ($conn) {
$conn->translate("SELECT '"); $conn->translate("SELECT '");
}, 'Dibi\Exception', 'SQL translate error'); }, 'Dibi\Exception', 'SQL translate error: Alone quote');
Assert::same('SELECT **Alone quote**', $e->getSql()); Assert::same('SELECT **Alone quote**', $e->getSql());
Assert::match( Assert::match(
@@ -478,8 +478,8 @@ $e = Assert::exception(function () use ($conn) {
'num%i' => ['1', ''], 'num%i' => ['1', ''],
]; ];
$conn->translate('INSERT INTO test %m', $array6); $conn->translate('INSERT INTO test %m', $array6);
}, 'Dibi\Exception', 'SQL translate error'); }, 'Dibi\Exception', 'SQL translate error: Multi-insert array "num%i" is different');
Assert::same('INSERT INTO test **Multi-insert array "num%i" is different.**', $e->getSql()); Assert::same('INSERT INTO test **Multi-insert array "num%i" is different**', $e->getSql());
$array6 = [ $array6 = [
'id' => [1, 2, 3, 4], 'id' => [1, 2, 3, 4],