Best Practices for Magento Debugging


Published on

This presentation was delivered in the technical track of the break out sessions at Magento's Imagine eCommerce Conference, held on February 7-9, 2011 in Los Angeles.

Published in: Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Best Practices for Magento Debugging

  1. 1. OVERVIEW•  Environment configuration•  Common Magento development problems and how to approach them•  Using the right tools for the job: PHP debugging and profiling with Magento/ EclipseI won’t be covering everything in minute detail. At the end of the presentation, I’llprovide a link to slideshow and link to blog posts explaining different sections in moredetail.
  2. 2. AUTO-SET DEVELOPER MODE•  Enable Mage::isDeveloperMode() on development and staging environments •  Preferably, set the MAGE_IS_DEVELOPER_MODE via .htaccess file or server configuration. Example: •  Alternatively, set the developer mode using conditional code in index.php. Example:
  3. 3. SHOW ALL ERRORS IN DEVELOPER MODE•  Ensure that all errors are displayed when in developer mode:
  4. 4. WHY DETAILED ERROR AND EXCEPTION BACKTRACES ROCK•  You can see all arguments passed to functions in the call stack•  You can see all local variables at the location of the error/exception•  You can customize xdebug to create links that allow you to jump to a specific line/ file in your favorite editorHOW DO I GET SUCH DETAIL?•  Install Xdebug (an Apache module) on your development/staging servers•  My recommended xdebug configuration values:
  5. 5. Before
  6. 6. After Links to the location the file Fully expanded argument variablesLocal variables
  7. 7. HACK MAGENTO (SAFELY) TO ENABLEBACKTRACES•  Modify the Mage::run() method to not catch exceptions if developer mode is on (blog post explaining how to make this modification:
  8. 8. USING MAGE::LOG TO LOG DEBUG DATA•  Mage::log() allows you to log code to either the default var/logs/system.log file, or a custom file. •  Mage::log(‘This is a custom message’, Zend_Log::INFO, ‘customfile.log’, true)•  Pass Varien_Debug::backtrace(true, false) to Mage::log() to log a backtrace. This is helpful if you want to know what code is calling a specific section of code. This can be helpful when testing methods that are difficult to use a debugger on, such as Paypal code.•  Log the attributes of a model using Mage::log($model->debug()) The debug() method prevents Mage::log from logging the dump of all of the nested objects•  You can monitor the contents of the log files using: •  Command-line: tail –f <file_name> •  Mac: (Must have developer tools installed) •  Windows: Baretail ( )
  9. 9. CONFIGURE EXCEPTION HANDLER TO EMAILREPORTS•  When a user of a live site encounters an error, Magento shows them a form (NOTE: on what conditions will Magento show a form vs a generic exception page) allowing them to submit the details that happened that caused that error. That exception will also be logged to var/log/exceptions.log•  If you want to be notified about all exceptions via email, copy errors/ local.xml.template to errors/local.xml and configure it to send reports via email and enter your email.
  10. 10. GET TO THE BOTTOM OF THE SQLSTATE ERRORS•  What do you do when Magento throws a generic SQLSTATE database error?•  An “Integrity contraint violation”, or some other SQLSTATE error, doesn’t tell you much, unless you can view the error and the backtrace•  Especially relevant for errors encountered in the admin panel•  We have to hack two files (these hacks are harmless)…
  11. 11. GET TO THE BOTTOM OF THE SQLSTATE ERRORSModify Zend_Db_Adapter_Pdo_Abstract to get backtraces for single queries
  12. 12. GET TO THE BOTTOM OF THE SQLSTATE ERRORSModify Zend_Db_Statement_Pdo to get backtraces for transactional query errors
  13. 13. THINGS TO CHECK WHEN A CUSTOM MODULE DOESN’T LOADProblem: You’ve created a basic skeleton of a module with a module xml file,config.xml file, layout file, and block, but the module isn’t showing. Here are somequick steps you can take to debug the issue:1.  Ensure that the module’s xml file in app/etc/modules/ is being loaded. Tip: Add an error to the module’s config file and then reload the page. If the page throws an error, the file is being loaded.2.  Ensure that the module’s config.xml file is being loaded using same technique above3.  Ensure that the layout file is getting loaded using same technique above4.  Ensure that caching is disabled and remove the cache directory (var/cache) manually if it isn’t5.  If you’re not working on a case-sensitive partition, ensure the cases of your class names and xml references are correct.
  14. 14. DEBUGGING BY PROCESS OF ELIMINATIONProblem: You are working on a site with 5 third-party modules and 4 custommodules. You’ve heavily modified the way that products work in the system. You runinto an error where products aren’t saving from the admin.•  You can either: •  Start digging into the code to determine the cause of the issue, OR: •  Isolate the cause of the issue by progressively disabling the modules on the site until you identify which module is causing the error. Most of the time, this is the better approach.•  After you identify the violating module, start commenting out different sections of the config.xml file until the issue goes away.•  After you identify the bad class/file, go through the code, progressively eliminating different sections until you narrow it down to a line of code
  15. 15. ESOTERIC ERROR MESSAGEProblem: Your client tries to place an order in the admin. When doing so, they get a generic error message about the product stock quantity not being able to be saved (NOTE: replace with real example, preferably one that can be recreated easily). You check the error and exception logs, but you have nothing to work with. What do you do?•  Take the error message and search the Magento codebase for the error.•  Once you locate the error, set a breakpoint and debug the error from there.
  16. 16. DISABLING A MODULE VS DISABLING BLOCK OUTPUT•  Disabling block output explained •  You can access this option in System > Configuration > Advanced •  Disabling a module’s block output makes the XXXX class not output the contents of any blocks in that module •  Why would you disable a modules block output as opposed to disabling it? •  NOTE: Fill in details •  Core Magento classes that are commonly disabled •  Mage_Poll, Mage_Review, Mage_Tag, Mage_Wishlist (NOTE: Need to verify that there are no issues disabling these module‘s block output)•  Disabling a module •  Change the <active>true</active> to false •  Remove the module’s config file from app/etc/modules
  17. 17. QUICK TIPS: DEBUGGING SHIPPING METHOD RATES•  Problem: Your shipping rates are configured like they should be, but when you add certain items to your cart, the shipping section is showing an error message like “No shipping rates found”. Magento provides no easy way to see what errors the carriers are returning, and so these errors can be hard to debug.•  Modify the classes in app/code/core/Mage/Usa/Model/Shipping/Carrier/ <carrier_name>.php to log XML returned from carrier •  UPS: Filename, line number •  Fedex: Filename, line number •  USPS: Filename, line number•  Once you have the full XML response data from the carrier, you can determine the cause of the error.•  Common causes: •  The products in the cart have 0 weight •  NOTE: Add additional items…
  18. 18. DON’T BE AFRAID TO HACK - TEMPORARILY•  Find the source of the problem•  Set a debug point, or add a Mage::log() function to log the important data
  19. 19. OVERVIEW OF ECLIPSE DEBUGGING•  Breakpoints / conditional breakpoints •  Stepping through code•  Inspecting variables in local scope (especially relevant are the _data and _items arrays on models and collections)•  Jumping back through the Call Stack to view variables in scope at that point in the stack•  Watch expressions•  Tip: Run code in current context using “Inspect”
  20. 20. BREAKPOINTS•  Purposes •  Identify that Magento is hitting a certain section of code (can also use Mage::log(__METHOD__ . ‘ ‘ . __LINE); for that) •  Determine what a certain section of code does•  Conditional breakpointsSWITCH TO ECLIPSE FOR WALKTHROUGH
  21. 21. INSPECTING VARIABLES•  Inspecting variables in local scope •  Many classes in Magento have recursive properties, so if you expand all properties, you’ll get an infinite nesting •  The _data array stores all of the data present in a model •  The _items array contains all of the items loaded in a collectionSWITCH TO ECLIPSE FOR WALKTHROUGH
  22. 22. CALL STACK•  Shows all code that has been run up to that point•  Clicking to previous lines in the call stack changes the variables in the local scopeSWITCH TO ECLIPSE FOR WALKTHROUGH
  23. 23. WATCH EXPRESSIONS•  See what a certain expression would return if it was being run in the code at that point. Example: $product->getStatus();•  Run code in current context using “Inspect” •  Use Ctl+Shift+I to run code in local scopeSWITCH TO ECLIPSE FOR WALKTHROUGH
  24. 24. QUESTIONS?
  25. 25. ZEND STUDIO-ONLY FEATURES•  Comparison between PDT and Zend Studio•  For debugging, the biggest advantages of Zend Studio are: •  Profiling •  Tight Zend Server integration (helpful if Magento site is running on Zend Server in production)SWITCH TO Zend Studio FOR WALKTHROUGH