yajra / pdo-via-oci8

PHP PDO_OCI functions via OCI8 extension
Other
88 stars 61 forks source link

Request: improved support for error handling #50

Open cartbeforehorse opened 6 years ago

cartbeforehorse commented 6 years ago

Hi. I know this isn't an "issue" as such, but I was wondering if you'd be able to update the exception-handler class with a few helper-functions that make it easier to fetch and manage the error, making it easier to debug the faulty SQL (or PL/SQL) code that threw the exception, and also make it easier to feed back the correct information to the user.

I realise that I can extend the exception myself, but just thought it would be easier to share with others this way.

class Oci8Exception extends PDOException
{
    public function getHtmlMessage() {
        $msg = $this->getMessage();
        $msg = str_replace (chr(13).chr(10), '</p><p>', $msg);
        $msg = str_replace (chr(10), '<br />', $msg);
        return '<p>' . $msg . '</p>';
    }

    public function getOciErrorStack() {
        $msg = $this->getMessage();
        preg_match ('/(Error Message\s*:[^\r]*)\r\n/s', $msg, $out);
        return $out[1];
    }
    public function getOciErrorMsg() {
        $msg = $this->getOciErrorStack();
        preg_match ('/Error Message\s*:\s*ORA-[0-9]{5}:\s*(.*)/', $msg, $out);
        return $out[1];
    }

    public function getOriginalStatement ($rtnstr =2) {
        $msg = $this->getMessage();
        preg_match ('/(Statement\s*:)\s*([^\r]*)/', $msg, $out);
        return $out[$rtnstr];
    }

    public function getBindings ($rtnstr =2) {
        $msg = $this->getMessage();
        preg_match ('/(Bindings\s*:)\s*([^\r]*)/', $msg, $out);
        return $out[$rtnstr];
    }
}
yajra commented 6 years ago

@cartbeforehorse this looks like a good idea. Can you please submit a PR given the following snippets you provided? Thanks!