Skip to content

skids/perl6xproto

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
t
 
 
 
 
 
 
 
 
 
 
 
 

perl6xproto

X::Protocol:: Perl 6 modules for protocol exceptions

Purpose

The X::Protocol superclass is a convenience for working with status results in protocol code. It allows one to reap the benefits of typed exceptions without having to type out their names very often. You simply feed the error code from the protocol in as an argument to X::Protocol.new (or, more usually, to a subclass) and it is automatically paired with a human readable error message, producing an object that can be printed, thrown, or inspected.

One can easily tell a protocol error apart from an internal code error by whether it matches X::Protocol, and tell which protocol an error came from either by looking at the .protocol attribute, or checking which subclass it belongs to.

Better yet, you can simply smart match the object against integers, strings and regular expressions in your error handling code.

For commonly used protocols, the X::Protocol module repository serves as a place for protocol-specific subclasses to store long lists of human-readable error messages so they can be shared by different protocol implementations and maintained in one place.

More finicky features are also available. See the embedded pod.

Idioms

The most common use will be something like this in a module:

class X::Protocol::BoxTruckOfFlashDrives is X::Protocol {
    method protocol { "BoxTruckOfFlashDrives" }
    method codes {
        {
            100 => "Out of gas";
            200 => "Petabytes per hour.  Beat that!";
            300 => "Now you have to plug them all in by hand";
            400 => "Hit a guard rail";
        }
    }
    method severity {
        when 200 { "success" };
        "error";
    }
}

...and then the user of the module would do something like this:

{
    # stuff that results in a status code in $result
    X::Protocol::BoxTruckOfFlashDrives.new(:status($result)).toss;
    # More stuff that happens if the exception is resumed or .toss
    # did not throw an exception.
    CATCH {
        when X::Protocol::BoxTruckOfFlashDrives {
            when 300 { plug_lots_of_flash_drives_in(); }
            when 100 { get_gas(); $_.resume }
        }
        # Handle other kinds of errors
    }
}

About

shared Perl6 repository for protocol-related exceptions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Other 100.0%