CodeIgniter Forums
JSon error when database is down - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: JSon error when database is down (/showthread.php?tid=74411)



JSon error when database is down - ejimenez - 09-19-2019

I have this snippet of code

Code:
$empresa = $this->dbAPI->table('api_empresas');
$usuario = $empresa->select("id,{$modo}_usuario as usu,{$modo}_contrasena as pwd,{$modo}_pin as pin," .
'access_token,expires_in,refresh_token,refresh_expires_in,hora')
->getWhere(['activo' => true, 'api_key' => $api_key])->getRow();

it runs well when database server is available.

The problem is that when the database server is offline for whatever reason, the first line does not fail but the second one returns a message related to a JSon conversion problem which I have not found a way to catch.

How does this issue should be properly addressed?


RE: JSon error when database is down - titounnes - 09-19-2019

before getRow(), you must check it.
$result= ..... ->getWhere(....);
if($result){
return $result->getRow();
}
//your exception code


RE: JSon error when database is down - ejimenez - 09-20-2019

(09-19-2019, 09:11 PM)titounnes Wrote: before getRow(), you must check it.
$result= ..... ->getWhere(....);
if($result){
return $result->getRow();
}
//your exception code

I did wht you suggested, but got this in return

<br />
<b>Fatal error</b>: Uncaught CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: &quot;Malformed UTF-8 characters, possibly incorrectly encoded&quot;. in D:\xampp\htdocs\SGAAPI\system\Format\Exceptions\FormatException.php:9
Stack trace:
#0 D:\xampp\htdocs\SGAAPI\system\Format\JSONFormatter.php(66): CodeIgniter\Format\Exceptions\FormatException::forInvalidJSON('Malformed UTF-8...')
#1 D:\xampp\htdocs\SGAAPI\system\API\ResponseTrait.php(386): CodeIgniter\Format\JSONFormatter-&gt;format(Array)
#2 D:\xampp\htdocs\SGAAPI\system\API\ResponseTrait.php(124): CodeIgniter\Debug\Exceptions-&gt;format(Array)
#3 D:\xampp\htdocs\SGAAPI\system\Debug\Exceptions.php(164): CodeIgniter\Debug\Exceptions-&gt;respond(Array, 500)
#4 [internal function]: CodeIgniter\Debug\Exceptions-&gt;exceptionHandler(Object(mysqli_sql_exception))
#5 {main}
thrown in <b>D:\xampp\htdocs\SGAAPI\system\Format\Exceptions\FormatException.php</b> on line <b>9</b><br />
{
"title": "ErrorException",
"type": "ErrorException",
"code": 500,
"message": "Uncaught CodeIgniter\\Format\\Exceptions\\FormatException: Failed to parse json string, error: \"Malformed UTF-8 characters, possibly incorrectly encoded\". in D:\\xampp\\htdocs\\SGAAPI\\system\\Format\\Exceptions\\FormatException.php:9\nStack trace:\n#0 D:\\xampp\\htdocs\\SGAAPI\\system\\Format\\JSONFormatter.php(66): CodeIgniter\\Format\\Exceptions\\FormatException::forInvalidJSON('Malformed UTF-8...')\n#1 D:\\xampp\\htdocs\\SGAAPI\\system\\API\\ResponseTrait.php(386): CodeIgniter\\Format\\JSONFormatter->format(Array)\n#2 D:\\xampp\\htdocs\\SGAAPI\\system\\API\\ResponseTrait.php(124): CodeIgniter\\Debug\\Exceptions->format(Array)\n#3 D:\\xampp\\htdocs\\SGAAPI\\system\\Debug\\Exceptions.php(164): CodeIgniter\\Debug\\Exceptions->respond(Array, 500)\n#4 [internal function]: CodeIgniter\\Debug\\Exceptions->exceptionHandler(Object(mysqli_sql_exception))\n#5 {main}\n thrown",
"file": "D:\\xampp\\htdocs\\SGAAPI\\system\\Format\\Exceptions\\FormatException.php",
"line": 9,
"trace": [
{
"function": "shutdownHandler",
"class": "CodeIgniter\\Debug\\Exceptions",
"type": "->",
"args": []
}
]
}


RE: JSon error when database is down - ejimenez - 09-20-2019

I think it could be the debugbar choking on the database not available exception.