1

I have the following PHP script, test.php in my Moodle plugin:

<?php
include('lib/httpful/httpful.phar');

    try{
        $response = \Httpful\Request::post($uri)                 
            ->body($requestbody)             
            ->send();       
    }catch (Exception $e) {
        echo "Exception occurred";
    }
?>

Whenever an exception occurs the text "Exception Occurred" is displayed as expected. Then I moved the code to a function in a class, classes\http_client.php. Thus:

class http_client{
  public function doPost($uri, $requestbody){

    try{
        $response = \Httpful\Request::post($uri)                 
            ->body($requestbody)             
            ->send();       
    }catch (Exception $e) {
        echo "Exception occurred";
    }
  }
}

Now I try to invoke from test.php:

$client = new http_client();
$client->doPost($uri, $requestbody);

The exceptions are no longer caught and the stack trace is displayed in the browser.

I have to mention that it only happens within Moodle. Outside Moodle the class http_client works fine, the catch block is executed.

My settings are: Moodle 3.0.1+ (Build: 20151223), PHP 5.5.12, Apache 2.4.9.

Thanks in advance

Alex Black
  • 21
  • 4

1 Answers1

1

I found the solution on this Moodle forum. https://moodle.org/mod/forum/discuss.php?d=207445. Since the class http_client was within a namespace (a fact I have stupidly omitted), I had to escape the Exception.

Thus:

}catch (\Exception $e) {
            echo "Exception occurred";
 }

\Exception $e instead of Exception $e.

Alex Black
  • 21
  • 4