asana-api-php Fatal error: Class 'Error' not found - php

When attempting to use the Asana API PHP library from here https://github.com/Asana/php-asana I get the following error:
Fatal error: Class 'Error' not found in \src\Asana\Dispatcher\OAuthDispatcher.php on line 49
I realise the error is because the authorization is not working - but how can I fix it to actually throw an exception?

you need to figure out if the class Error is exists and in a loader path.
but for now you can wrap problematic code to try.... catch
try{
line 49 code here
}catch(Exception $e){}

Related

PHP Uncaught Exception, should be caught

I'm handling some PHP errors during an Ajax call and I get the following message in Chrome Console:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined function data()
Filename: /var/www/application/models/M_ajax.php
Line Number: 604
Backtrace:
File: /var/www/application/controllers/Global_fxns.php
Line: 47
Function: _4___signIn
File: /var/www/html/index.php
Line: 349
Function: require_once
The thing is, its inside a try/catch block. And PHP errors should be caught. For example, if i execute this code inside the try block
strlen();
The code is caught as you would expect, as strlen expects a parameter. But this error is not caught. The line of code that is triggering the error is
if ( ! $this->M_account->addUserTrafficEvent( $userId, data('Y-m-d >H:i:s'), 2.1, '' ) ) {
And it's an error because I meant to type 'date' instead of 'data' to use PHP's date function, and because 'data' is not a function its creating an error. Why isn't this error being caught? Is there some kind of difference between errors created with PHP functions and user-defined functions, and if so shouldn't it still be caught? It's generating a PHP error message after-all
Thanks
From PHP 7 you can use catch(Error $err).
<?php
try {
data('Y-m-d >H:i:s');
} catch (Exception $exc) {
echo 'Fatal exception caught: '.$exc->getMessage();
} catch (Error $err) {
echo 'Fatal error caught: '.$err->getMessage();
}
?>
Checkout the Fiddle I've made for you

PHP Fatal error: Uncaught exception 'Exception'

I'm playing around with exceptions in PHP. For example, I have a script that reads a $_GET request and loads a file; If the file doesn't exists, an new exception should be thrown:
if ( file_exists( $_SERVER['DOCUMENT_ROOT'] .'/'.$_GET['image'] ) ) {
// Something real amazing happens here.
}
else {
throw new Exception("The requested file does not exists.");
}
The problem is that, when I try to supply an non existent file for the test, I got a 500 error instead of the exception message. The server log is the following:
[09-Jul-2013 18:26:16 UTC] PHP Fatal error: Uncaught exception 'Exception' with message 'The requested file does not exists.' in C:\sites\wonderfulproject\script.php:40
Stack trace:
#0 {main}
thrown in C:\sites\wonderfulproject\script.php on line 40
I wonder if I'm missing something real obvious here.
I've checked this question PHP fatal error: Uncaught exception 'Exception' with message but it's not quite like my issue, and have no concise answer.
Help, please?
* EDIT *
It seems this is something related to the throw keyword. If I use echo for example, I got the message printed on the screen, like this:
exception 'Exception' with message 'The file does not exists.' in C:\sites\wonderfulproject\script.php:183 Stack trace: #0 {main}
Why is that?
** EDIT 2 **
Thanks to #Orangepill, I got a better understanding about how to handle exceptions. And I found a superb tut from nettuts that helped a lot. The link: http://net.tutsplus.com/tutorials/php/the-ins-and-outs-of-php-exceptions/
This is expected behavior for an uncaught exception with display_errors off.
Your options here are to turn on display_errors via php or in the ini file or catch and output the exception.
ini_set("display_errors", 1);
or
try{
// code that may throw an exception
} catch(Exception $e){
echo $e->getMessage();
}
If you are throwing exceptions, the intention is that somewhere further down the line something will catch and deal with it. If not it is a server error (500).
Another option for you would be to use set_exception_handler to set a default error handler for your script.
function default_exception_handler(Exception $e){
// show something to the user letting them know we fell down
echo "<h2>Something Bad Happened</h2>";
echo "<p>We fill find the person responsible and have them shot</p>";
// do some logging for the exception and call the kill_programmer function.
}
set_exception_handler("default_exception_handler");
Just adding a bit of extra information here in case someone has the same issue as me.
I use namespaces in my code and I had a class with a function that throws an Exception.
However my try/catch code in another class file was completely ignored and the normal PHP error for an uncatched exception was thrown.
Turned out I forgot to add "use \Exception;" at the top, adding that solved the error.
For
throw new Exception('test exception');
I got 500 (but didn't see anything in the browser), until I put
php_flag display_errors on
in my .htaccess (just for a subfolder).
There are also more detailed settings,
see Enabling error display in php via htaccess only

Can't catch BadMethodCallException

Here is the part of my code:
// ... code ...
$action = self::defineAction( $request->getPath() );
try {
$response = Controller::$action( $request );
} catch( \BadMethodCallException $exception ) {
Logger::logError( $exception );
$response = new NotFoundResponse();
}
// ... code ...
I try to catch an exception if by some accident the action of the controller with the defined name is not implemented or if the name is defined wrongly.
But instead of catching exception I get Fatal Error in Apache's error log:
PHP Fatal error: Call to undefined method app\\Controller::testingAction() ...
If I try to call an undefined method inside the existing (defined and callable) action of the controller, I also can't catch the aforementioned exception - the Fatal Error occurs instead:
PHP Fatal error: Call to undefined method app\\SomeClass::someUndefinedMethod() in /********/Controller.php on line *** ...
Replacing the "\BadMethodCallException" by the "\Exception" has no effect: I'm keeping get the Fatal Errors.
Putting the "try-catch" block inside the every action of the controller is not the acceptable solution for me.
Why the exception can't be caught this way? How can I solve this problem?
I'm running PHP 5.3.8.
Catch blocks can only catch thrown exceptions, not errors. Call to undefined method is an error and you will need to test for this and throw an exception yourself. Please see this for differences between exceptions and errors.
You can test whether a method exists by doing something like this:
if( !method_exists('app\Controller', 'testingAction') ) {
throw new \BadMethodCallException();
}

Redirect to other page if fatal error comes

Is there any way i can redirect my visitors to other url if i got fatal error? or any way by which i can ignore that fatal error so that it will solve problem. fatal error show to some user based on less than 30 facebook friends. Error is this
Fatal error: Uncaught Exception: 100: null does not resolve to a valid user ID thrown in /home/scorpio/public_html/domain.com/src/base_facebook.php on line 1024
catch this exception! Exceptions

Facebook PHP SDK Throwing an Uncatchable OAuthException

I'm attempting to post an open graph action to the Facebook Graph API but receiving an OAuth Exception (#3501) User is already associated to the <object>. That's all well and good, I expect Facebook to throw that exception. I get some other exceptions regarding authenticating a user (maybe with old/stale sessions, whatever).
My question is, has anyone else experienced that this exception is uncatchable in php? In this specific example (of posting graph actions) I am absolutely wrapping the call to the api in a try/catch statement; but I still get the fatal error.
<?php
try {
//publishing to open graph
$this->fb->api('/me/app:action', 'POST', array(
'object' => 'http://www.domain.com/path/to/graph/object',
));
}
catch (Exception $e)
{
/*
We may get here if the user has already posted this action before...
or if our session somehow went sour
or bc facebook is down...
or one of any other 1000 reasons the graph api is currently
sucking...
in any case it doesn't much matter, this is not a mission critical
thing to worry about; if we don't post the graph action - we don't
post the graph action..nbd.
*/
}
The above code is the snippet the publishes the graph action (generalized, because the content of it isn't important to this example).
I realize that the Exception that the Facebook PHP SDK is throwing is a FacebookApiException but that class extends Exception. I can't for the life of me figure out why in the name of all things logical, I can't catch my exception like this.
Has anyone experienced this issue? Is this a bug in the FB PHP SDK? Am I missing something else here? Thanks for your help!
Also, for reference, the relevant parts of the FB PHP SDK are here:
FacebookAPIException Definition (base_facebook.php line 30)
Throwing OAuthException (base_facebook.php line 1105
Edit 5/1/12
After some more investigation, it turns out that this "Exception" isn't really being treated like an exception at all. Typical Exceptions print out a stack trace back to the method call which resulted in throwing the exception. These "OAuthExceptions" do not. Also, typical exceptions pring out their error string a bit differently, for example:
PHP Fatal error: Uncaught exception 'Exception' with message 'foo' /path/to/file.php:10
or
PHP Fatal error: Uncaught exception 'MyException' with message 'stupid php' /path/to/file:10
#0 /path/to/file.php(17): doTest()
#1 {main}
thrown in /path/to/file.php on line 10
In this particular case, we don't get any of that, and it looks much more like a typical fatal error:
PHP Fatal error: Uncaught OAuthException: (#3501) User is already associated \
to the <object> object on a unique action type <action>. Original Action ID: \
123123123
thrown in /path/to/app/libs/fb/base_facebook.php on line 1107, \
referer: http://www.domain.com/path/to/page
I can't make any sense of why this forsaken "Exception" is so weird/uncatchable.
The Solution:
I figured out the answer to my own question; I've added it below - it's developer error, not a bug. The answer is below.
Also, this very well could be part of a bug, if you wanted to say that being able to reference a class definition as a type-hint to the catch definition which didn't exist (or wasn't available in the current namespace) is a bug.
So, something that's not outline above is that I'm utilizing PHP namespaces. This is a big gotchta since namespaces are relatively new to php, it's super easily overlooked I feel. Regardless, it's a pretty silly oversight/error.
If you're in a defined namespace (that is, not the root (\) namespace) you don't have direct access to the Exception class. Instead of php throwing a warning about not knowing what that class is, it just ignores the fact that it doesn't know what it is - and doesn't catch the exception.
Solution 1:
import the exception class:
<?php
use \Exception;
// ...codes
try {
//...codes
}
catch (Exception $e)
{
//...codes
}
Solution 2:
provide the full path to the exception class:
<?php
try {
//.....
}
catch (\Exception $e)
{
// voila.
}

Categories