1
我有一個處理的對象這樣的JSON-RPC處理函數:爲什麼SQLite3錯誤不是例外?
class Service {
public function sqlite_query($token, $filename, $query) {
if (!$this->valid_token($token)) {
throw new Exception("Access Denied: Invalid Token");
}
$db = new SQLite($filename);
$res = $db->query($query);
if ($res) {
if (preg_match("/^\s*INSERT|UPDATE|DELETE|ALTER|CREATE/i", $query)) {
return $db->rowAffected();
} else {
return $res->fetchAll();
}
} else {
throw new Error("Coudn't open file");
}
}
}
SQLite是調用SQLite的2或3碼捕獲所有異常的一類,但是當我嘗試執行無效的SQL我也不例外但PHP錯誤處理此代碼:
set_error_handler('error_handler');
ini_set('display_errors', 1);
ini_set('track_errors', 1);
ob_start();
function error_handler($err, $message, $file, $line) {
global $stop;
$stop = true;
$content = explode("\n", file_get_contents($file));
header('Content-Type: application/json');
$id = extract_id();
$error = array(
"code" => 100,
"message" => "Server error",
"error" => array(
"name" => "PHPErorr",
"code" => $err,
"message" => $message,
"file" => $file,
"at" => $line,
"line" => $content[$line-1]));
ob_end_clean();
echo response(null, $id, $error);
exit();
}
有沒有辦法讓SQLite拋出異常?
將PDO與'$ db-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION)一起使用;'工作。 – jcubic