/** * An overview of how to use the JsonRpcResponse class. * @test */ public function synopsis() { /* Needs to be based on a related request */ $request = new JsonRpcRequest('MyService', (object) array('jsonrpc' => '2.0', 'method' => 'remoteProcedure', 'params' => array(1, 2, 3), 'id' => 'identifier')); $response = new JsonRpcResponse('object, array, or scalar', null, $request); $obj = $response->getResponseObject(); $this->assertEquals("2.0", $obj->jsonrpc, "Responds with the same version"); $this->assertEquals('object, array, or scalar', $obj->result, "Result is passed through"); $this->assertEquals("identifier", $obj->id, "Request ID is passed back"); /* Notifications generate no response */ $request = new JsonRpcRequest('MyService', (object) array('method' => 'notifyProcedure')); $response = new JsonRpcResponse("anything", null, $request); $this->assertEquals("", $response->getResponseObject()); /* An error passes back a message and a code */ $request = new JsonRpcRequest('MyService', (object) array('method' => 'any', 'id' => 123)); $response = new JsonRpcResponse(null, new Exception("ex", 123), $request); $obj = $response->getResponseObject(); $this->assertEquals(123, $obj->error->code); $this->assertEquals("ex", $obj->error->message); }
/** * Provides the handler with an opportunity to perform any last minute * error handling logic. The returned value will be serialized by the * handler's encoder. * * @param Exception $e The exception that was thrown. * @return mixed Returns a serializable value that will be encoded and returned * to the client. */ public function handleException(Exception $e) { if ($e->getCode() > -32000) { /* Don't pass through internal errors in case there's something sensitive. */ $response = new JsonRpcResponse(null, new Exception("Internal Error", self::ERR_INTERNAL_ERROR)); } else { /* JSON-RPC errors (<= -32000) can be passed on. */ $response = new JsonRpcResponse(null, $e, null); } return $response->getResponseObject(); }