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

Fixed Template->render() exception handling #78

Merged
merged 1 commit into from
Jul 9, 2015
Merged

Fixed Template->render() exception handling #78

merged 1 commit into from
Jul 9, 2015

Conversation

elazar
Copy link
Contributor

@elazar elazar commented Jul 9, 2015

Template->render() calls ob_get_clean() to get the result of rendering the template. According to the PHP manual page for this function, this is what it does, emphasis mine:

Gets the current buffer contents and delete current output buffer.

After this function is called, another block is executed to render the layout. Within this block, it's possible for a LogicException to be thrown if, for example, the layout template cannot be found. If this happens, there is a catch block to handle it.

There are two problems with this catch block:

  1. Because the aforementioned call to ob_get_clean() deleted the output buffer, the call to ob_end_clean() in the catch block has no buffer to delete and thus causes a notice to be emitted: "failed to delete buffer. No buffer to delete."
  2. Rather than the existing LogicException instance being re-thrown, a new one is being instantiated with the message of the original, causing the trace information associated with the original to be lost. This makes debugging difficult.

This PR applies these corrections:

  1. Wraps the ob_end_clean() call in a ob_get_length() check so that it is only called if needed, avoiding the previously emitted notice.
  2. Re-throws the existing LogicException instance so that the original trace information is preserved.

If the template was found but the layout was not, a notice was
being emitted: "failed to delete buffer. No buffer to delete."

Additionally, within the branch to catch a LogicException, rather than the
existing LogicException instance being re-thrown, a new one was being
instantiated with the message of the original, losing trace information in the
process.
@reinink
Copy link
Contributor

reinink commented Jul 9, 2015

Love it, thanks for working this out Matt!

reinink added a commit that referenced this pull request Jul 9, 2015
Fixed Template->render() exception handling
@reinink reinink merged commit 2d8569e into thephpleague:master Jul 9, 2015
@elazar
Copy link
Contributor Author

elazar commented Jul 9, 2015

@reinink Ha, you're quick. I'd just tweeted this at you. :p Thanks!

@elazar elazar deleted the fix/template-render-exceptions branch July 9, 2015 02:17
@reinink
Copy link
Contributor

reinink commented Jul 9, 2015

Haha, not always! Tagged.

@elazar
Copy link
Contributor Author

elazar commented Jul 9, 2015

@reinink Ha! Was just about to ask if you'd tag a release. It's like you're in my head or something. ;) Thank you sir! :D

okaprinarjaya pushed a commit to okaprinarjaya/plates that referenced this pull request Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants