public function addRequest(EntityRequest $request, $callable = null) { $id = $request->getId(); if ($id === null && $callable !== null) { throw new Exception\InvalidArgument('Request ID is NULL. This means no response for server. Cannot ' . 'attach callable to this request.', 1); } if ($id !== null && array_key_exists($id, $this->ids)) { throw new Exception\RuntimeException('Cannot add same id `' . $id . '` twice to the stack!'); } // Add the callable to the stack if ($id !== null) { $this->ids[$id] = $callable; } return parent::offsetSet(null, $request); }
/** * 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; }