2017-02-19 22:02:03 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-02-19 22:02:03 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Feed;
|
2017-02-19 22:02:03 +00:00
|
|
|
|
2020-01-23 22:07:20 +00:00
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
2020-01-21 13:42:38 +00:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2020-01-23 22:07:20 +00:00
|
|
|
use GuzzleHttp\Exception\TooManyRedirectsException;
|
|
|
|
use PicoFeed\PicoFeedException;
|
2020-01-21 13:42:38 +00:00
|
|
|
|
2017-03-28 04:12:12 +00:00
|
|
|
class Exception extends \JKingWeb\Arsse\AbstractException {
|
2020-03-01 23:32:01 +00:00
|
|
|
protected const CURL_ERROR_MAP = [1 => "invalidUrl",3 => "invalidUrl",5 => "transmissionError","connectionFailed","connectionFailed","transmissionError","forbidden","unauthorized","transmissionError","transmissionError","transmissionError","transmissionError","connectionFailed","connectionFailed","transmissionError","transmissionError","transmissionError","transmissionError","transmissionError","invalidUrl","transmissionError","transmissionError","transmissionError","transmissionError",28 => "timeout","transmissionError","transmissionError","transmissionError","transmissionError","transmissionError",35 => "invalidCertificate","transmissionError","transmissionError","transmissionError","transmissionError",45 => "transmissionError","unauthorized","maxRedirect",52 => "transmissionError","invalidCertificate","invalidCertificate","transmissionError","transmissionError",58 => "invalidCertificate","invalidCertificate","invalidCertificate","transmissionError","invalidUrl","transmissionError","invalidCertificate","transmissionError","invalidCertificate","forbidden","invalidUrl","forbidden","transmissionError",73 => "transmissionError","transmissionError",77 => "invalidCertificate","invalidUrl",90 => "invalidCertificate","invalidCertificate","transmissionError",94 => "unauthorized","transmissionError","connectionFailed"];
|
|
|
|
protected const HTTP_ERROR_MAP = [401 => "unauthorized",403 => "forbidden",404 => "invalidUrl",408 => "timeout",410 => "invalidUrl",414 => "invalidUrl",451 => "invalidUrl"];
|
2020-02-03 21:54:45 +00:00
|
|
|
|
2017-02-19 22:02:03 +00:00
|
|
|
public function __construct($url, \Throwable $e) {
|
2020-01-23 22:07:20 +00:00
|
|
|
if ($e instanceof BadResponseException) {
|
2020-02-03 21:54:45 +00:00
|
|
|
$msgID = self::HTTP_ERROR_MAP[$e->getCode()] ?? "transmissionError";
|
2020-01-23 22:07:20 +00:00
|
|
|
} elseif ($e instanceof TooManyRedirectsException) {
|
|
|
|
$msgID = "maxRedirect";
|
|
|
|
} elseif ($e instanceof GuzzleException) {
|
2020-02-03 21:54:45 +00:00
|
|
|
$msg = $e->getMessage();
|
|
|
|
if (preg_match("/^Error creating resource:/", $msg)) {
|
2020-01-23 22:07:20 +00:00
|
|
|
// PHP stream error; the class of error is ambiguous
|
2020-02-03 21:54:45 +00:00
|
|
|
$msgID = "transmissionError";
|
|
|
|
} elseif (preg_match("/^cURL error (\d+):/", $msg, $match)) {
|
|
|
|
$msgID = self::CURL_ERROR_MAP[(int) $match[1]] ?? "internalError";
|
2020-01-23 22:07:20 +00:00
|
|
|
} else {
|
2020-02-03 21:54:45 +00:00
|
|
|
$msgID = "internalError";
|
2020-01-23 22:07:20 +00:00
|
|
|
}
|
|
|
|
} elseif ($e instanceof PicoFeedException) {
|
2020-01-21 13:42:38 +00:00
|
|
|
$className = get_class($e);
|
|
|
|
// Convert the exception thrown by PicoFeed to the one to be thrown here.
|
2020-01-23 22:07:20 +00:00
|
|
|
$msgID = preg_replace('/^PicoFeed\\\(?:Client|Parser|Reader)\\\([A-Za-z]+)Exception$/', '$1', $className);
|
2020-01-21 13:42:38 +00:00
|
|
|
// If the message ID doesn't change then it's unknown.
|
2020-02-03 21:54:45 +00:00
|
|
|
$msgID = ($msgID !== $className) ? lcfirst($msgID) : "internalError";
|
2020-01-23 22:07:20 +00:00
|
|
|
} else {
|
2020-02-03 21:54:45 +00:00
|
|
|
$msgID = "internalError";
|
2020-01-21 13:42:38 +00:00
|
|
|
}
|
2017-02-19 22:02:03 +00:00
|
|
|
parent::__construct($msgID, ['url' => $url], $e);
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|