implement SysError errno handling

This commit is contained in:
Ben Burdette 2020-05-06 14:07:20 -06:00
parent 7ffb5efdbc
commit e76ad2e48a
5 changed files with 21 additions and 19 deletions

View file

@ -35,6 +35,14 @@ int main()
logger->logEI(ei); 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. // For completeness sake, info through vomit levels.
// But this is maybe a heavy format for those. // But this is maybe a heavy format for those.

View file

@ -16,14 +16,6 @@ BaseError & BaseError::addPrefix(const FormatOrString & fs)
return *this; return *this;
} }
std::string SysError::addErrno(const std::string & s)
{
errNo = errno;
return s + ": " + strerror(errNo);
}
std::optional<string> ErrorInfo::programName = std::nullopt; std::optional<string> ErrorInfo::programName = std::nullopt;
std::ostream& operator<<(std::ostream &os, const hintformat &hf) std::ostream& operator<<(std::ostream &os, const hintformat &hf)

View file

@ -160,13 +160,14 @@ public:
template<typename... Args> template<typename... Args>
SysError(const Args & ... args) SysError(const Args & ... args)
: Error(args...) // TODO addErrNo for hintfmt :Error("")
// : Error(addErrno(hintfmt(args...))) {
{ } errNo = errno;
auto hf = hintfmt(args...);
err.hint = hintfmt("%1% : %2%", normaltxt(hf.str()), strerror(errNo));
}
private: virtual const char* sname() const override { return "SysError"; }
std::string addErrno(const std::string & s);
}; };
} }

View file

@ -75,8 +75,8 @@ inline std::string fmt(const std::string & fs, const Args & ... args)
template <class T> template <class T>
struct yellowtxt struct yellowtxt
{ {
yellowtxt(T &s) : value(s) {} yellowtxt(const T &s) : value(s) {}
T &value; const T &value;
}; };
template <class T> template <class T>
@ -88,8 +88,8 @@ std::ostream& operator<<(std::ostream &out, const yellowtxt<T> &y)
template <class T> template <class T>
struct normaltxt struct normaltxt
{ {
normaltxt(T &s) : value(s) {} normaltxt(const T &s) : value(s) {}
T &value; const T &value;
}; };
template <class T> template <class T>

View file

@ -312,7 +312,8 @@ string readFile(const Path & path, bool drain)
void readFile(const Path & path, Sink & sink) void readFile(const Path & path, Sink & sink)
{ {
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); 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); drainFD(fd.get(), sink);
} }