ZF2 Catching Exceptions doesn't work - php

I wrote service to sending emails in ZF2 and unfortunatelly sometimes I get exception:
Zend\Mail\Protocol\Exception\RuntimeException
Could not read from [smtp server]
Part of stack trace:
#0 /homez.157/pastydol/www/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/AbstractProtocol.php(339): Zend\Mail\Protocol\AbstractProtocol->_receive(300)
#1 /homez.157/pastydol/www/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/Smtp.php(353): Zend\Mail\Protocol\AbstractProtocol->_expect(221, 300)
#2 /homez.157/pastydol/www/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/Smtp.php(389): Zend\Mail\Protocol\Smtp->quit()
#3 /homez.157/pastydol/www/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/Smtp.php(380): Zend\Mail\Protocol\Smtp->_disconnect()
#4 /homez.157/pastydol/www/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(152): Zend\Mail\Protocol\Smtp->disconnect()
#5 /homez.157/pastydol/www/module/Page/src/Page/Controller/OrderController.php(144): Zend\Mail\Transport\Smtp->__destruct()
I want to catch this exception, in service, which sending email I did:
try {
$transport->send($mail);
} catch(\Zend\Mail\Exception $e) {
return -1;
}
catch(\Exception $ex) {
return -1;
}
But it doesn't work, I still getting these exceptions.

Related

"An error occurred while handling another error: yii\web\HeadersAlreadySentException"

I am trying to submit a comment on a guestbook application based on the Yii 2 Framework. On localhost on my PC works everything fine, but on the shared hosting when I want to submit a comment in View, I get this error.
Here is the error:
An error occurred while handling another error:
exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /home/mahdikas/public_html/guestbook/controllers/PostController.php on line 117.' in /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
#2 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /home/mahdikas/public_html/guestbook/controllers/PostController.php on line 117.' in /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
#2 /home/mahdikas/public_html/guestbook/web/index.php(12): yii\base\Application->run()
#3 {main}
In the postController I have this code:
public function actionAdd_comment()
{
//print_r($_POST);
$model = new \app\models\Comments;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->comment_date = date('Y-m-d H:i:s');
if ($model->save()) {
echo 'Thanks for your comment.';
} else {
echo 'Failed!';
}
}
}
which line 117 in the error is:
echo 'Thanks for your comment.';
How can I solve this problem?
Since Yii 2.0.14 you cannot echo in a controller. A response must be returned:
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->comment_date = date('Y-m-d H:i:s');
if ($model->save()) {
return 'Thanks for your comment.';
} else {
return 'Failed!';
}
}
}
You may also call exit at the end of your method to prevent further processing or wrap your code with ob_start() and ob_get_clean(), if you're not able to avoid echo.
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$this->someMagicWithEcho();
exit;
}
}
or
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
ob_start();
$this->someMagicWithEcho();
return ob_get_clean();
}
}
Although I accept rob006's solution as correct, I have encountered a situation where there was no echo in the controller, but I also got the error. After going through several sites looking for a solution, I discovered an alternative.
You can check the php.ini file and ensure the output buffer is enabled. If not, you can enable it by adding this line in php.ini if it does not exist:
output_buffering = on
And turn it off for just the script - the script where it is not required by either...
calling ob_end_flush(), or
calling ob_end_clean()

PHP Exceptions CodeIgniter

my code is generating ssse following error:
Fatal error: Uncaught TypeError: Argument 1 passed to
CI_Exceptions::show_exception() must be an instance of Exception,
instance of Error given, called in
C:\xampp\htdocs\gog\lib\core\Common.php on line 662 and defined in
C:\xampp\htdocs\gog\lib\core\Exceptions.php:190 Stack trace: #0
C:\xampp\htdocs\gog\lib\core\Common.php(662):
CI_Exceptions->show_exception(Object(Error)) #1 [internal function]:
_exception_handler(Object(Error)) #2 {main} thrown in C:\xampp\htdocs\gog\lib\core\Exceptions.php on line 190
The line of the codes described is these:
function _exception_handler(Throwable $exception)
{
$_error =& load_class('Exceptions', 'core');
$_error->log_exception('error', 'Exception: '.$exception->getMessage(), $exception->getFile(), $exception->getLine());
// Should we display the error?
if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors')))
{
$_error->show_exception($exception); //line 662
}
exit(1); // EXIT_ERROR
}
public function show_exception(Exception $exception) //line 190
{
$templates_path = config_item('error_views_path');
if (empty($templates_path))
{
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
}
$message = $exception->getMessage();
if (empty($message))
{
$message = '(null)';
}
if (is_cli())
{
$templates_path .= 'cli'.DIRECTORY_SEPARATOR;
}
else
{
set_status_header(500);
$templates_path .= 'html'.DIRECTORY_SEPARATOR;
}
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include($templates_path.'error_exception.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}
Someone can pinpoint my problem ?
No CodeIgniter version has that _exception_handler() signature. This is what happens when you modify stock framework files.
Download a fresh copy of the latest CodeIgniter and replace yours with it.
You call show_exception(Exception $exception) from within _exception_handler(Throwable $exception). Since the former takes an Exception as argument, you cannot give it a Throwable, as you do in line 662. Exception implements the Throwable interface, but that does not guarantee all Throwables to be Exceptions (e. g. they could be of type Error).
Replace _exception_handler(Throwable $exception) with _exception_handler(Exception $exception), or change show_exception(Exception $exception) to show_exception(Throwable $exception), and change the method bodies accordingly if needed.

Try/Catch block in PHP-on-couch not catching Exception

I have the following PHP code:
try{
$doc = couchDocument::getInstance($db,"document10");
}catch(couchExpention $e){
echo $e->getMessage();
}
However instead of the desired output I get:
PHP Fatal error: Uncaught exception 'couchNotFoundException' with message 'Object Not Found - missing (GET /db/document10[])' in /var/www/public_html/lib/couchClient.php:1012
Stack trace:
#0 /var/www/public_html/lib/couchClient.php(157): couchException::factory(Array, 'GET', '/db/169488e77e2...', Array)
#1 /var/www/public_html/lib/couchClient.php(427): couchClient->_queryAndTest('GET', '/db/169488e77e2...', Array, Array)
#2 /var/www/public_html/lib/couchDocument.php(47): couchClient->getDoc('169488e77e2j385...')
#3 /var/www/public_html/lib/couchDocument.php(102): couchDocument->load('169488e77e2j385...')
#4 /var/www/public_html/index.php(8): couchDocument::getInstance(Object(couchClient), '169488e77e2j385...')
#5 {main}
thrown in /var/www/public_html/lib/couchClient.php on line 1012
You code is trying to catch a couchExpention instead of a couchException

(400) Bad Requestwhen using the permission to the library google-api-php-client

Uploading a file and immediately do permission->update:
public function sharingFile() {
$fileId = $this->file['id'];
$permissionId = $this->file['userPermission']['id'];
try {
$permission = $this->service->permissions->get($fileId, $permissionId);
$permission->setRole('writer');
$permission->setType('default');
print_r($permission);
return $this->service->permissions->update($fileId, $permissionId, $permission);
} catch (Exception $e) {
return "Error: " . $e;
}
return NULL;
}
and get an error:
Error: exception 'Google_ServiceException' with message 'Error calling PUT
https://www.googleapis.com/drive/v2/files/0B6xE_F1PfpXTbF9IdHgxbEJueEk/permissions/me:
(400) Bad Request' in Z:\home\site.com\www\google-api-php-client\src\io\Google_REST.php:66
Stack trace:
#0 Z:\home\site.com\www\google-api-php-client\src\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest))
#1 Z:\home\site.com\www\google-api-php-client\src\service\Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest))
#2 Z:\home\site.com\www\google-api-php-client\src\contrib\Google_DriveService.php(774): Google_ServiceResource->__call('update', Array)
it may be a bug in the library?
Help, please.
Your code is trying to update permissions for the document owner and make that user a writer instead. A Drive document must have exactly one owner, so your request is invalid.

problem with Zend Exception

I using Zend Gdata library for search video Youtube API.
If I make a bad request, then a zend exception..
Example:
Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' in D:\Webserver\domains\loc\controller\Zend\Gdata\App.php:710
Stack trace:
#0 D:\Webserver\domains\loc\controller\Zend\Gdata.php(221): Zend_Gdata_App->performHttpRequest('GET', 'http://gdata.yo...', Array, NULL, NULL, NULL)
#1 D:\Webserver\domains\loc\controller\Zend\Gdata\App.php(876): Zend_Gdata->performHttpRequest('GET', 'http://gdata.yo...', Array)
#2 D:\Webserver\domains\loc\controller\Zend\Gdata\App.php(764): Zend_Gdata_App->get('http://gdata.yo...', NULL)
#3 D:\Webserver\domains\loc\controller\Zend\Gdata\App.php(220): Zend_Gdata_App->importUrl('http://gdata.yo...', 'Zend_Gdata_YouT...', NULL)
#4 D:\Webserver\domains\loc\controller\Zend\Gdata.php(187): Zend_Gdata_App->getEntry('http://gdata.yo...', 'Zend_Gdata_YouT...')
#5 D:\Webserver\domains\loc\controller\Zend\Gdata\YouTube.php(293): Zend_Gdata->getEntry('http://gdata.yo...', 'Zend_Gdata_YouT...')
#6 D:\Webserver\domains\loc\controller\Yo in D:\Webserver\domains\loc\controller\Zend\Gdata\App.php on line 710
But I do not want users of my site have seen this error ... I need to catch this error and cause no Exception and just want to get the text of the error .. What should I do?
Put the code that causes the exception into a try/catch statement
try {
// code calling Zend_Gdata_App->performHttpRequest
} catch(Zend_Gdata_App_HttpException $e) {
$message = $e->getMessage();
// do something with $message now
}
More information: http://de3.php.net/manual/en/language.exceptions.php

Categories