1

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

executable
  • 3,365
  • 6
  • 24
  • 52

3 Answers3

0

You're using a relative namespace to define the exception Transmission\Exception\NetworkException $e in the catch statement

Either simply use NetworkException or add a backslash in front to make it an fully qualified reference.

EDIT: Reference from the php documentation https://www.php.net/manual/en/language.namespaces.basics.php

EDIT: clarifying solution

either

try
{...}
catch (NetworkException $e)
{ ... }

or

try
{...}
catch (\Transmission\Exception\NetworkException $e)
{ ... }
devb
  • 269
  • 1
  • 8
  • Can you show exactly what I need to put ? I used `} catch (\NetworkException $e) {` but still the same error – executable Nov 28 '19 at 14:15
  • Thanks, I tried both but unfortunately I still get the same error – executable Nov 28 '19 at 14:40
  • @executable If you throw the exception directly in the "try" block, using `throw new NetworkException("test",400)` does it get caught? I'm asking because perhaps something unexpected is happening due to the way you generate the instance (`new static(...)`) – devb Nov 28 '19 at 15:01
  • If I add `throw new NetworkException("test",400);` int the `try` block and catch `} catch (\NetworkException $e) {` I have the following error `Fatal error: Uncaught Transmission\Exception\NetworkException: test in path/to/my/file.php:19 Stack trace: #0 {main} thrown in /path/to/my/file.php on line 19
    `
    – executable Nov 28 '19 at 15:06
  • @executable remove the `\\` before the NetworkException in the catch statement – devb Nov 28 '19 at 15:08
  • I removed it and I have the following error : https://justpaste.it/1ycti – executable Nov 28 '19 at 15:12
  • @executable Looking at the stack trace of this last exception, it appears the exception is thrown outside of your try block. Is that possible? Or is there a part of the stack trace missing? – devb Nov 28 '19 at 15:40
  • why tou think it's outside of the try block ? – executable Nov 28 '19 at 15:46
  • because neither the constructor of the Transmission Client nor any direct throw statement (like you had in the previous comment) appear in the stack trace. – devb Nov 28 '19 at 15:54
  • I suggest you use logging methods, or if possible debug you program to find the source of the problem. Another thing you can try is add an additional catch-all block with `catch(\Exception) {} ` which should catch any exception occurring in that try block. – devb Nov 28 '19 at 15:56
  • @executable:place the throw statement first, or comment the instantiation of the transmision client for a while. The idea of adding the throw statement is simply a sanity check while triangulating to a solution. – devb Nov 29 '19 at 07:27
  • @executable, next, again looking at the stack trace it looks as if the exception is thrown outside your try block. An exception can be thrown anywhere, also during the "require" statement. And since it's the only place I see reference to the "vendor" stuff, I'm suspecting it's triggered there, before you even enter the try/catch – devb Nov 29 '19 at 07:33
  • Here is what I tried : https://justpaste.it/2hj0f Output : `ok` – executable Nov 29 '19 at 08:02
0

As you don't have a use-statement for NetworkException, PHP will assume it should be loaded from the namespace you're currently operating in. (Presumably \.) To fix this, simply add a statement like use Transmission\Exception\NetworkException;. This will tell PHP what type of exception you're trying to catch.

CerebralFart
  • 3,336
  • 5
  • 26
  • 29
  • I added `use Transmission\Exception\NetworkException;` and using catch like `} catch (NetworkException $e) {` I'm still getiing the error :( – executable Nov 28 '19 at 14:49
0

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");
}
executable
  • 3,365
  • 6
  • 24
  • 52