Closed ysbaddaden closed 5 days ago
I'm wondering if we could simplify the implementation a bit? The code seems to be mostly identical across platforms with the only difference in retrieving the error message allocation-free.
Perhaps we could extract this for Errno
and WinError
(WasiError.message
already does not allocate).
Some APIs on Windows return Errno
, so it might make sense to have a fixed API like SystemError
, accepting any system error value (Errno | WinError | WasiError
).
@straight-shoota You're right. I thought I couldn't abstract a method without allocating... but all I had to do was yield. The method also takes an explicit Errno|WinError|WasiError
param. That simplified the panic method a lot.
Note: a follow-up PR will introduce strerror_r
(thread-safe) instead of strerror
(thread unsafe) when available.
Why is this under the Crystal::System
namespace?
Some languages like go have panic
way more accessible.
It's a low level primitive for stdlib implmentations. It's probably not very practical for user code. Error handling is usually intended to work with exceptions. And .abort
is a similar, public method for exiting directly with an error message.
That makes sense. Thanks for the explanation.
Prints a system error message on the standard error then exits with an error status. Raising an exception should always be preferred but there are a few cases where we can't allocate any memory (e.g. stop the world) and still need to fail when reaching a system error.
Extracted from #14729