Zen Cart Logo
Forums / Zen Cart Code Suggestions / Use debug_backtrace() to determine query_factory caller on error

Use debug_backtrace() to determine query_factory caller on error

Views: 399

Results 1 to 16 of 16
17 Dec 2013, 8:27 PM
#1
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,081
Plugin Contributions:
56

Use debug_backtrace() to determine query_factory caller on error

There are many times when a Zen Cart store owner reports a debug-log that indicates that there was a problem detected in the query_factory class and sometimes it's difficult to track down "who" the actual culprit is. Since that class includes its own error-formatter (the show_error function), I suggest that that function make use of the PHP debug_backtrace function to determine the actual caller of a query_factory class function that errored and to include that debug information in the log ... making it much easier to help debug what are sometimes difficult problems.

18 Dec 2013, 4:42 AM
#2
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,869
Plugin Contributions:
7

Re: Use debug_backtrace() to determine query_factory caller on error

making it much easier to help debug what are sometimes difficult problems.
But that would mean I wouldn't have to resort to hours of pain crashing Eclipse and Xdebug watching code execute line by line.
Where would the fun be in knowing exactly where the bad call came from and spending more time growing my business, instead of liberally sowing seeds/echos through fields/pages of code and having to delete them all again (much) later.
Ooh you killjoy!

More power to your elbow!:clap:

18 Dec 2013, 8:31 AM
#3
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Location:
Amersfoort, The Netherlands
Posts:
2,862
Plugin Contributions:
5

Re: Use debug_backtrace() to determine query_factory caller on error

And how would i explain al those extra hours i need now to my customers :laugh:

Great Idea, I fully support.:thumbsup: I really hate searching all those possible query's.

18 Dec 2013, 6:46 PM
#4
countrycharm avatar

countrycharm

Totally Zenned

Join Date:
Jul 2007
Posts:
2,179
Plugin Contributions:
2

Re: Use debug_backtrace() to determine query_factory caller on error

This is a great idea, I also hate looking, all those errors. I'm assure it will cut out a lot of extra work for me.... :(

18 Dec 2013, 7:46 PM
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,081
Plugin Contributions:
56

Re: Use debug_backtrace() to determine query_factory caller on error

Well, I hate to make less work for people, but ... I made the following changes to the v1.5.2 version of /includes/classes/db/mysql/query_factory.php:

  function show_error() {
    if ($this->error_number == 0 && $this->error_text == DB_ERROR_NOT_CONNECTED && !headers_sent() && file_exists('nddbc.html') ) include('nddbc.html');
    echo '<div class="systemError">';
    if (defined('STRICT_ERROR_REPORTING') && STRICT_ERROR_REPORTING == true)
    {
      echo $this->error_number . ' ' . $this->error_text;
      echo '<br />in:<br />[' . (strstr($this->zf_sql, 'db_cache') ? 'db_cache table' : $this->zf_sql) . ']<br />';
    } else {
      echo 'WARNING: An Error occurred, please refresh the page and try again.';
    }
//-bof-20131218-lat9-Determine query_factory caller
    $backtrace_array = debug_backtrace();
    $query_factory_caller = '';
    foreach($backtrace_array as $current_caller) {
      if (strcmp($current_caller['file'], __FILE__) != 0) {
        $query_factory_caller = ' ==> (as called by) ' . $current_caller['file'] . ' on line ' . $current_caller['line'] . ' <==';
        break;
      }
    }
//-eof-20131218-lat9
    trigger_error($this->error_number . ':' . $this->error_text . ' :: ' . $this->zf_sql . $query_factory_caller, E_USER_ERROR);  //-20131218-lat9-added debug_backtrace
    if (defined('IS_ADMIN_FLAG') && IS_ADMIN_FLAG==true) echo 'If you were entering information, press the BACK button in your browser and re-check the information you had entered to be sure you left no blank fields.<br />';
    echo '</div>';
  }

and created this naughty file (/includes/functions/extra_functions/make_queryfactory_error.php:

<?php
$value = $db->Execute("SELECT unknown_field from " . TABLE_ORDERS . " WHERE orders_id = 1");

which resulted in the following debug log file:

[18-Dec-2013 20:41:10 Europe/Berlin] PHP Fatal error:  1054:Unknown column 'unknown_field' in 'field list' :: SELECT unknown_field from orders WHERE orders_id = 1 ==> (as called by) C:\xampp\htdocs\v1.5.2\includes\functions\extra_functions\make_queryfactory_error.php on line 2 <== in C:\xampp\htdocs\v1.5.2\includes\classes\db\mysql\query_factory.php on line 153
18 Dec 2013, 8:24 PM
#6
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Location:
Amersfoort, The Netherlands
Posts:
2,862
Plugin Contributions:
5

Re: Use debug_backtrace() to determine query_factory caller on error

Gave it a test-drive right away, and so far it seems to work great.
Thank you very much for this great little piece of code. :clap: that is a lot less of :frusty:

ps, it also works in ZC 1.5.1

19 Dec 2013, 2:12 AM
#7
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Use debug_backtrace() to determine query_factory caller on error

Curious, is the folder one that can be overridden? Guess could try to verify, but was wanting to make the change last beyond the next upgrade.

19 Dec 2013, 12:32 PM
#8
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,081
Plugin Contributions:
56

Re: Use debug_backtrace() to determine query_factory caller on error

mc12345678:

Curious, is the folder one that can be overridden? Guess could try to verify, but was wanting to make the change last beyond the next upgrade.
The change is, unfortunately, made to a core file so you'll need to carry the change over to the next upgrade.

19 Dec 2013, 1:42 PM
#9
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Use debug_backtrace() to determine query_factory caller on error

lat9:

The change is, unfortunately, made to a core file so you'll need to carry the change over to the next upgrade.

That's not a problem, didn't see that path as an override path when reviewing other resources, but was wishfully thinking.

Confirmed also works in 1.5.1, looking at implementation and php manual, seems will work on any PHP version in which the function exists.

19 Dec 2013, 3:32 PM
#10
countrycharm avatar

countrycharm

Totally Zenned

Join Date:
Jul 2007
Posts:
2,179
Plugin Contributions:
2

Re: Use debug_backtrace() to determine query_factory caller on error

Unfortunately when I add the changes to /includes/classes/db/mysql/query_factory.php
and add the other file to extra functions it creates the error file

[19-Dec-2013 15:24:46 UTC] PHP Fatal error: 1054:Unknown column 'unknown_field' in 'field list' :: SELECT unknown_field from orders WHERE orders_id = 1 ==> (as called by) /home/xxxxx/public_html/includes/functions/extra_functions/make_queryfactory_error.php on line 2 <== in /home/xxxxxx/public_html/includes/classes/db/mysql/query_factory.php on line 130

but I also get and error message when trying to access my store front ( WARNING: An Error occurred, please refresh the page and try again. )
if I leave (/includes/functions/extra_functions/make_queryfactory_error.php: in place.

I'm running zen cart 1.5.1

19 Dec 2013, 3:38 PM
#11
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Use debug_backtrace() to determine query_factory caller on error

countrycharm:

Unfortunately when I add the changes to /includes/classes/db/mysql/query_factory.php
and add the other file to extra functions it creates the error file

[19-Dec-2013 15:24:46 UTC] PHP Fatal error: 1054:Unknown column 'unknown_field' in 'field list' :: SELECT unknown_field from orders WHERE orders_id = 1 ==> (as called by) /home/xxxxx/public_html/includes/functions/extra_functions/make_queryfactory_error.php on line 2 <== in /home/xxxxxx/public_html/includes/classes/db/mysql/query_factory.php on line 130

but I also get and error message when trying to access my store front ( WARNING: An Error occurred, please refresh the page and try again. )
if I leave (/includes/functions/extra_functions/make_queryfactory_error.php: in place.

I'm running zen cart 1.5.1

Umm, exactly.

The "bad/nasty/whatever it was called file, was provided to force an error. It should be removed if the test is no longer needed.

6 Mar 2014, 5:23 AM
#12
explorer1979 avatar

explorer1979

Inactive

Join Date:
Jan 2007
Posts:
377
Plugin Contributions:
0

Re: Use debug_backtrace() to determine query_factory caller on error

lat9,

Thank you, this is useful for newbie too.

6 Mar 2014, 1:22 PM
#13
badarac avatar

badarac

Totally Zenned

Join Date:
Aug 2009
Location:
Longs, SC
Posts:
635
Plugin Contributions:
0

Re: Use debug_backtrace() to determine query_factory caller on error

Awesome suggestion. Thanks for this!

6 Mar 2014, 6:44 PM
#14
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Use debug_backtrace() to determine query_factory caller on error

@lat9, this feature will be included in v1.5.2 and newer. Thanks for the great suggestion.

6 Mar 2014, 8:07 PM
#15
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,081
Plugin Contributions:
56

Re: Use debug_backtrace() to determine query_factory caller on error

DrByte:

@lat9, this feature will be included in v1.5.2 and newer. Thanks for the great suggestion.
Thanks, DrByte! I (and others) await the v1.5.2 announcement(s)!

6 Mar 2014, 8:24 PM
#16
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Location:
Amersfoort, The Netherlands
Posts:
2,862
Plugin Contributions:
5

Re: Use debug_backtrace() to determine query_factory caller on error

DrByte:

@lat9, this feature will be included in v1.5.2 and newer. Thanks for the great suggestion.

That's great news