catfan/Medoo — pull request #172 by adriancrepaz (2014-12-05 22:27:45+00:00)
Title: throws PDOException. Fixed undefined $dsn.

I was browsing your Git and noticed your exception handling.

```php
catch (PDOException $e) {
throw new Exception($e->getMessage());
}
```

This isn't required, and also by re-throwing the exception, you're dropping trace and error code information.

`PDOException` ultimately extends off `Exception` (via `RuntimeException`).

**Exception**
```
string(46) "SQLSTATE[42000] [1049] Unknown database 'test'"

NULL

int(0)

string(41) "/Applications/MAMP/htdocs/Medoo/medoo.php"

int(146)

array(1) {
[0]=>
array(6) {
["file"]=>
string(39) "/Applications/MAMP/htdocs/medooTest.php"
["line"]=>
int(29)
["function"]=>
string(11) "__construct"
["class"]=>
string(5) "medoo"
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
array(5) {
["database_type"]=>
string(5) "mysql"
["database_name"]=>
string(4) "test"
["server"]=>
string(9) "localhost"
["username"]=>
string(4) "root"
["password"]=>
string(4) "root"
}
}
}
}

string(83) "#0 /Applications/MAMP/htdocs/medooTest.php(29): medoo->__construct(Array)
#1 {main}"
```

**PDOException**
```
string(46) "SQLSTATE[42000] [1049] Unknown database 'test'"

NULL

int(1049)

string(41) "/Applications/MAMP/htdocs/Medoo/medoo.php"

int(138)

array(2) {
[0]=>
array(6) {
["file"]=>
string(41) "/Applications/MAMP/htdocs/Medoo/medoo.php"
["line"]=>
int(138)
["function"]=>
string(11) "__construct"
["class"]=>
string(3) "PDO"
["type"]=>
string(2) "->"
["args"]=>
array(4) {
[0]=>
string(32) "mysql:host=localhost;dbname=test"
[1]=>
string(4) "root"
[2]=>
string(4) "root"
[3]=>
array(0) {
}
}
}
[1]=>
array(6) {
["file"]=>
string(39) "/Applications/MAMP/htdocs/medooTest.php"
["line"]=>
int(29)
["function"]=>
string(11) "__construct"
["class"]=>
string(5) "medoo"
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
array(5) {
["database_type"]=>
string(5) "mysql"
["database_name"]=>
string(4) "test"
["server"]=>
string(9) "localhost"
["username"]=>
string(4) "root"
["password"]=>
string(4) "root"
}
}
}
}

string(196) "#0 /Applications/MAMP/htdocs/Medoo/medoo.php(138): PDO->__construct('mysql:host=loca...', 'root', 'root', Array)
#1 /Applications/MAMP/htdocs/medooTest.php(29): medoo->__construct(Array)
#2 {main}"
```

You can see how `PDOException` gives a better trace and error code, using the appropiate getters.

----

You should consider setting ERRMODEs in PDO, this will help give better debuging results too.

I just modified the $commands array as a test, and passed an invalid query to exec() (line 144).
No error was thrown at all.

I enabled ERRMODE_EXCEPTION, and we got some useful info back.

**Exeption**
```
string(206) "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TEST' at line 1"

NULL

int(0)

string(41) "/Applications/MAMP/htdocs/Medoo/medoo.php"

int(149)

array(1) {
[0]=>
array(6) {
["file"]=>
string(39) "/Applications/MAMP/htdocs/medooTest.php"
["line"]=>
int(29)
["function"]=>
string(11) "__construct"
["class"]=>
string(5) "medoo"
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
array(5) {
["database_type"]=>
string(5) "mysql"
["database_name"]=>
string(5) "layer"
["server"]=>
string(9) "localhost"
["username"]=>
string(4) "root"
["password"]=>
string(4) "root"
}
}
}
}

string(83) "#0 /Applications/MAMP/htdocs/medooTest.php(29): medoo->__construct(Array)
#1 {main}"
```

**PDOException**

```
string(206) "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TEST' at line 1"

NULL

string(5) "42000"

string(41) "/Applications/MAMP/htdocs/Medoo/medoo.php"

int(145)

array(2) {
[0]=>
array(6) {
["file"]=>
string(41) "/Applications/MAMP/htdocs/Medoo/medoo.php"
["line"]=>
int(145)
["function"]=>
string(4) "exec"
["class"]=>
string(3) "PDO"
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
string(4) "TEST"
}
}
[1]=>
array(6) {
["file"]=>
string(39) "/Applications/MAMP/htdocs/medooTest.php"
["line"]=>
int(29)
["function"]=>
string(11) "__construct"
["class"]=>
string(5) "medoo"
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
array(5) {
["database_type"]=>
string(5) "mysql"
["database_name"]=>
string(5) "layer"
["server"]=>
string(9) "localhost"
["username"]=>
string(4) "root"
["password"]=>
string(4) "root"
}
}
}
}

string(152) "#0 /Applications/MAMP/htdocs/Medoo/medoo.php(145): PDO->exec('TEST')
#1 /Applications/MAMP/htdocs/medooTest.php(29): medoo->__construct(Array)
#2 {main}"
```

Notice the difference in the trace, we can now see the error was caused at exec(), rightly so, where-as your recycled Exception is showing the error falsely occured at `medoo->construct()`.

---

```php
$example = $database->select("accountss", [
"name",
"email"
]);

var_dump($example);
```

I ran this code as a test, notice the delibrate misspelling of the table name.
The response was `bool(false)`.

If this was a more complex query, it would be quite easy to presume there wasn't any data to be returned, and I might easily miss the typo. By handling exceptions the correct way, the incorrect table name would be glaring obvious.

For a Databse wrapper to be useful, it should give the same (or more) details about errors that occur, than the native class.

Adrian