/** * Makes an HTTP request to the provided URL * * @param Boolean - should this throw an exception? (Default = TRUE) * * @return **Object** - * Example response object * <code> * { * status: "http_response_header", * route: "STRING", * errors: "ARRAY/OBJECT/STRING", * data: "ARRAY/OBJECT/STRING" * } * </code> * * @since 2015-11-05 * @author Deac Karns <*****@*****.**> * @author Wesley Dekkers <*****@*****.**> */ public function run($throw_exception = TRUE) { $contents = json_decode(file_get_contents($this->url, false, $this->context)); $result = new \stdClass(); $result->status = $http_response_header[0]; $result->route = $this->url; $result->errors = ""; $result->success = TRUE; if (!strpos($http_response_header[0], '200')) { if (is_object($contents) && !empty($contents->errors)) { foreach ($contents->errors as $error) { \Rhonda\Error::add_summary_item($error); } } else { \Rhonda\Error::add_summary_item(implode(" - ", $http_response_header)); } $result->errors = \Rhonda\Error::summary(); $result->success = FALSE; // When hard error End call here if ($throw_exception) { throw new \Exception("Rhonda API Gateway detected an error"); } } $result->data = $contents->data; return $result; }
/** * Package incoming data along with the error summary from \Ronda\Error::summary() * * In order an error to be populated it uses \Rhonda\Error::add_summary_item(); and \Rhonda\Error::summary(); * * @param Parameter - The result you want to return * * @example * <code> * \Rhonda\Response:: package($result); * </code> * * @return Return - **Object** * { * "errors": ARRAY of error OBJECTS, * "data": STRING/OBJECT/ARRAY, * "request": { * "url": "REQUEST URL", * "method": "POST/PUT/DELETE/GET/PATCH.. etcetera" * } * } * * @since 2016-07-29 * @author Deac Karns <*****@*****.**> * @author Wesley Dekkers <*****@*****.**> **/ public static function package($data, $response_code = false) { $result = new \stdClass(); $result->errors = \Rhonda\Error::summary(); $result->data = $data; $result->request = new \stdClass(); $result->request->url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $result->request->method = $_SERVER['REQUEST_METHOD']; // When error not empty and data not empty change headers 206 if (!$response_code && !empty($result->errors) && !empty($data)) { $response_code = 209; } // when error not empty and data empty return 400 if (!$response_code && !empty($result->errors) && empty($data)) { $response_code = 400; } \Rhonda\Headers::set_response_code($response_code); return $result; }
try { throw new Exception("Demo Error Exception 2"); } catch (\Exception $e) { $error = new \Rhonda\Error(); echo $error->handle($e); } echo "</br>"; try { throw new Exception("Demo Error Exception 3", 404); } catch (\Exception $e) { $error = new \Rhonda\Error(); echo $error->handle($e); } echo "</br>"; try { throw new Exception("Demo Error Exception 4"); } catch (\Exception $e) { $error = new \Rhonda\Error(); echo $error->handle($e, 402); } \Rhonda\Error::deprecation_warning("message", "http://alternate/route"); echo "</br>"; $error = new \stdClass(); $error->code = 444; $error->message = "test message"; \Rhonda\Error::add_summary_item($error); \Rhonda\Error::add_summary_item($error); echo "<pre>"; print_r(\Rhonda\Error::summary()); echo "</pre>"; echo "</br>";