public function handle() { try { // Make sure all warnings are properly intercepted set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) { error_log("{$errstr} in {$errfile} on line {$errline}"); throw new Exception("PHP error at {$errfile}:{$errline}: {$errstr}"); }); if ($this->shouldValidteSession()) { $this->validateSession(); } $args = $this->parseRequest(); try { $args = $this->getRequestValidator()->exec("REQUEST", $args); } catch (Validator\ValidationException $e) { throw new Exception($e->getMessage(), self::STATUS_BAD_REQUEST); } $result = null; Model::beginTransaction(); try { $result = $this->handleRequest($args); $result = $this->getResponseValidator()->exec("RESPONSE", $result); Model::commitTransaction(); } catch (Validator\ValidationException $e) { Model::rollbackTransaction(); throw new Exception($e->getMessage(), self::STATUS_SERVER_ERROR); } catch (Exception $e) { Model::rollbackTransaction(); throw $e; } $this->sendResponse(self::STATUS_SUCCESS, $result); } catch (Exception $e) { $code = $e->getCode() ?: self::STATUS_SERVER_ERROR; $message = $e->getMessage(); $this->sendResponse($code, array(self::FIELD_ERROR_MESSAGE => $message)); } }