<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: error code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 20:21:07 GMT</pubDate>
    <description>DZone Snippets: error code</description>
    <item>
      <title>[PHP] A simple error function for everyday use...</title>
      <link>http://snippets.dzone.com/posts/show/5538</link>
      <description>... It uses the php function "error_log".&lt;br /&gt;&lt;code&gt;&lt;br /&gt;define("PATH_LOG","./log/");&lt;br /&gt;&lt;br /&gt;function error($line,$method,$class,$system_error,$user_error = "",$date = "",$log = true,$show = true) {&lt;br /&gt;	if (empty($date)) {&lt;br /&gt;		$date = date('r');&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	if (empty($user_error)) {&lt;br /&gt;		$user_error = $system_error;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	&lt;br /&gt;	$error = "$date - $method at $line - $system_error\n";&lt;br /&gt;	&lt;br /&gt;	if ($log == true) {&lt;br /&gt;		error_log($error,3,PATH_LOG."$class.log");&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	if ($show == true) {&lt;br /&gt;		echo "&lt;div class=\"error\"&gt;$user_error&lt;/div&gt;";&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//Example&lt;br /&gt;class Test {&lt;br /&gt;	private showError true;&lt;br /&gt;	&lt;br /&gt;	public function __construct() {&lt;br /&gt;		$test = false;&lt;br /&gt;		&lt;br /&gt;		if ($test === false) {&lt;br /&gt;			error(__LINE__,__METHOD__,__CLASS__,"sys error blub","There are internal problems. Sorry for that.","",true,$this-&gt;showError);&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$test = new Test();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 25 May 2008 19:25:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5538</guid>
      <author>alexvetter (Alexander Vetter)</author>
    </item>
    <item>
      <title>Adding helpful error messages to your Ruby code</title>
      <link>http://snippets.dzone.com/posts/show/5446</link>
      <description>This Ruby code raises an error if the XPath query fails because the attribute being queried did not exist for the given element. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def map_pattr(node, fieldx, valuex)&lt;br /&gt;    begin&lt;br /&gt;    parameter = node.root.elements["parameter[@field='#{fieldx}']"]&lt;br /&gt;    parameter.add_attribute('value', valuex)&lt;br /&gt;    parameter&lt;br /&gt;    &lt;br /&gt;    rescue&lt;br /&gt;      puts 'feedpopulated.rb: map_attr() the field ' + fieldx + ' was not found in params.'&lt;br /&gt;      raise&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Notice that a raise statement is used to ensure that the system error message is raised and any further code execution is halted.&lt;br /&gt;&lt;br /&gt;Without adding a customized helpful message I would be left scratching my head trying to work out what the following system error message meant.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;./feedpopulated.rb:27:in `map_pattr': undefined method `add_attribute' for nil:NilClass (NoMethodError)&lt;br /&gt;     from ./feedpopulated.rb:51:in `create_record'&lt;br /&gt;     from ./feedpopulated.rb:49:in `each'&lt;br /&gt;     from ./feedpopulated.rb:49:in `create_record'&lt;br /&gt;     from ./recordx.rb:91:in `call_create'&lt;br /&gt;     from ./s3fileuploader_handler.rb:14:in `call'&lt;br /&gt;     from ./s3fileuploader_handler.rb:40:in `invoke'&lt;br /&gt;     from ./uploadtwitteraudio.rb:22:in `initialize'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/element.rb:890:in `each'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/element.rb:890:in `each'&lt;br /&gt;     from ./uploadtwitteraudio.rb:18:in `initialize'&lt;br /&gt;     from ./uploadtwitteraudio.rb:72:in `new'&lt;br /&gt;     from ./uploadtwitteraudio.rb:72&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html"&gt;Programming Ruby: The Pragmatic Programmer's Guide - Exceptions, Catch, and Throw&lt;/a&gt; [ruby-doc.org]</description>
      <pubDate>Wed, 30 Apr 2008 11:40:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5446</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>MySQL error reporting</title>
      <link>http://snippets.dzone.com/posts/show/2739</link>
      <description>MySQL error reporting&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$result = mysql_query($query) or die("&lt;b&gt;A fatal MySQL error occured&lt;/b&gt;.\n&lt;br /&gt;Query: " . $query . "&lt;br /&gt;\nError: (" . mysql_errno() . ") " . mysql_error());&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 30 Sep 2006 07:03:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2739</guid>
      <author>Shpigford (Josh)</author>
    </item>
    <item>
      <title>Full information for Ruby errors</title>
      <link>http://snippets.dzone.com/posts/show/2427</link>
      <description>Sometimes I want to be able to print out everything about an error: it's class, message and the stack trace. So how about this:&lt;br /&gt;&lt;br /&gt;Update: followed suggestion by onarap and changed the line breaks to $/. No syntax highlighting because &lt;b&gt;the code breaks the snippet parser&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class StandardError&lt;br /&gt;  def info&lt;br /&gt;    "#{self.class}: #{message}#$/#{backtrace.join($/)}"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;</description>
      <pubDate>Wed, 16 Aug 2006 10:51:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2427</guid>
      <author>ciconia (Sharon Rosner)</author>
    </item>
    <item>
      <title>Let php show all errors</title>
      <link>http://snippets.dzone.com/posts/show/1718</link>
      <description>I have wondered why my php script doesn't show&lt;br /&gt;errors like it did before. It seems my server admin&lt;br /&gt;has default config set not to show the errors.&lt;br /&gt;&lt;br /&gt;Here's how to override it.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;error_reporting(E_ALL);&lt;br /&gt;ini_set('display_errors', '1');&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;BTW, it's really a pain to code in a language that&lt;br /&gt;demand a ';' after every line.</description>
      <pubDate>Mon, 20 Mar 2006 08:35:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1718</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Converting old PHP Errors into Exceptions</title>
      <link>http://snippets.dzone.com/posts/show/1617</link>
      <description>The PHP5 Exception class will not work correctly. It would reflect an incorrect line number and file name and not record the 'errcontext' value associated with PHP errors. To solve this problem, we must extend the Exception class.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * This exception behaves like a "old school" PHP Error&lt;br /&gt; */&lt;br /&gt;class STEM_ErrorException extends Exception&lt;br /&gt;{&lt;br /&gt;	/**&lt;br /&gt;	 * The PHP Error Context&lt;br /&gt;	 *&lt;br /&gt;	 * The fifth parameter is optional, errcontext, which is an array that points to the active symbol table at the point the error occurred. In other words, errcontext  will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context.&lt;br /&gt;	 */&lt;br /&gt;	private $m_arContext;&lt;br /&gt;&lt;br /&gt;	/**&lt;br /&gt;	 * Constructor&lt;br /&gt;	 */&lt;br /&gt;	public function __construct($vMessage, $vCode, $vFile, $vLine, $arContext = null)&lt;br /&gt;	{&lt;br /&gt;		parent::__construct($vMessage, $vCode);&lt;br /&gt;		&lt;br /&gt;		$this-&gt;file = $vFile;&lt;br /&gt;		$this-&gt;line = $vLine;&lt;br /&gt;		&lt;br /&gt;		$this-&gt;m_arContext = $arContext;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We need to define a function to handle errors. We also want to write two helper functions that will set and restore the error handler to our function. To do this, we write three methods in an abstract class.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * STEM Error Handler&lt;br /&gt; *&lt;br /&gt; * Registers Itself as a PHP Error Handler and proceeds to convert all&lt;br /&gt; * native "old school" PHP errors into new PHP5 Exceptions.&lt;br /&gt; *&lt;br /&gt; * Call STEM_ErrorHandler::Initialize(); before your try blocks and&lt;br /&gt; * STEM_ErrorHandler::Uninitialize(); afterwards.&lt;br /&gt; */&lt;br /&gt;abstract class STEM_ErrorHandler&lt;br /&gt;{&lt;br /&gt;	/**&lt;br /&gt;	 * Encapsulates set_error_handler()&lt;br /&gt;	 */&lt;br /&gt;	public static function Initialize()&lt;br /&gt;	{&lt;br /&gt;		set_error_handler(array("STEM_ErrorHandler", "HandleError"));&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * Encapsulates restore_error_handler()&lt;br /&gt;	 */&lt;br /&gt;	public static function Uninitialize()&lt;br /&gt;	{&lt;br /&gt;		restore_error_handler();&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * Handles PHP Errors&lt;br /&gt;	 */&lt;br /&gt;	public static function HandleError($errno, $errstr, $errfile, $errline, $errcontext)&lt;br /&gt;	{&lt;br /&gt;		throw new STEM_ErrorException($errstr, $errno, $errfile, $errline, $errcontext);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To test it, we create a file that triggers errors. Before triggering the first error, we call Initialize(). We then Uninitialize() the error handler and trigger the error again to check that our error handler has been removed. It is important to check the line numbers mentioned in the message that this prints!&lt;br /&gt;&lt;code&gt;&lt;br /&gt;STEM_ErrorHandler::Initialize();&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;	trigger_error("Hello World!");&lt;br /&gt;}&lt;br /&gt;catch (Exception $e)&lt;br /&gt;{&lt;br /&gt;	print $e;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;STEM_ErrorHandler::Uninitialize();&lt;br /&gt;print "&lt;hr /&gt;";&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;	trigger_error("Hello World!");&lt;br /&gt;}&lt;br /&gt;catch (Exception $e)&lt;br /&gt;{&lt;br /&gt;	print $e;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;Version 0.1.0 - 2006-02-14&lt;br /&gt;STEM: The STEM Cells of PHP&lt;br /&gt;This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License&lt;br /&gt;http://creativecommons.org/licenses/by-sa/2.5/</description>
      <pubDate>Thu, 02 Mar 2006 18:57:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1617</guid>
      <author>Charlie (Stephen Martindale)</author>
    </item>
    <item>
      <title>Fix for nil object error in Rails test fixtures</title>
      <link>http://snippets.dzone.com/posts/show/1135</link>
      <description>If you're seeing errors like this when you run Rails tests:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# NoMethodError: You have a nil object when you didn't expect it!&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You might need to edit test/test_helper.rb to make sure use_instantiated_fixtures is true:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;self.use_instantiated_fixtures = true&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Prior to 1.0, Rails automatically created instance variables out of fixtures. So if you had a fixture record named "foo", you could access it in your test as "@foo". As of 1.0, the default is to disable that feature, which breaks a lot of existing code. Mike Clark &lt;a href="http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting" title="Mike Clark's Weblog: Faster Testing with Rails 1.0"&gt;explains the change&lt;/a&gt;.</description>
      <pubDate>Wed, 11 Jan 2006 06:43:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1135</guid>
      <author>brainpipe ()</author>
    </item>
    <item>
      <title>TryCatch: simple try catch block in perl</title>
      <link>http://snippets.dzone.com/posts/show/928</link>
      <description>&lt;code&gt;&lt;br /&gt;### begin_: file metadata&lt;br /&gt;    ### &lt;region-file_info&gt;&lt;br /&gt;    ### main:&lt;br /&gt;    ###   - name    : TryCatch: try catch block&lt;br /&gt;    ###     desc    : a simple try-catch block example in perl&lt;br /&gt;    ###     date    : created="Thu 2005-12-01 10:58:09"&lt;br /&gt;    ###     last    : lastmod="Thu 2005-12-01 10:58:13"&lt;br /&gt;    ###     tags    : try catch finally error perl cfTryCatch exception&lt;br /&gt;    ### &lt;/region-file_info&gt;&lt;br /&gt;&lt;br /&gt;### begin_: init perl&lt;br /&gt;    use strict;&lt;br /&gt;    use warnings;&lt;br /&gt;&lt;br /&gt;### begin_: try-catch block&lt;br /&gt;    print "begin \n";&lt;br /&gt;    eval{&lt;br /&gt;        ### try block&lt;br /&gt;        print Non_Existent_Function();&lt;br /&gt;    };&lt;br /&gt;    if ($@){&lt;br /&gt;        ### catch block&lt;br /&gt;        print "Failed \n";&lt;br /&gt;    };&lt;br /&gt;    print "end \n";&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Dec 2005 06:00:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/928</guid>
      <author>drefty (drefty)</author>
    </item>
  </channel>
</rss>
