aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBen Burdette <bburdette@gmail.com>2020-05-06 14:07:20 -0600
committerBen Burdette <bburdette@gmail.com>2020-05-06 14:07:20 -0600
commite76ad2e48a71aa9804311f4dfb34374a5295b3b6 (patch)
tree1f15bd925884c5353a456e5e622f81436ab4f3ba /src
parent7ffb5efdbc943851d2ee9d0573dca3e96b9bd742 (diff)
implement SysError errno handling
Diffstat (limited to 'src')
-rw-r--r--src/error-demo/error-demo.cc8
-rw-r--r--src/libutil/error.cc8
-rw-r--r--src/libutil/error.hh13
-rw-r--r--src/libutil/fmt.hh8
-rw-r--r--src/libutil/util.cc3
5 files changed, 21 insertions, 19 deletions
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<string> 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<typename... Args>
SysError(const Args & ... args)
- : Error(args...) // TODO addErrNo for hintfmt
- // : Error(addErrno(hintfmt(args...)))
- { }
-
-private:
+ :Error("")
+ {
+ errNo = errno;
+ auto hf = hintfmt(args...);
+ err.hint = hintfmt("%1% : %2%", normaltxt(hf.str()), strerror(errNo));
+ }
- 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 <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>
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);
}