lundi 21 décembre 2015

Why SQLite3 errors are not exceptions?

I have JSON-RPC handler function that handle objects like this:

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 is a class that call SQLite 2 or 3. The code catch all exceptions but when I try to execute invalid SQL I got not exception but php error handled by this code:

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();
}

Is there a way to make SQLite throw exception?

Aucun commentaire:

Enregistrer un commentaire