diff --git a/src/libutil/error.cc b/src/libutil/error.cc index 71f422622..81c1f1805 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -41,8 +41,6 @@ void print_code_lines(string &prefix, NixCode &nix_code) % nix_code.errLine->errLineOfCode << endl; - - // error arrows for the column range. if (nix_code.errLine->columnRange.has_value()) { @@ -98,7 +96,7 @@ void print_error(ErrorInfo &einfo) } } - int ndl = level_string.length() + 3 + einfo.errName.length() + einfo.toolName.length(); + int ndl = level_string.length() + 3 + einfo.name.length() + einfo.program.length(); int dashwidth = errwidth - 3 ? 3 : 80 - ndl; string dashes; @@ -110,9 +108,9 @@ void print_error(ErrorInfo &einfo) % prefix % level_string % "---" - % einfo.errName + % einfo.name % dashes - % einfo.toolName + % einfo.program << endl; // filename. diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 8dac1508e..69776eb8c 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -39,13 +39,40 @@ class NixCode { class ErrorInfo { public: ErrLevel level; - string errName; + string name; string description; - string toolName; + string program; optional nixCode; string hint; + ErrorInfo& GetEI() { return *this; } }; +template +class AddName : private T +{ + public: + T& name(const std::string &name){ + GetEI().name = name; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } +}; + +template +class AddDescription : private T +{ + public: + T& description(const std::string &description){ + GetEI().description = description; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } +}; + +typedef AddName> StandardError; + string showErrLine(ErrLine &errLine); void print_code_lines(string &prefix, NixCode &nix_code); diff --git a/tests/errors/main.cc b/tests/errors/main.cc index 1c998103e..0e6d781c9 100644 --- a/tests/errors/main.cc +++ b/tests/errors/main.cc @@ -24,13 +24,18 @@ using namespace nix; ErrorInfo generic; generic.level = elError; - generic.errName = "error name"; + generic.name = "error name"; generic.description = "general error description"; - generic.toolName = "nixtool.exe"; + generic.program = "nixtool.exe"; generic.nixCode = nixcode; print_error(generic); + + StandardError standardError; + + print_error(standardError.name("blah").description("blah")); + return 0; }