develooper Front page | perl.perl6.users | Postings from October 2018

Re: Appropriate last words

Thread Previous | Thread Next
From:
yary
Date:
October 23, 2018 07:20
Subject:
Re: Appropriate last words
Message ID:
CAG2CFAYF+jsQZsAQdN35JeoQs_eXj_0mvgttfmsEXEeSFkEp8g@mail.gmail.com
Richard seems to be close to a workable answer, but now I am wondering
about this issue.

Imagine writing a configuration-checking class (or role) to be sure all the
files and directories exist at startup. The goal is to use exceptions, so
the consumer of the checker can use standard exception handling... maybe
the mainline code can create the missing item. Unhandled exceptions are to
note a message on stderr, with no backtrace, and exit with non-zero status.

I think that's RIchard's original question, with the twist of requiring
throwing a catchable exception. And I think it is doable

1. A role "X-no-trace" to compose into any exception class,
2. which installs into the outermost scope a CATCH block for that exception
class,
3. and that CATCH block is { .Str.note; exit 1 }
4. ... create an exception class that "does X-no-trace" and throw it as
needed
5. If caller does nothing, outermost block catches it and notes/dies
6. If caller installs a CATCH block for that class, it can intercept and do
as it will.

Too tired to try it out at the moment, and I think step 2 would want the
COMPOSE phaser which is documented as unimplemented. And need to check that
$?CLASS is ok to use in a role. A challenge to get working!

-y


On Sun, Oct 21, 2018 at 3:08 AM Richard Hainsworth <rnhainsworth@gmail.com>
wrote:

> I am trying to find a way to send a message via STDERR to a user, and to
> exit, but to eliminate the backtrace printing.
>
> so .. either I use your suggestion of 'exit note $message' which I find
> elegant, but so far difficult to test.
>
> (I tried timo's suggestion of &*EXIT = -> | { die 'exited' }
>
> but then I cant work out how to get the message. I've been looking at
> Zoffix's Test::Output, but not
>
> or .. change the default exception handler. However, I only want to
> change the way the fatal exception is handled for the class that I am
> writing.
>
> Eg.
>
> class conditions {
>
> has $!dir = 'path';
>
> has @!files = ();
>
> ...
>
> method check-dir {
>
>      die 'directory not present' unless $!dir.IO ~~ :d;
>
> # I would like here exit note 'directory not present' ...
>
> }
>
> method check-files {
>
>      die 'no files yet' unless +@!files;
>
> }
>
> This may not yet be the right  approach to get what I want.
>
>
> On 21/10/18 17:32, Elizabeth Mattijsen wrote:
> > I’m not sure what you mean by: "How do I attach a default CATCH to all
> methods in the class.”.  What are you trying to achieve?
> >
> >> On 21 Oct 2018, at 10:35, Richard Hainsworth <rnhainsworth@gmail.com>
> wrote:
> >>
> >> This sounds great.
> >>
> >> So I am writing a class verifies conditions, and dies when conditions
> are not met.
> >>
> >> How do I attach a default CATCH to all methods in the class.
> >> Or do I need to define my own Exception.
> >>
> >> On 04/09/18 04:48, Curt Tilmes wrote:
> >>>
> >>> On Mon, Sep 3, 2018 at 4:28 PM Parrot Raiser <1parrota@gmail.com>
> wrote:
> >>> If I understand that correctly, "die" needs to be documented as always
> >>> outputting the line number, and that for user-oriented messages, one
> >>> of the other techniques should be used.
> >>>
> >>> die throws the Exception -- you can CATCH it and handle it how you
> like, including printing the message without line numbers.
> >>>
> >>> (Or, for example, print the backtrace during development, but a
> simpler error message in operations).
> >>>
> >>>
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About