Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No need to echo db exception #9212

Closed
wants to merge 1 commit into from
Closed

No need to echo db exception #9212

wants to merge 1 commit into from

Conversation

kathysledge
Copy link
Contributor

No description provided.

@samdark
Copy link
Member

samdark commented Jul 25, 2015

Why?

@kathysledge
Copy link
Contributor Author

$exception contains a raw exception message from the database server, and that exception also seems to be handled fine like any other exception:

Extra info in error page
See the text printed here underneath the standard error screen (in prod. mode).

@samdark samdark added the status:to be verified Needs to be reproduced and validated. label Jul 25, 2015
@cebe
Copy link
Member

cebe commented Jul 25, 2015

how did you get this output? looks a bit like yii 1.1 error page...

@kathysledge
Copy link
Contributor Author

When no errorAction is set in the errorHandler config, the following view file is rendered: yiisoft/yii2/views/errorHandler/error.php. That's the one in the pic.

BTW, a full stacktrace is shown in debug mode, with a longer version of the exception being echoed at the end of the page (because of echo $exception).

@cebe cebe added this to the 2.0.x milestone Jul 26, 2015
@samdark samdark self-assigned this Oct 5, 2015
@samdark samdark modified the milestones: 2.0.7, 2.0.x Oct 5, 2015
@samdark samdark added type:bug Bug and removed status:to be verified Needs to be reproduced and validated. labels Oct 5, 2015
@samdark
Copy link
Member

samdark commented Oct 5, 2015

echo was added in #1946

@samdark
Copy link
Member

samdark commented Oct 5, 2015

I've verified that in current code it seems to work just fine w/o echo. @cebe any comments?

@kidol
Copy link
Contributor

kidol commented Oct 5, 2015

The echo part is intended for writeSession() only. If some database exception happens before writeSession() (and the exception is handled correctly), then writeSession() will be called anyhow because of register_shutdown_function([$this, 'close']); in Session::init().

This works correctly though:

Yii::$app->session->get('test');
sleep(5); // shutdown db server here
Yii::$app->session->set('test', 123);

@samdark
Copy link
Member

samdark commented Oct 5, 2015

If there's no DB table it works correcty as well i.e. exception is handled properly.

@kidol
Copy link
Contributor

kidol commented Oct 6, 2015

@samdark Yes it's handled, but not in writeSession(). You must test db exception that's thrown in writeSession() to see the need for echo().

Use this:

Yii::$app->session->get('test');
sleep(5); // shutdown db server here
Yii::$app->session->set('test', 123); // will cause exception in writeSession
exit;

This outputs the exception as string without rendering any html.

And if you do

// Shutdown db server here
Yii::$app->session->open();
exit;

You get rendered html + the string at bottom.

Dirty fix could be:

// Only echo and log if we haven't processed an exception yet
if (Yii::$app->errorHandler->exception === null) {
   $exception = ErrorHandler::convertExceptionToString($e);
   // its too late to use Yii logging here
   error_log($exception);
   echo $exception;
}

@cebe Is there really no way to properly handle exceptions thrown in writeSession()? For example, looking at symfony they seem to throw: https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php#L377

@samdark samdark removed their assignment Oct 6, 2015
@cebe cebe self-assigned this Oct 6, 2015
@cebe cebe modified the milestones: 2.0.8, 2.0.7 Feb 6, 2016
@cebe cebe modified the milestones: 2.0.9, 2.0.8 Apr 3, 2016
@cebe cebe modified the milestones: 2.0.10, 2.0.9 Jun 20, 2016
@cebe
Copy link
Member

cebe commented Oct 13, 2016

@cebe Is there really no way to properly handle exceptions thrown in writeSession()? For example, looking at symfony they seem to throw: https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php#L377

see http://us3.php.net/manual/en/function.session-set-save-handler.php#refsect1-function.session-set-save-handler-notes

however this issue has been worked around by fa845ee already. Closing this PR.

@cebe cebe closed this Oct 13, 2016
@cebe
Copy link
Member

cebe commented Oct 13, 2016

related to #11726

@samdark samdark mentioned this pull request Sep 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants