in reply to Re: STDERR in Test Results
in thread STDERR in Test Results

Thanks kcott but that still generates the rogue output to STDERR

I've tried this:

my $got_warn = 0; eval { $webhook_fail->process(); } or do { $got_warn = 1; }; ok( $got_warn, "Signature warning generated"); ok( !$webhook_fail->success, "Signature error" );
Both tests pass but I still get the output to STDERR when the process method is called.

I guess I'm just looking for a way to suppress the output...

Replies are listed 'Best First'.
Re^3: STDERR in Test Results
by kcott (Archbishop) on Jun 20, 2023 at 02:09 UTC
    "... expected error ... Webhook Error ... Signature error ... an error displayed ..."

    Originally, you repeatedly referred to an error; now you seem to have changed that to a warning. My solution traps errors, which you asked for; it doesn't trap warnings.

    $ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +' $ perle ' my $expected_error = 1; my $got_error = 0; eval { my @rgb = qw{#ff0000 #00ff00 #0000ff}; open my $fh, "<", "nonexistent_file"; 1; } or do { $got_error = 1; }; say "\$expected_error[$expected_error] \$got_error[$got_error]"; ' Possible attempt to put comments in qw() list at -e line 6. $expected_error[1] $got_error[1]

    Perhaps "warnings: Fatal Warnings" would help you.

    — Ken

      Originally, you repeatedly referred to an error; now you seem to have changed that to a warning

      Sorry for the confusion.

      My module throws warnings and errors - it generates both by printing to STDERR

      The question wasn't so much about dealing with warnings and errors but more about suppressing the warnings during the output from the tests. Although, this is one of those situations where I didn't really know exactly what I wanted to know until I had asked the question and got lots of helpful comments. The original question asked about suppressing this line

      Stripe Webhook Error: Invalid Stripe Signature
      Hence why I referred to errors

        My module throws warnings and errors - it generates both by printing to STDERR

        No, it does not, at least not in version 1.11 as published on CPAN. It throws no errors and no warnings. It just prints to STDERR, although it looks like it should alternatively call a custom function. (See below the line.)

        To throw errors, call die. To throw warnings, call warn. To suppress "at <filename> line <number>", append a newline to the message. To get a stack trace, see Carp.


        I think this does not do what you want:

        # Returns error if last operation failed sub error { my $self = shift; return $self->{'error'}; } # ... sub _error { my ($self, $message) = @_; $self->{'error'} = $message; # <--(1) if (defined &{$self->{'error'}}) { # <--(2) &{$self->{'error'}}($message); # <--(3) } else { STDERR->print("Stripe Webhook Error: $message\n"); } }

        At (1), you store the error message in $self->{'error'}, much like you do in sub error. At (2), you check if $self->{'error'} that you just have overwritten contains an error callback function which you try to call at (3). Unless $message is set to a valid function name, this code will always print to STDERR.

        I think what you wanted to implement were two attributes, one for the error message (e.g. $self->{'errorMessage'}) and one for an error handler (e.g. $self->{'errorHandler'}).

        But what you really want is just a proper exception. Don't re-implement the wheel, don't mess up your object with all of that error handling nonsense. If something goes wrong, just call die and leave it to the user if (s)he wants to use a plain old eval, something like Try::Tiny, the fancy new try-catch-finally, or just let the program die. Same for warnings. Just call warn and let the user decide if warnings should cause some action or should be ignored.

        If you want fancy stack traces, use Carp instead of plain die/warn.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^3: STDERR in Test Results
by afoken (Chancellor) on Jun 19, 2023 at 18:23 UTC

    Update: Problem already found in Re^4: STDERR in Test Results.


    Both tests pass but I still get the output to STDERR when the process method is called. I guess I'm just looking for a way to suppress the output...

    Or perhaps for a place where someone prints to STDERR or warns.

    Looking at B::S::W 1.11 at CPAN, B::S::W::process() calls $self->_error() with the error message shown. And now look at that method:

    sub _error { my ($self, $message) = @_; $self->{'error'} = $message; if (defined &{$self->{'error'}}) { &{$self->{'error'}}($message); } else { STDERR->print("Stripe Webhook Error: $message\n"); } }

    That's why you shall use warn and die instead of printing to STDERR. sub _warning has the same problem.


    Oh, by the way: eval catches errors, not warnings, so your variable would be better named $got_error instead of $got_warn.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)