Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

fetch prints result

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
mwaack
Smarty Rookie


Joined: 01 Mar 2017
Posts: 7

PostPosted: Wed Sep 12, 2018 11:21 am    Post subject: fetch prints result Reply with quote

When u make an error in the template and user fetch() to store the Result, its always printing.

code looks like:
Code:
try {
    $output = $smarty->fetch('file');
} catch (Exception $ex) {
    die();
}
print $output;


And the Template:
Code:
{$object->property}


"property" is protected, so it throws an error.
If so, the template is still displayed with the error Message.

Expected is that nothing is displayed because "die();"
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Sep 12, 2018 11:37 am    Post subject: Reply with quote

Error, not exception.
If you want to catch errors as exceptions, you gotta set up the wrapper.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Sep 12, 2018 11:40 am    Post subject: Reply with quote

Code:
            try
            {
              set_error_handler(function($s, $m, $f, $l, $c = null) { throw new \ErrorException($m, 0, $s, $f, $l); });

              // Work

            }
            catch(\ErrorException $e)
            {
              error_log("Bad stuff happened.");
            }
            finally
            {
              restore_error_handler();
            }
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Sep 12, 2018 11:41 am    Post subject: Reply with quote

Adding to that, if the best you can do is die() there's no good reason to catch in first place.

And display_errors should be 0 on production site.
Back to top
View user's profile Send private message
mwaack
Smarty Rookie


Joined: 01 Mar 2017
Posts: 7

PostPosted: Wed Sep 12, 2018 11:57 am    Post subject: Reply with quote

The code is just an example, i have no die();

I have tested:

Code:
                try {
                    set_error_handler(function ($s, $m, $f, $l, $c = null) {
                        throw new \ErrorException($m, 0, $s, $f, $l);
                    });
                    $output = $this->smarty->fetch($this->controller->templateFile, $this->controller->cacheId, $this->controller->compileId);
                    print $output;
                } catch (\ErrorException $e) {
                    debug("ERROR");
                } finally {
                    restore_error_handler();
                }


Fail: The $output is still displayed.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Sep 12, 2018 7:57 pm    Post subject: Reply with quote

And why it is displayed?
Did you try actually debug your code?
Back to top
View user's profile Send private message
mwaack
Smarty Rookie


Joined: 01 Mar 2017
Posts: 7

PostPosted: Thu Sep 13, 2018 5:21 am    Post subject: Thats it Reply with quote

Thats the problem, why its displayed when i use "fetch" and not "display"

I Know the Error, but i want to catch it and display another Template, not the current.

The Error comes when i try to access a property that is protected.
Also other Errors, always fetch() do display the result with Error-Message.

From the Doku (https://www.smarty.net/docs/en/api.fetch.tpl)

Quote:
This returns the template output instead of displaying it.


But not when an error occurs.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Sep 13, 2018 10:06 am    Post subject: Reply with quote

Run your script in the debugger, and see, at which point it prints anything.
May be there's other interceptors that break your wrapper.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP