diff --git a/lib/contents.php b/lib/contents.php index 0cf1ab9c..26f44366 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -11,6 +11,75 @@ * @link https://github.com/rss-bridge/rss-bridge */ + +/** + * Exception class to handle all errors, when executing getContents + */ +class GetContentsException extends \Exception { + public function __construct($details, $code = 0, Throwable $previous = null) { + $message = trim($this->getMessageHeading() . "\n$details"); + + $lastError = error_get_last(); + if($lastError !== null) + $message .= "\nLast PHP Error: " . $lastError['message']; + + parent::__construct($message, $code, $previous); + } + + protected function getMessageHeading() { + return 'Could not get contents'; + } +} + +/** + * Exception class to handle HTTP responses with Cloudflare challenges + **/ +class CloudflareChallengeException extends \Exception { + public function __construct($code = 0, Throwable $previous = null) { + if (!$message) { + $message = <<responseCode = $responseCode; + $this->responseHeaders = $responseHeaders; + $this->responseBody = $responseBody; + + parent::__construct('', $responseCode, $previous); + } + + public function getResponseCode() { + return $this->responseCode; + } + + public function getResponseHeaders() { + return $this->responseHeaders; + } + + public function getResponseBody() { + return $this->responseBody(); + } +} + /** * Gets contents from the Internet. * @@ -189,22 +258,14 @@ function getContents($url, $header = array(), $opts = array(), $returnHeader = f break; default: if(array_key_exists('Server', $finalHeader) && strpos($finalHeader['Server'], 'cloudflare') !== false) { - returnServerError(<<< EOD -The server responded with a Cloudflare challenge, which is not supported by RSS-Bridge! -If this error persists longer than a week, please consider opening an issue on GitHub! -EOD - ); + throw new CloudflareChallengeException($errorCode); } - $lastError = error_get_last(); - if($lastError !== null) - $lastError = $lastError['message']; - returnError(<<