Skip to content

Error Handling

Error Messages

Sooner or later something is going to go wrong in your code. If this happens WebDyne will generate an error showing what the error was and attempting to give information on where it came from: Take the following example:

<start_html title="Error">
Let's divide by zero: !{! my $z=0; return 5/$z !}
<end_html>

Run

If you run the above example an error message will be displayed

In this example the backtrace is within in-line code, so all references in the backtrace are to internal WebDyne modules. The code fragment will show the line with the error.

If we have a look at another example:

<start_html title="Error">
<perl method="hello"/>
<end_html>

__PERL__

sub hello {

    die('bang !');

}

Run

And the corresponding screen shot:

We can see that the error occurred in the "hello" subroutine (invoked at line 2 of the page) within the perl block on line 9. The 32 digit hexadecimal number is the page unique ID - it is different for each page. WebDyne runs the code for each page in a package name space that includes the page's UID - in this way pages with identical subroutine names (e.g. two pages with a "hello" subroutine) can be accommodated with no collision.

Exceptions

Errors (exceptions) can be generated within a WebDyne page in two ways:

  • By calling die() as shown in example above.

  • By returning an error message via the err() method, exported by default.

Examples

__PERL__


#  Good
#
sub hello {

    return err('no foobar') if !$foobar;

}

# Also OK
#
sub hello {

    return die('no foobar') if !$foobar;

}

Error Checking

So far all the code examples have just assumed that any call to a WebDyne API method has been successful - no error checking is done. WebDyne always returns "undef" if an API method call fails - which should be checked for after every call in a best practice scenario.

<start_html title="Error">
<perl method="hello">

Hello World ${foo}

</perl>
<end_html>

__PERL__

sub hello {

    #  Check for error after calling render function
    #
    shift()->render( bar=> 'Again') || return err();

}

Run

You can use the err() function to check for errors in WebDyne Perl code associated with a page, e.g.:

<start_html title="Error">
<form>
<submit name="Error" value="Click here for error !">
</form>
<perl method="foo"/><end_html>

__PERL__

sub foo {

    &bar() || return err();
    \undef;

}

sub bar {

    return err('bang !') if $_{'Error'};
    \undef;
}

Run

Note that the backtrace in this example shows clearly where the error was triggered from.