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);
}
// 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.

View file

@ -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<string> ErrorInfo::programName = std::nullopt;
std::ostream& operator<<(std::ostream &os, const hintformat &hf)

View file

@ -160,13 +160,14 @@ public:
template<typename... Args>
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"; }
};
}

View file

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

View file

@ -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);
}