phptransmission

Use try/catch doesn't catch the exception


I have the following code :

<?php
require 'vendor/autoload.php';
use Transmission\Exception;

try{
    $transmission = new Transmission\Client($hostname, $port, $username, $password, $httpClientBuilder = null);
} catch (NetworkException $e) {
    $result = array("Error" => "Remote access is disabled");
}

I'm trying to catch the exception but I'm still getting the error :

<br />
<b>Fatal error</b>:  Uncaught Transmission\Exception\NetworkException: 403: Forbidden - Your IP Address is Not Whitelisted in /php-transmission-sdk/src/Exception/NetworkException.php:82
Stack trace:
#0 /php-transmission-sdk/src/HttpClient/Plugin/ExceptionThrower.php(44): Transmission\Exception\NetworkException::createByCode(403, '403: Forbidden ...')
#1 /php-transmission-sdk/vendor/php-http/httplug/src/Promise/HttpFulfilledPromise.php(34): Transmission\HttpClient\Plugin\ExceptionThrower-&gt;Transmission\HttpClient\Plugin\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /php-transmission-sdk/src/HttpClient/Plugin/ExceptionThrower.php(48): Http\Client\Promise\HttpFulfilledPromise-&gt;then(Object(Closure))
#3 /php-transmission-sdk/vendor/php-http/client-common/src/PluginClient.php(132): Transmission\HttpClient\Plugin\ExceptionThrower-&gt;handleRequest(Object(Nyholm\Psr7\Request), O in <b>/php-transmission-sdk/src/Exception/NetworkException.php</b> on line <b>82</b><br />

NetworkException.php

<?php

namespace Transmission\Exception;

/**
 * NetworkException
 */
class NetworkException extends \Exception
{
    public const CONFLICT = 409;

    public static $statusCodes = [
        // 4xx: Client Error - The request contains bad syntax or cannot be fulfilled.
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Payload Too Large',
        414 => 'Request-URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Requested Range Not Satisfiable',
        417 => 'Expectation Failed',
        418 => 'I\'m a teapot',
        421 => 'Misdirected Request',
        422 => 'Unprocessable Entity',
        423 => 'Locked',
        424 => 'Failed Dependency',
        426 => 'Upgrade Required',
        428 => 'Precondition Required',
        429 => 'Too Many Requests',
        431 => 'Request Header Fields Too Large',
        444 => 'Connection Closed Without Response',
        451 => 'Unavailable For Legal Reasons',
        499 => 'Client Closed Request',

        // 5xx: Server Error - The server failed to fulfill an apparently valid request.
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported',
        506 => 'Variant Also Negotiates',
        507 => 'Insufficient Storage',
        508 => 'Loop Detected',
        510 => 'Not Extended',
        511 => 'Network Authentication Required',
        599 => 'Network Connect Timeout Error',
    ];

    /**
     * Create Exception by Network Code.
     *
     * @param int         $statusCode
     * @param null|string $message
     *
     * @return static
     */
    public static function createByCode(int $statusCode, string $message = null): self
    {
        $errorMessage = null;
        if (isset(static::$statusCodes[$statusCode])) {
            $errorMessage = static::$statusCodes[$statusCode];

            if (filled($message)) {
                $errorMessage = $errorMessage . ' - ' . $message;
            }
        }


        $message = sprintf('%d: %s', $statusCode, $errorMessage ?? $message);

        return new static($message, $statusCode);
    }
}

Repo: https://github.com/irazasyed/php-transmission-sdk

PHP 7.3.11


Solution

  • Here is how I solved :

    <?php
    require 'vendor/autoload.php';
    try{
        $transmission = new Transmission\Client($hostname, $port, $username, $password, $httpClientBuilder = null);
    } catch (Transmission\Exception\NetworkException $e) {
        $result = array("Error" => "Remote access is disabled");
    }