Skip to content

Commit 146899d

Browse files
committed
Specify error type
1 parent c2ab2dc commit 146899d

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/JSONDB/JSONDB.php

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function __construct()
203203
public function setDatabase($database)
204204
{
205205
if (NULL === $this->server) {
206-
throw new Exception("Can't use the database \"{$database}\", there is no connection established with a server.");
206+
throw new Exception("JSONDB Error: Can't use the database \"{$database}\", there is no connection established with a server.");
207207
}
208208
$this->database = $database;
209209
return $this;
@@ -218,7 +218,7 @@ public function setDatabase($database)
218218
public function setTable($table)
219219
{
220220
if (NULL === $this->database) {
221-
throw new Exception("Can't use the table \"{$table}\", there is no database selected.");
221+
throw new Exception("JSONDB Error: Can't use the table \"{$table}\", there is no database selected.");
222222
}
223223
$this->table = $table;
224224
return $this;
@@ -293,12 +293,12 @@ public function createServer($path, $username, $password, $connect = FALSE)
293293
if (isset($path, $username, $password)) {
294294
if (file_exists($path) || is_dir($path)) {
295295
$this->benchmark->mark('jsondb_(createServer)_end');
296-
throw new Exception("Can't create server \"{$path}\", the directory already exists.");
296+
throw new Exception("JSONDB Error: Can't create server \"{$path}\", the directory already exists.");
297297
}
298298

299299
if (!@mkdir($path, 0777, TRUE) && !is_dir($path)) {
300300
$this->benchmark->mark('jsondb_(createServer)_end');
301-
throw new Exception("Can't create the server \"{$path}\". Maybe you don't have write access.");
301+
throw new Exception("JSONDB Error: Can't create the server \"{$path}\". Maybe you don't have write access.");
302302
}
303303

304304
chmod($path, 0777);
@@ -327,19 +327,19 @@ public function createDatabase($name)
327327
$this->benchmark->mark('jsondb_(createDatabase)_start');
328328
if (NULL === $this->server) {
329329
$this->benchmark->mark('jsondb_(createDatabase)_end');
330-
throw new Exception("Can't create the database \"{$name}\", there is no connection established with a server.");
330+
throw new Exception("JSONDB Error: Can't create the database \"{$name}\", there is no connection established with a server.");
331331
}
332332

333333
$path = $this->_getDatabasePath($name);
334334

335335
if (file_exists($path)) {
336336
$this->benchmark->mark('jsondb_(createDatabase)_end');
337-
throw new Exception("Can't create the database \"{$name}\" in the server \"{$this->server}\", the database already exist.");
337+
throw new Exception("JSONDB Error: Can't create the database \"{$name}\" in the server \"{$this->server}\", the database already exist.");
338338
}
339339

340340
if (!@mkdir($path, 0777, TRUE) && !is_dir($path)) {
341341
$this->benchmark->mark('jsondb_(createDatabase)_end');
342-
throw new Exception("Can't create the database \"{$name}\" in the server \"{$this->server}\".");
342+
throw new Exception("JSONDB Error: Can't create the database \"{$name}\" in the server \"{$this->server}\".");
343343
} else {
344344
chmod($path, 0777);
345345
}
@@ -368,12 +368,12 @@ public function connect($server, $username, $password, $database = NULL)
368368

369369
if (!array_key_exists($server, $config)) {
370370
$this->benchmark->mark('jsondb_(connect)_end');
371-
throw new Exception("There is no registered server with the path \"{$server}\".");
371+
throw new Exception("JSONDB Error: There is no registered server with the path \"{$server}\".");
372372
}
373373

374374
if ($config[$server]['username'] !== sha1(md5($username)) || $config[$server]['password'] !== sha1(md5($password))) {
375375
$this->benchmark->mark('jsondb_(connect)_end');
376-
throw new Exception("User's authentication failed for user \"{$username}\" on server \"{$server}\". Access denied.");
376+
throw new Exception("JSONDB Error: User's authentication failed for user \"{$username}\" on server \"{$server}\". Access denied.");
377377
}
378378

379379
$this->config->addUser($server, $username, $password);
@@ -420,7 +420,7 @@ public function createTable($name, array $prototype)
420420
$table_path = $this->_getTablePath($name);
421421
if (file_exists($table_path)) {
422422
$this->benchmark->mark('jsondb_(createTable)_end');
423-
throw new Exception("Can't create the table \"{$name}\" in the database \"{$this->database}\". The table already exist.");
423+
throw new Exception("JSONDB Error: Can't create the table \"{$name}\" in the database \"{$this->database}\". The table already exist.");
424424
}
425425

426426
$fields = array();
@@ -432,7 +432,7 @@ public function createTable($name, array $prototype)
432432
$has_uk = array_key_exists('unique_key', $prop);
433433
if ($ai_exist && $has_ai) {
434434
$this->benchmark->mark('jsondb_(createTable)_end');
435-
throw new Exception("Can't use the \"auto_increment\" property on more than one fields.");
435+
throw new Exception("JSONDB Error: Can't use the \"auto_increment\" property on more than one fields.");
436436
} elseif (!$ai_exist && $has_ai) {
437437
$ai_exist = TRUE;
438438
$prototype[$field]['unique_key'] = TRUE;
@@ -458,7 +458,7 @@ public function createTable($name, array $prototype)
458458
);
459459
if (touch($table_path) === FALSE) {
460460
$this->benchmark->mark('jsondb_(createTable)_end');
461-
throw new Exception("Can't create file \"{$table_path}\".");
461+
throw new Exception("JSONDB Error: Can't create file \"{$table_path}\".");
462462
}
463463
chmod($table_path, 0777);
464464
file_put_contents($table_path, json_encode($data));
@@ -528,10 +528,10 @@ public function bindValue($key, $value, $parse_method = JSONDB::PARAM_STRING)
528528
}
529529
$this->queryString = str_replace($key, $value, $this->queryString);
530530
} else {
531-
throw new Exception("Can't bind the value \"{$value}\" for the key \"{$key}\". The key isn't in the query.");
531+
throw new Exception("JSONDB Error: Can't bind the value \"{$value}\" for the key \"{$key}\". The key isn't in the query.");
532532
}
533533
} else {
534-
throw new Exception("Can't use JSONDB::bindValue() with non prepared queries. Send your query with JSONDB::prepare() first.");
534+
throw new Exception("JSONDB Error: Can't use JSONDB::bindValue() with non prepared queries. Send your query with JSONDB::prepare() first.");
535535
}
536536
}
537537

@@ -553,7 +553,7 @@ public static function quote($value)
553553
public function execute()
554554
{
555555
if (NULL === $this->database || NULL === $this->parsedQuery) {
556-
throw new Exception("Can't execute the query. No database/table selected or internal error.");
556+
throw new Exception("JSONDB Error: Can't execute the query. No database/table selected or internal error.");
557557
}
558558

559559
if ($this->queryIsPrepared()) {
@@ -564,7 +564,7 @@ public function execute()
564564
$this->setTable($this->parsedQuery['table']);
565565
$table_path = $this->_getTablePath();
566566
if (!file_exists($table_path) || !is_readable($table_path) || !is_writable($table_path)) {
567-
throw new Exception("Can't execute the query. The table \"{$this->table}\" doesn't exists in database \"{$this->database}\" or file access denied.");
567+
throw new Exception("JSONDB Error: Can't execute the query. The table \"{$this->table}\" doesn't exists in database \"{$this->database}\" or file access denied.");
568568
}
569569

570570
$json_array = $this->cache->get($table_path);
@@ -815,15 +815,15 @@ protected function _insert($data)
815815
$rows = $this->parsedQuery['extensions']['in'];
816816
foreach ((array)$rows as $row) {
817817
if (!in_array($row, $data['prototype'], FALSE)) {
818-
throw new Exception("Can't insert data in the table \"{$this->table}\". The column \"{$row}\" doesn't exist.");
818+
throw new Exception("JSONDB Error: Can't insert data in the table \"{$this->table}\". The column \"{$row}\" doesn't exist.");
819819
}
820820
}
821821
}
822822

823823
$values_nb = count($this->parsedQuery['parameters']);
824824
$rows_nb = count($rows);
825825
if ($values_nb !== $rows_nb) {
826-
throw new Exception("Can't insert data in the table \"{$this->table}\". Invalid number of parameters (given \"{$values_nb}\" values to insert in \"{$rows_nb}\" columns).");
826+
throw new Exception("JSONDB Error: Can't insert data in the table \"{$this->table}\". Invalid number of parameters (given \"{$values_nb}\" values to insert in \"{$rows_nb}\" columns).");
827827
}
828828
$current_data = $data['data'];
829829
$ai_id = (int)$data['properties']['last_insert_id'];
@@ -836,7 +836,7 @@ protected function _insert($data)
836836
foreach ((array)$this->parsedQuery['extensions']['and'] as $values) {
837837
$values_nb = count($values);
838838
if ($values_nb !== $rows_nb) {
839-
throw new Exception("Can't insert data in the table \"{$this->table}\". Invalid number of parameters (given \"{$values_nb}\" values to insert in \"{$rows_nb}\" columns).");
839+
throw new Exception("JSONDB Error: Can't insert data in the table \"{$this->table}\". Invalid number of parameters (given \"{$values_nb}\" values to insert in \"{$rows_nb}\" columns).");
840840
}
841841
$to_add = array('#rowid' => $this->_getLastValidRowID(array_merge($current_data, $insert), FALSE) + 1);
842842
foreach ((array)$values as $key => $value) {
@@ -880,7 +880,7 @@ protected function _insert($data)
880880
if ($pk_error) {
881881
$values = implode(', ', $value);
882882
$keys = implode(', ', $data['properties']['primary_keys']);
883-
throw new Exception("Can't insert value. Duplicate values \"{$values}\" for primary keys \"{$keys}\".");
883+
throw new Exception("JSONDB Error: Can't insert value. Duplicate values \"{$values}\" for primary keys \"{$keys}\".");
884884
}
885885
}
886886
}
@@ -893,7 +893,7 @@ protected function _insert($data)
893893
$value = array_intersect_key($value, array($uk => $uk));
894894
$uk_error = $uk_error || (!empty($item[$uk]) && ($value === $array_data));
895895
if ($uk_error) {
896-
throw new Exception("Can't insert value. Duplicate values \"{$value[$uk]}\" for unique key \"{$uk}\".");
896+
throw new Exception("JSONDB Error: Can't insert value. Duplicate values \"{$value[$uk]}\" for unique key \"{$uk}\".");
897897
}
898898
}
899899
}
@@ -932,15 +932,15 @@ protected function _replace($data)
932932
$rows = $this->parsedQuery['extensions']['in'];
933933
foreach ((array)$rows as $row) {
934934
if (!in_array($row, $data['prototype'], FALSE)) {
935-
throw new Exception("Can't insert data in the table \"{$this->table}\". The column \"{$row}\" doesn't exist.");
935+
throw new Exception("JSONDB Error: Can't insert data in the table \"{$this->table}\". The column \"{$row}\" doesn't exist.");
936936
}
937937
}
938938
}
939939

940940
$values_nb = count($this->parsedQuery['parameters']);
941941
$rows_nb = count($rows);
942942
if ($values_nb !== $rows_nb) {
943-
throw new Exception("Can't insert data in the table \"{$this->table}\". Invalid number of parameters (given \"{$values_nb}\" values to insert in \"{$rows_nb}\" columns).");
943+
throw new Exception("JSONDB Error: Can't insert data in the table \"{$this->table}\". Invalid number of parameters (given \"{$values_nb}\" values to insert in \"{$rows_nb}\" columns).");
944944
}
945945
$current_data = $data['data'];
946946
$insert = array();
@@ -978,7 +978,7 @@ protected function _replace($data)
978978
if ($pk_error) {
979979
$values = implode(', ', $item);
980980
$keys = implode(', ', $data['properties']['primary_keys']);
981-
throw new Exception("Can't replace value. Duplicate values \"{$values}\" for primary keys \"{$keys}\".");
981+
throw new Exception("JSONDB Error: Can't replace value. Duplicate values \"{$values}\" for primary keys \"{$keys}\".");
982982
}
983983
}
984984
}
@@ -991,7 +991,7 @@ protected function _replace($data)
991991
$item = array_intersect_key($item, array($uk => $uk));
992992
$uk_error = $uk_error || (!empty($item[$uk]) && ($item === $array_data));
993993
if ($uk_error) {
994-
throw new Exception("Can't replace value. Duplicate values \"{$item[$uk]}\" for unique key \"{$uk}\".");
994+
throw new Exception("JSONDB Error: Can't replace value. Duplicate values \"{$item[$uk]}\" for unique key \"{$uk}\".");
995995
}
996996
}
997997
}
@@ -1084,13 +1084,13 @@ protected function _update($data)
10841084
}
10851085

10861086
if (!array_key_exists('with', $this->parsedQuery['extensions'])) {
1087-
throw new Exception("Can't execute the \"update()\" query without values.");
1087+
throw new Exception("JSONDB Error: Can't execute the \"update()\" query without values.");
10881088
}
10891089

10901090
$fields_nb = count($this->parsedQuery['parameters']);
10911091
$values_nb = count($this->parsedQuery['extensions']['with']);
10921092
if ($fields_nb !== $values_nb) {
1093-
throw new Exception("Can't execute the \"update()\" query. Invalid number of parameters (trying to update \"{$fields_nb}\" columns with \"{$values_nb}\" values).");
1093+
throw new Exception("JSONDB Error: Can't execute the \"update()\" query. Invalid number of parameters (trying to update \"{$fields_nb}\" columns with \"{$values_nb}\" values).");
10941094
}
10951095

10961096
$values = array_combine($this->parsedQuery['parameters'], $this->parsedQuery['extensions']['with']);
@@ -1103,7 +1103,7 @@ protected function _update($data)
11031103
if ($pk_error) {
11041104
$v = implode(', ', $array_data);
11051105
$k = implode(', ', $data['properties']['primary_keys']);
1106-
throw new Exception("Can't update value. Duplicate values \"{$v}\" for primary keys \"{$k}\".");
1106+
throw new Exception("JSONDB Error: Can't update value. Duplicate values \"{$v}\" for primary keys \"{$k}\".");
11071107
}
11081108
}
11091109

@@ -1114,7 +1114,7 @@ protected function _update($data)
11141114
$array_data = array_intersect_key($array_data, array($uk => $uk));
11151115
$uk_error = $uk_error || (!empty($item[$uk]) && ($item === $array_data));
11161116
if ($uk_error) {
1117-
throw new Exception("Can't replace value. Duplicate values \"{$item[$uk]}\" for unique key \"{$uk}\".");
1117+
throw new Exception("JSONDB Error: Can't replace value. Duplicate values \"{$item[$uk]}\" for unique key \"{$uk}\".");
11181118
}
11191119
}
11201120
}
@@ -1247,7 +1247,7 @@ protected function _filter($data, $filters)
12471247
}
12481248
foreach ((array)$result as $line) {
12491249
if (!array_key_exists($filter['field'], $line)) {
1250-
throw new Exception("The field \"{$filter['field']}\" doesn't exists in the table \"{$this->table}\".");
1250+
throw new Exception("JSONDB Error: The field \"{$filter['field']}\" doesn't exists in the table \"{$this->table}\".");
12511251
}
12521252
switch ($filter['operator']) {
12531253
case '<':
@@ -1300,7 +1300,7 @@ protected function _filter($data, $filters)
13001300
break;
13011301

13021302
default:
1303-
throw new Exception("The operator \"{$filter['operator']}\" is not supported. Try to use one of these operators: \"<\", \"<=\", \"=\", \">=\", \">\", \"<>\", \"!=\", \"%=\" or \"%!\".");
1303+
throw new Exception("JSONDB Error: The operator \"{$filter['operator']}\" is not supported. Try to use one of these operators: \"<\", \"<=\", \"=\", \">=\", \">\", \"<>\", \"!=\", \"%=\" or \"%!\".");
13041304
}
13051305
}
13061306
$result = $temp;

0 commit comments

Comments
 (0)