in reply to Re^3: STDERR in Test Results
in thread STDERR in Test Results
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
Hence why I referred to errorsStripe Webhook Error: Invalid Stripe Signature
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: STDERR in Test Results
by afoken (Chancellor) on Jun 25, 2023 at 22:02 UTC | |
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:
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". ;-) | [reply] [d/l] [select] |
by Bod (Parson) on Jun 26, 2023 at 19:48 UTC | |
To throw errors, call die. To throw warnings, call warn The next version of the module will call warn so I can trap the output with Test::Warn However, I will not call die. I find it frustrating when modules die. Especially modules where there is a reasonable chance that there may be an error such as when they are dealing with a network or rely on the user to provide input in a particular format such as valid JSON - I attempt not to inflict that frustration on others. | [reply] [d/l] [select] |
by afoken (Chancellor) on Jun 27, 2023 at 21:14 UTC | |
However, I will not call die. I find it frustrating when modules die. WTF? Especially modules where there is a reasonable chance that there may be an error such as when they are dealing with a network or rely on the user to provide input in a particular format such as valid JSON So, what should your module do when being fed with garbage instead of sane input? Pretend nothing evil has happened? Invent other, sane input? Ask for more garbage to be stuffed into your module? What should it do if the network goes down or a required service somewhere on the internet can't be reached? Wait for the heat death of the universe? What should it do if the system runs out of disk space? Gain root privileges and recursively delete all files not used in the last three years? I attempt not to inflict that frustration on others. Get over it. There is nothing to be frustrated about if an exception occurs (i.e. die is called). It is completely normal, it is "just" another way of control flow. The butt-ugly alternative "solution" is to return some kind of error information in-band and check each and every function call result for error information. In other words, C:
Update: Yes, you could implement a kind of exception handling in C using setjmp/sigsetjmp and longjmp/siglongjmp. Be prepared for "interesting" results due to undefined behaviour ... The smart way is to accept that things can go wrong:
(Pseudocode-example stolen from Exception handling.) This can be very useful e.g. if you need to work with a database:
(Ripped right out of the DBI documentation.) Update: Compare with "Exceptions" in Image::Magick: You have to check each and every return value, and you need to do that in at least three different ways, depending on what the function is expected to return. There is NO exception handing in Image::Magick. The main point is that if you accidentally forget to check for errors, your programm will die. It won't chew on garbage that happens to linger around somewhere in memory and it won't produce any more garbage. It will just stop and exit with an error message. In C, omitting error checks will cause a lot of "interesting" results instead. Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] [d/l] [select] |
by eyepopslikeamosquito (Archbishop) on Jun 27, 2023 at 23:55 UTC | |
by bliako (Abbot) on Jul 17, 2023 at 21:59 UTC | |
by stevieb (Canon) on Jun 28, 2023 at 02:53 UTC | |
However, I will not call die. I agree with afoken's "WTF" here. There are many instances where it would be dangerous to NOT throw an exception. For example... my webapp that front-ends a connection to Tesla's API... you hammer that too hard without a proper auth token and I don't throw, that may cause issues. I'm not going to nicely keep telling you to please don't do that, I'm breaking your caller. Another example is my IPC::Shareable software. If you try to write to a memory location that isn't yours, you should know better, and I'm throwing an exception. The case that's relative here is the test software. Your tests should use a liberal amount of throws_ok() or similar to ensure that you catch as many possible exception cases as your software throws. Here's an example use of throws_ok():
You can do it without Test::Exception with a bit more work:
| [reply] [d/l] [select] |