1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00
Arsse/lib/Feed/Exception.php

50 lines
3.6 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2021-04-14 15:17:01 +00:00
2017-03-28 04:12:12 +00:00
namespace JKingWeb\Arsse\Feed;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\TooManyRedirectsException;
use PicoFeed\PicoFeedException;
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"];
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";
} else {
$msgID = "internalError";
}
}
2021-01-22 03:44:22 +00:00
parent::__construct($msgID, $vars, $e);
}
2017-08-29 14:50:31 +00:00
}