From 04c6590ab865a64ad9abc802bf7f32c65831ec43 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 3 Dec 2023 08:36:50 +1300 Subject: [PATCH] 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 --- std/exception.d | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/std/exception.d b/std/exception.d index 5c786859c..58a667c1d 100644 --- a/std/exception.d +++ b/std/exception.d @@ -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); } }