/** Checks if safe mode will prevent of throwing an exception * also verifies if invalid JSON response exception will * contain correct error code, message and details */ public function testSafeModeEnabled() { $handler = new Postmen('', ''); $curl_response = $this->headers . 'NOT VALID JSON, BUT EXCEPTION IS NOT GOING TO BE RAISED'; $mock_curl = new PHPUnit_Extensions_MockFunction('curl_exec', $handler); $mock_curl->expects($this->atLeastOnce())->will($this->returnValue($curl_response)); $mock_curl_length = new PHPUnit_Extensions_MockFunction('curl_getinfo', $handler); $mock_curl_length->expects($this->atLeastOnce())->will($this->returnValue($this->headers_length)); $result = $handler->get('labels', '', NULL, array('safe' => true)); $exception = $handler->getError(); $this->assertNull($result); $this->assertEquals($exception->getCode(), 500); $this->assertEquals($exception->isRetryable(), false); $this->assertEquals($exception->getMessage(), 'Something went wrong on Postmen\'s end'); $this->assertEquals(count($exception->getDetails()), 0); }
// exception which will inform you what // is wrong with your payload echo "using try ... catch\n"; try { $api = new Postmen('THIS IS NOT A VALID API KEY', $region); $result = $api->get('labels'); } catch (exception $e) { echo "ERROR:\n"; echo $e->getCode() . "\n"; // error code echo $e->getMessage() . "\n"; // error message print_r($e->getDetails()); // error details } // we also can enable the safe mode, // this way try{}catch(){} is no // longer required echo "using safe mode\n"; $api = new Postmen('THIS IS NOT A VALID API KEY', $region); $result = $api->get('labels', NULL, array('safe' => true)); if (!$result) { $e = $api->getError(); echo "ERROR:\n"; echo $e->getCode() . "\n"; // error code echo $e->getMessage() . "\n"; // error message print_r($e->getDetails()); // error details }