SHARE
TWEET

PHP: error and exception handler for Ajax driven sites/JSON

planzelle Jun 7th, 2016 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function BaseHandler($errLevel, $errMsg, $errFile, $errLine) {
  2.     $obj = new stdClass();
  3.     $obj->status = 1;
  4.     $obj->message = $errMsg;
  5.     $obj->stamp = microtime(true);
  6.     $obj->result = array('errLevel'=>$errLevel,'errMsg'=>$errMsg, 'errFile'=>$errFile, 'errLine'=>$errLine);
  7.     $obj->resultCount = count($obj->result);
  8.     $obj->html = '';
  9.     header('Content-Type: application/json;charset=utf-8');
  10.     echo json_encode($obj);
  11.     die();
  12. }
  13.  
  14. function ErrorHandler($errLevel, $errMsg, $errFile, $errLine) {
  15.     BaseHandler($errLevel, $errMsg, $errFile, $errLine);
  16. }
  17.  
  18. function ExceptionHandler(Exception $e) {
  19.     BaseHandler($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
  20.  
  21. }
  22.  
  23.  
  24. set_error_handler("ErrorHandler");
  25. set_exception_handler("ExceptionHandler");
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top