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);
|
2021-04-14 15:17:01 +00:00
|
|
|
|
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
|
|
|
|
2024-12-15 21:31:57 +00:00
|
|
|
public function __construct(string $msgID = "", $vars = null, ?\Throwable $e = null) {
|
2021-01-22 03:44:22 +00:00
|
|
|
if ($msgID === "") {
|
|
|
|
assert($e !== null, new \Exception("Expecting Picofeed or Guzzle exception when no message specified."));
|
|
|
|
if ($e instanceof BadResponseException) {
|
|
|
|
$msgID = self::HTTP_ERROR_MAP[$e->getCode()] ?? "transmissionError";
|
|
|
|
} elseif ($e instanceof TooManyRedirectsException) {
|
|
|
|
$msgID = "maxRedirect";
|
|
|
|
} elseif ($e instanceof GuzzleException) {
|
|
|
|
$msg = $e->getMessage();
|
|
|
|
if (preg_match("/^Error creating resource:/", $msg)) {
|
|
|
|
// PHP stream error; the class of error is ambiguous
|
|
|
|
$msgID = "transmissionError";
|
|
|
|
} elseif (preg_match("/^cURL error (\d+):/", $msg, $match)) {
|
|
|
|
$msgID = self::CURL_ERROR_MAP[(int) $match[1]] ?? "internalError";
|
|
|
|
} else {
|
2024-07-09 18:58:31 +00:00
|
|
|
// Fallback for future Guzzle exceptions we may not know about
|
|
|
|
$msgID = "internalError"; // @codeCoverageIgnore
|
2021-01-22 03:44:22 +00:00
|
|
|
}
|
|
|
|
} elseif ($e instanceof PicoFeedException) {
|
|
|
|
$className = get_class($e);
|
|
|
|
// Convert the exception thrown by PicoFeed to the one to be thrown here.
|
|
|
|
$msgID = preg_replace('/^PicoFeed\\\(?:Client|Parser|Reader)\\\([A-Za-z]+)Exception$/', '$1', $className);
|
|
|
|
// If the message ID doesn't change then it's unknown.
|
|
|
|
$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-23 22:07:20 +00:00
|
|
|
}
|
2020-01-21 13:42:38 +00:00
|
|
|
}
|
2021-01-22 03:44:22 +00:00
|
|
|
parent::__construct($msgID, $vars, $e);
|
2017-02-19 22:02:03 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|