How to override the Exception Class and not to display the Fatal error

2.1k views Asked by At

I am trying to write a class that communicate with an API. I would like to override the standard Exception class in PHP to return the message, error code that I want.

I have added this extension

<?php namespace API;

/**
 * API Exception
 *
 * @package ICWS
 */
class ApiException extends \Exception
{
    public function __construct($message, $code = 0)
    {
        // Custom ICWS API Exception Message
        $apiMessage = 'ICWS API Error: ' . $message;

        //More code to come to custom the error message/code.....

        // Run parent construct using custom message
        parent::__construct($apiMessage, $code);
    }
}
?> 

Then when needed I create new ApiException like so

    throw new ApiException($errorMessage, $errorNo);

Finally I wrap the function that throws an exception by try{} catch() block to capture the exception.

However, I still get the a fatal error instead of just the message that I provided.

Here is my code

public function createSession($userID, $password){

    $data = array('userID' => $userID,
                  'password' => $password);

    try {

        $data = $this->_makeCall('POST', 'connection', $data);

        $this->_csrfToken = $data['csrfToken'];
        $this->_sessionId = $data['sessionId'];
        $this->_alternateHostList = $data['alternateHostList'];

    } catch (Exception $e){
        $this->_displayError($e);
    }
}

private function _makeCall($uri, $data = false, $header = array())
{
    $ch = curl_init();
    $url = $this->_baseURL . $uri;

    //disable the use of cached connection
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

    curl_setopt($ch, CURLOPT_URL, $url);

    //return the respond from the API
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    if(!empty($header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }

    curl_setopt($ch, CURLOPT_POST, true);

    if ($data){
        $JSON = json_encode( $data );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $JSON );
    }


    $result = curl_exec($ch);

    //throw cURL exception
    if($result === false){
        $errorNo = curl_errno($ch);
        $errorMessage = curl_error($ch);

        throw new ApiException($errorMessage, $errorNo);
    }   

    $result = json_decode($result, true);

    //throw API exception
    if(  $this->_hasAPIError($result) )
    ){          
        throw new ApiException($result['message'], 0);
    }

    return $result;
}

private function _displayError(Exception $e){
    echo 'Error Number: ' . $e->getCode() . "\n";
    echo 'Error Description: ' . $e->getMessage() . "\n\n";     
}

private function _hasAPIError($result){
    if(    isset($result['errorId']) && !empty($result['errorId'])
        && isset($result['errorCode']) && !empty($result['errorCode'])
        && isset($result['message']) && !empty($result['message'])
    ){          
        return true;
    }

    return false;       
}

I would like to see something like this at then end "if there is an error"

Error Number: 0
Error Description: ICWS API Error: The authentication process failed

This is what I currently get

Fatal error: Uncaught exception 'API\ApiException' with message 'ICWS API Error: The authentication process failed.' in C:\phpsites\icws\API\ICWS.php:130 Stack trace: #0 C:\phpsites\icws\API\ICWS.php(57): API\ICWS->_makeCall('connection', Array) #1 C:\phpsites\icws\index.php(17): API\ICWS->createSession('user', 'pass') #2 {main} thrown in C:\phpsites\icws\API\ICWS.php on line 130
2

There are 2 answers

6
mathielo On BEST ANSWER

The error is that you are catching Exception, not ApiException. Try this:

try {
    $data = $this->_makeCall('POST', 'connection', $data);
    $this->_csrfToken = $data['csrfToken'];
    $this->_sessionId = $data['sessionId'];
    $this->_alternateHostList = $data['alternateHostList'];
} catch (ApiException $e){ // Here is the change: Exception to ApiException
    $this->_displayError($e);
}
0
helmbert On

You did not import the Exception class into your namespace, so when doing catch (Exception $e), Exception is an unknown class (because PHP is assuming API\Exception) and PHP will not notice that APIException is a subclass of Exception. Curiously, PHP does not complain about catching a non-existing class (I've just confirmed this locally with PHP 5.6.8).

The following should work:

catch (\Exception $e) {
    // ...
}

Alternatively:

use Exception;
// ...
catch (\Exception $e) {
    // ...
}