/** * General response creator for exceptions * * @param Entity\Request $request * @param \Exception $e * @return Entity\Response */ public function createResponseFromException(Entity\Request $request, \Exception $e) { if ($e instanceof Exception\ExceptionInterface === false) { $e = new Exception\InvalidRequest('Internal error', static::INTERNAL_ERROR, $e); } // Create required objects $response = new Entity\Response(); $error = new Entity\Error(); // Attach error the response object $response->setError($error); $response->setJsonrpc($request->getJsonrpc()); // Set the error data $error->setCode($e->getCode()); $error->setMessage($e->getMessage()); // The error code $code = $e->getCode(); // Set the request ID based on error code if ($code !== static::PARSE_ERROR && $code !== static::INVALID_REQUEST) { $response->setId($request->getId()); } $data = array(); if ($response->getId() !== null && $code !== static::METHOD_NOT_FOUND) { $methodName = strtolower($request->getMethod()); $namespace = 'global'; if (strpos($methodName, '.') !== false) { list($namespace, $methodName) = explode('.', $methodName, 2); } if ($this->handlers->offsetExists($namespace)) { $methods = $this->getHandlerReflectionMaker()->reflect($this->handlers->offsetGet($namespace)); if ($methods->offsetExists($methodName)) { $method = $methods->offsetGet($methodName); $data['parameters'] = $method->getParameters()->getArrayCopy(); } } } // In debug mode we print more data if ($this->debugMode === true) { $data['exceptions'] = array(); $data['backtrace'] = $e->getTrace(); while (null !== ($e = $e->getPrevious())) { $data['exceptions'][] = array($e->getCode() => $e->getMessage()); } } // Set all the data $error->setData($data); return $response; }