Example #1
0
 public function it_should_catch_any_exception_and_convert_it_to_error(RequestParserInterface $requestParser, MethodDispatcherInterface $methodDispatcher, ResponseSerializerInterface $responseSerializer, ErrorResponseFactory $errorResponseFactory)
 {
     $requestParser->parse('{"jsonrpc":"2.0","method":"calculator.calculateProfit","params":{"month":"January"},"id":"calculate-january-profit"}')->willReturn(new Request('2.0', 'calculate-january-profit', 'calculator.calculateProfit', ['month' => 'January']));
     $methodDispatcher->dispatch('calculator.calculateProfit', ['month' => 'January'])->will(function () {
         throw new Exception('test', 32);
     });
     $errorResponseFactory->createForException(new Exception('test', 32), 'calculate-january-profit')->willReturn(ErrorResponse::applicationDefinedError('calculate-january-profit', 'test', 32));
     $responseSerializer->serializeResponse('2.0', new ErrorResponse('calculate-january-profit', new Error('test', 32)))->willReturn('{"jsonrpc":"2.0","error": {"code": 32, "message": "test"}')->shouldBeCalled();
     $this->handle('{"jsonrpc":"2.0","method":"calculator.calculateProfit","params":{"month":"January"},"id":"calculate-january-profit"}')->shouldBeLike('{"jsonrpc":"2.0","error": {"code": 32, "message": "test"}');
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function handle($requestContent)
 {
     $request = null;
     try {
         $request = $this->requestParser->parse($requestContent);
         $result = $this->methodDispatcher->dispatch($request->getMethod(), $request->getParams());
         $response = new SuccessResponse($request->getId(), $result);
         $responseContent = $this->responseSerializer->serializeResponse($request->getVersion(), $response);
     } catch (\Exception $e) {
         $response = $this->errorResponseFactory->createForException($e, $request ? $request->getId() : null);
         $responseContent = $this->responseSerializer->serializeResponse($request ? $request->getVersion() : '2.0', $response);
     }
     return $responseContent;
 }