Home > Perl6 > Coloured Error Messages with Rakudo (and without Windows)

Coloured Error Messages with Rakudo (and without Windows)

Rakudo can display coloured error messages (controlled by %*ENV<RAKUDO_ERROR_COLOR>). If you want to be as cool as Rakudo, here is how it’s done.

class X::Parameter::ExtraNamed is Exception {
    has $.extra-parameters;
    has $.classname;
    has $.method-name;
    method message () {
        state ($r, $c, $g);
        once {
            ($r, $c, $g) = '','','';
            try { ($r, $c, $g ) = Rakudo::Internals.error-rcgye; } # red, clear, green, yelow, eject
        }

        "The method $g$.method-name$c of $g$.classname$c received " ~
        "an invalid named parameter(s): $r$.extra-parameters$c";
    }
}

The default is not to show any control chars. Since you may not be on Rakudo or those internals change in the future, sticking the real control chars into state variables is done in a try block.

Categories: Perl6

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: