diff --git a/src/error-demo/error-demo.cc b/src/error-demo/error-demo.cc index 82b03d71b..b1b313e32 100644 --- a/src/error-demo/error-demo.cc +++ b/src/error-demo/error-demo.cc @@ -35,6 +35,14 @@ int main() logger->logEI(ei); } + // SysError; picks up errno + try { + auto x = readFile(-1); + } + catch (Error &e) { + std::cout << "error: " << e.sname() << std::endl; + logError(e.info()); + } // For completeness sake, info through vomit levels. // But this is maybe a heavy format for those. diff --git a/src/libutil/error.cc b/src/libutil/error.cc index 91fa1ccd8..d4305ddd8 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -16,14 +16,6 @@ BaseError & BaseError::addPrefix(const FormatOrString & fs) return *this; } - -std::string SysError::addErrno(const std::string & s) -{ - errNo = errno; - return s + ": " + strerror(errNo); -} - - std::optional ErrorInfo::programName = std::nullopt; std::ostream& operator<<(std::ostream &os, const hintformat &hf) diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 86cff5609..2155ad344 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -160,13 +160,14 @@ public: template SysError(const Args & ... args) - : Error(args...) // TODO addErrNo for hintfmt - // : Error(addErrno(hintfmt(args...))) - { } + :Error("") + { + errNo = errno; + auto hf = hintfmt(args...); + err.hint = hintfmt("%1% : %2%", normaltxt(hf.str()), strerror(errNo)); + } -private: - - std::string addErrno(const std::string & s); + virtual const char* sname() const override { return "SysError"; } }; } diff --git a/src/libutil/fmt.hh b/src/libutil/fmt.hh index bfacfeb4d..12ab9c407 100644 --- a/src/libutil/fmt.hh +++ b/src/libutil/fmt.hh @@ -75,8 +75,8 @@ inline std::string fmt(const std::string & fs, const Args & ... args) template struct yellowtxt { - yellowtxt(T &s) : value(s) {} - T &value; + yellowtxt(const T &s) : value(s) {} + const T &value; }; template @@ -88,8 +88,8 @@ std::ostream& operator<<(std::ostream &out, const yellowtxt &y) template struct normaltxt { - normaltxt(T &s) : value(s) {} - T &value; + normaltxt(const T &s) : value(s) {} + const T &value; }; template diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 68dc1b738..e8f22ab71 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -312,7 +312,8 @@ string readFile(const Path & path, bool drain) void readFile(const Path & path, Sink & sink) { AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); - if (!fd) throw SysError("opening file '%s'", path); + if (!fd) + throw SysError("opening file '%s'", path); drainFD(fd.get(), sink); }