Can I redefine the way PHP displays uncaught exceptions? - php

A normal thrown and uncaught exception in PHP will produce output similar to this:
Fatal error: Uncaught exception 'Exception' with message 'Insufficient permissions or something' in /home/codemonkey/foo/bar/File.php:143
Stack trace:
#0 /home/codemonkey/dev/foo/bar/SomeOtherFile.php(85): foo\bar\File::fromUpload(Array)
#1 /home/codemonkey/dev/foo/bar/OmniDataFileImport.php(35): foo\bar\OmniDataFileImport->performFileImportLogic()
#2 [internal function]: foo\bar\OmniDataFileImport->post(Object(Request), false)
#3 /home/codemonkey/dev/foo/vendor/tonic/lib/tonic.php(654): call_user_func_array(Array, Array)
#4 /home/codemonkey/dev/foo/dispatch.php(22): Resource->exec(Object(Request))
#5 {main}
thrown in /home/codemonkey/dev/foo/broadnet/files/File.php on line 143
The message and stack trace is cool, but I would really like more information. For that reason I want to extend the Exception with more information such as local variables and their values. The problem is that PHP will probably still output the same above information but with the text "Uncaught exception 'MyException'" instead.
Can I override the way PHP displays uncaught exceptions? I recall reading about a function in PHP that you can set as a backup to catch all uncaught exceptions. Perhaps I can use that functionality to format my own message and kill the process.
Thanks for all helpful information on the topic

You are looking for set_exception_handler().
However, you cannot retrieve local variables etc. - PHP's error handling/debugging is rather poor unless you use a debugging extension which is not suitable for a production system.

Related

Google API php quickstart listFiles() 500 Internal Error

I tried the Example CLI PHP code from Google. The listFiles() method throws a 500 Internal Error from Google's servers, as shown in the stack trace:
bash-4.2$ php test.php
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/drive/v2/files?maxResults=10: (500) Internal Error' in /home/ben/.../vendor/google/apiclient/src/Google/Http/REST.php:79
Stack trace:
#0 /home/ben/.../vendor/google/apiclient/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
#1 /home/ben/.../vendor/google/apiclient/src/Google/Client.php(556): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#2 /home/ben/.../vendor/google/apiclient/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request))
#3 /home/ben/.../vendor/google/apiclient/src/Google/Service/Drive.php(1783): Google_Service_Resource->call('list', Array, 'Google_Service_...')
#4 /home/ben/.../test.php(78): Google_Service_Drive_Files_Resource->listFiles(Arra in /home/ben/.../vendor/google/apiclient/src/Google/Http/REST.php on line 79
I deleted the saved credentials and tried again. That did not help:
bash-4.2$ php test.php
Open the following link in your browser:
https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=urn%3A
ietf%3Awg%3Aoauth%3A2.0%3Aoob&client_id=...&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&access_type=offline&approval_prompt=auto
Enter verification code: ...
Credentials saved to /home/ben/.credentials/drive-php-quickstart.json
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/drive/v2/files?maxResults=10: (500) Internal Error' in /home/ben/.../vendor/google/apiclient/src/Google/Http/REST.php:79
Stack trace:
#0 /home/ben/.../vendor/google/apiclient/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
#1 /home/ben/.../vendor/google/apiclient/src/Google/Client.php(556): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#2 /home/ben/.../vendor/google/apiclient/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request))
#3 /home/ben/.../vendor/google/apiclient/src/Google/Service/Drive.php(1783): Google_Service_Resource->call('list', Array, 'Google_Service_...')
#4 /home/ben/.../test.php(78): Google_Service_Drive_Files_Resource->listFiles(Arra in /home/ben/.../vendor/google/apiclient/src/Google/Http/REST.php on line 79
I searched through the github issues and stackoverflow questions tagged google-api-php-client, but did not find any clues. I logged into the developer console and clicked Logs, which resulted in the text
Failed to load.
Any ideas how to get more detailed information about 500 errors from Google?
try adding this to the beginning of the code in the file calling Google Drive API
date_default_timezone_set('America/New_York') //change timezone you want
Also make sure, folder permission inside the google drive is set.

Zend Stack Trace not showing up due to fatal error

I have a Zend 1.11 application that is giving me a similar fatal error when any error exists
The Error is as follows:
Fatal error: Uncaught exception 'Zend_Mail_Protocol_Exception' with message 'No connection could be made because the target machine actively refused it. ' in C:\xampp\php\pear\Zend\Mail\Protocol\Abstract.php:277
Stack trace:
#0 C:\xampp\php\pear\Zend\Mail\Protocol\Smtp.php(167): Zend_Mail_Protocol_Abstract->_connect('tcp://localhost...')
#1 C:\xampp\php\pear\Zend\Mail\Transport\Smtp.php(199): Zend_Mail_Protocol_Smtp->connect()
#2 C:\xampp\php\pear\Zend\Mail\Transport\Abstract.php(348): Zend_Mail_Transport_Smtp->_sendMail()
#3 C:\xampp\php\pear\Zend\Mail.php(1194): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#4 C:\xampp\htdocs\test2\application\modules\default\controllers\ErrorController.php(153): Zend_Mail->send()
#5 C:\xampp\htdocs\test2\application\modules\default\controllers\ErrorController.php(66): Default_ErrorController->SendMail('Application err...', 'An error oc...')
#6 C:\xampp\php\pear\Zend\Controller\Action.php(516): Default_ErrorController->errorAction()
#7 C:\xampp\php\pear\Zend\Controller\Disp in C:\xampp\php\pear\Zend\Controller\Plugin\Broker.php on line 336
This is preventing the correct stack trace from showing up (i.e this error came from an intentional misnaming of the database which would normally throw an unknow DB exception) and there is'nt really anything to do with mail on this PHP application. Any advice would help. Thanks
On reviewing your callstack, it's the ErrorController within your application which is throwing the error.
I would guess it's trying to e-mail you the error details, rather than throwing the exception.
This obviously needs to be fixed, but to see your error now try adding this into your application.ini
resources.frontController.params.displayExceptions = 1
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
phpSettings.error_reporting = "8191"
resources.frontController.throwExceptions = true
This will throw any errors to screen, rather than trying to handle them.

Error on large files with GridFS and MongoDB

I seem to be getting an error when trying to upload large files into MongoDB..
Fatal error: Uncaught exception 'MongoGridFSException' with message 'could not open file ' in /home/jclark/thundergrid.fusionstrike.com/thundergrid/thundergrid.php:33 Stack trace: #0 /home/jclark/thundergrid.fusionstrike.com/thundergrid/thundergrid.php(33): MongoGridFS->storeUpload('pic', Array) #1 /home/jclark/thundergrid.fusionstrike.com/thundergrid/admin/index.php(7): Admin->upload() #2 {main} Next exception 'MongoGridFSException' with message 'error setting up file: ' in /home/jclark/thundergrid.fusionstrike.com/thundergrid/thundergrid.php:33 Stack trace: #0 /home/jclark/thundergrid.fusionstrike.com/thundergrid/thundergrid.php(0): MongoGridFS->storeUpload() #1 /home/jclark/thundergrid.fusionstrike.com/thundergrid/admin/index.php(7): Admin->upload() #2 {main} thrown in /home/jclark/thundergrid.fusionstrike.com/thundergrid/thundergrid.php on line 33
Is there a reason I'm getting these or...
Many thanks!
If the file is over 2GB/4GB, then this is likely a limitation on how PHP is build on your platform. In most cases, LFS (large file system) is not enabled (and theoretically it's not officially supported by the PHP dev team either). It is also possible that you are using a 32-bit compile of PHP as well, in which case you need a 64-bit build.

Error setting up WURFL The ZipArchive extension is not loaded. Load the extension or use the flat wurfl.xml file

Have any of you encountered this error before while running the demo of WURFL? I am tyring to user WURFL to redirect our mobile site visitors to different content but this error is not letting me go ahead.
Here is the error Detail:
Fatal error: Uncaught exception 'WURFL_WURFLException' with message 'The ZipArchive extension is not loaded. Load the extension or use the flat wurfl.xml file' in /var/www/bpaudel/wurfl-php-1.4.1/WURFL/Xml/Utils.php:49 Stack trace: #0 /var/www/bpaudel/wurfl-php-1.4.1/WURFL/Xml/Utils.php(36): WURFL_Xml_Utils::getZippedFile('/var/www/bpaude...') #1 /var/www/bpaudel/wurfl-php-1.4.1/WURFL/Xml/AbstractIterator.php(47): WURFL_Xml_Utils::getXMLFile('/var/www/bpaude...') #2 /var/www/bpaudel/wurfl-php-1.4.1/WURFL/DeviceRepositoryBuilder.php(83): WURFL_Xml_AbstractIterator->__construct('/var/www/bpaude...') #3 /var/www/bpaudel/wurfl-php-1.4.1/WURFL/WURFLManagerFactory.php(150): WURFL_DeviceRepositoryBuilder->build('/var/www/bpaude...', NULL) #4 /var/www/bpaudel/wurfl-php-1.4.1/WURFL/WURFLManagerFactory.php(134): WURFL_WURFLManagerFactory->deviceRepository(Object(WURFL_Storage_File), Object(WURFL_UserAgentHandlerChain)) #5 /var/www/bpaudel/wurfl-php-1.4.1/WURFL/WURFLManagerFactory.php(77): WURFL_WURFLManagerFactory->init() #6 /var/w in /var/www/bpaudel/wurfl-php-1.4.1/WURFL/Xml/Utils.php on line 49
In the error, it also says "use the flat wurfl.xml" file. How would we use wurfl.xml as an alternative?
Thanks,
Bishnu

Google Api Oath Exception Issues

I am newer to the Google Plus Api, but I have followed each and every rule to integrate G-Plus API in my WEB. But every time I get stuck at the issue:
Fatal error: Uncaught exception 'apiAuthException' with message 'Error refreshing the OAuth2 token, message: ''' in C:\AppServ\www\googleplus-source\google-api-php-client\src\auth\apiOAuth2.php:203
Stack trace:
#0 C:\AppServ\www\googleplus-source\google-api-php-client\src\io\apiCurlIO.php(51): apiOAuth2->sign(Object(apiHttpRequest))
#1 C:\AppServ\www\googleplus-source\google-api-php-client\src\io\apiREST.php(55): apiCurlIO->authenticatedRequest(Object(apiHttpRequest))
#2 C:\AppServ\www\googleplus-source\google-api-php-client\src\service\apiServiceResource.php(148): apiREST::execute(Object(apiServiceRequest))
#3 C:\AppServ\www\googleplus-source\google-api-php-client\src\contrib\apiPlusService.php(204): apiServiceResource->__call('get', Array)
#4 C:\AppServ\www\googleplus-source\google-plus-access.php(40): PeopleServiceResource->get('me')
#5 C:\AppServ\www\googleplus-source\index.php(2): include_once('C:\AppServ\www\...')
#6 {main} thrown in C:\AppServ\www\googleplus-source\google-api-php-client\src\auth\apiOAuth2.php on line 203
What could be the reason for this error?
What could be the reason for this error?
The reason for this error (a Fatal error) is given in the error message: Uncaught exception. That means an exception­Docs has been thrown and was not caught. This results in a fatal error.
Most likely in your case, the root cause of the problem highlighted in the exception message is misconfiguration and/or wrong API usage. As you have not posted any code in your question, you should read the API documentation first and double check your usage of the API and it's configuration.

Categories