|
<?php |
|
|
|
/** |
|
* IPU protocol errors |
|
*/ |
|
define('IPU_UNKNOWN_ERROR', 0); |
|
define('IPU_TOKEN_MISSING', 1); |
|
define('IPU_MESSAGE_MISSING', 2); |
|
define('IPU_PRIVATE_KEY_UNAVAILABLE', 3); |
|
define('IPU_JSON_ERROR', 4); |
|
define('IPU_UNKNOWN_ACTION', 5); |
|
define('IPU_PHP_EXCEPTION', 6); |
|
|
|
/** |
|
* Convenience function for reporting IPU errors |
|
*/ |
|
function reportIPUError($code, $payload = NULL) { |
|
$error = new stdClass;; |
|
$error->code = (integer) $code; |
|
if ($payload !== NULL) { |
|
$error->payload = $payload; |
|
} |
|
switch ($error->code) { |
|
case IPU_TOKEN_MISSING: |
|
$error->message = 'Parameter \'token\' is missing'; |
|
break; |
|
case IPU_MESSAGE_MISSING: |
|
$error->message = 'Parameter \'message\' is missing'; |
|
break; |
|
case IPU_PRIVATE_KEY_UNAVAILABLE: |
|
$error->message = 'Could not load private key'; |
|
break; |
|
case IPU_JSON_ERROR; |
|
$error->message = 'JSON error'; |
|
$error->json = new stdClass; |
|
$error->json->code = json_last_error(); |
|
$error->json->message = json_last_error_msg(); |
|
break; |
|
case IPU_UNKNOWN_ACTION; |
|
$error->message = 'Unknown action'; |
|
break; |
|
case IPU_PHP_EXCEPTION; |
|
$error->message = 'A PHP exception or error occured'; |
|
break; |
|
default: |
|
$error->code = UNKNOWN_ERROR; |
|
$error->message = 'Unknown error'; |
|
} |
|
global $response; |
|
$response->errors[] = $error; |
|
} |
|
|
|
/** |
|
* Error handler, passes flow over the exception logger with new ErrorException. |
|
*/ |
|
function logError($num, $str, $file, $line, $context = null) { |
|
logException(new ErrorException($str, 0, $num, $file, $line)); |
|
} |
|
|
|
/** |
|
* Uncaught exception handler. |
|
*/ |
|
function logException(Exception $e) { |
|
$payload = new stdClass; |
|
$payload->exception->code = $e->getCode(); |
|
$payload->exception->message = $e->getMessage(); |
|
$payload->exception->file = $e->getFile(); |
|
$payload->exception->line = $e->getLine(); |
|
reportIPUError(IPU_PHP_EXCEPTION, $payload); |
|
} |
|
|
|
/** |
|
* Checks for a fatal error, work around for set_error_handler not working on fatal errors. |
|
*/ |
|
function checkForFatal() { |
|
$error = error_get_last(); |
|
if ($error['type'] === E_ERROR) { |
|
logError($error['type'], $error['message'], $error['file'], $error['line']); |
|
} |
|
} |
|
|
|
// register functions |
|
register_shutdown_function('checkForFatal'); |
|
set_error_handler('logError'); |
|
set_exception_handler('logException'); |