Add errnoMsg property to ErrnoException

This is used, for example, when you'd want to get a raw error message
from the errno number given by the underlying system call/C function,
instead of using the msg property, which includes extra (often irrelevant)
error information given by the called Phobos function.

Previously you would have to use something like this:
    strerror(e.errno).fromStringz()

Example:
> catch (ErrnoException e) writeln(e.msg);
Cannot open file `filename' in mode `r' (No such file or directory)
> catch (ErrnoException e) writeln(e.errnoMsg);
No such file or directory
This commit is contained in:
Jeremy 2023-12-03 08:36:50 +13:00
parent b64bfbf911
commit 04c6590ab8

View file

@ -1632,6 +1632,9 @@ class ErrnoException : Exception
/// Operating system error code.
final @property uint errno() nothrow pure scope @nogc @safe { return _errno; }
private uint _errno;
/// Localized error message generated through $(REF strerror_r, core,stdc,string) or $(REF strerror, core,stdc,string).
final @property string errnoMsg() nothrow pure scope @nogc @safe { return _errnoMsg; }
private string _errnoMsg;
/// Constructor which takes an error message. The current global $(REF errno, core,stdc,errno) value is used as error code.
this(string msg, string file = null, size_t line = 0) @safe
{
@ -1642,7 +1645,8 @@ class ErrnoException : Exception
this(string msg, int errno, string file = null, size_t line = 0) @safe
{
_errno = errno;
super(msg ~ " (" ~ errnoString(errno) ~ ")", file, line);
_errnoMsg = errnoString(errno);
super(msg ~ " (" ~ errnoMsg ~ ")", file, line);
}
}