From 6a420d672ca690ef4235ac7a5833c1789a7d8b10 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Wed, 20 May 2020 22:18:26 -0600 Subject: print LOC for stdin, string args --- src/libutil/error.hh | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index b374c2780..d60c0fefe 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -32,10 +32,18 @@ typedef enum { lvlVomit } Verbosity; +typedef enum { + foFile, + foStdin, + foString +} FileOrigin; + + struct ErrPos { int line = 0; int column = 0; string file; + FileOrigin origin; operator bool() const { @@ -45,6 +53,7 @@ struct ErrPos { template ErrPos& operator=(const P &pos) { + origin = pos.origin; line = pos.line; column = pos.column; file = pos.file; -- cgit v1.2.3 From 4d1a4f02178b1f77a4bcf2de0483500d89c1424c Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Thu, 18 Jun 2020 15:25:26 -0600 Subject: addTrace --- src/libutil/error.hh | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 8494b9412..10bdeea1d 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -25,20 +25,20 @@ namespace nix { /* -This file defines two main structs/classes used in nix error handling. + This file defines two main structs/classes used in nix error handling. -ErrorInfo provides a standard payload of error information, with conversion to string -happening in the logger rather than at the call site. + ErrorInfo provides a standard payload of error information, with conversion to string + happening in the logger rather than at the call site. -BaseError is the ancestor of nix specific exceptions (and Interrupted), and contains -an ErrorInfo. + BaseError is the ancestor of nix specific exceptions (and Interrupted), and contains + an ErrorInfo. -ErrorInfo structs are sent to the logger as part of an exception, or directly with the -logError or logWarning macros. + ErrorInfo structs are sent to the logger as part of an exception, or directly with the + logError or logWarning macros. -See the error-demo.cc program for usage examples. + See the error-demo.cc program for usage examples. -*/ + */ typedef enum { lvlError = 0, @@ -50,7 +50,7 @@ typedef enum { lvlVomit } Verbosity; -typedef enum { +typedef enum { foFile, foStdin, foString @@ -93,12 +93,18 @@ struct NixCode { std::optional nextLineOfCode; }; +struct Trace { + ErrPos pos; + hintformat hint; +}; + struct ErrorInfo { Verbosity level; string name; string description; std::optional hint; std::optional nixCode; + std::list traces; static std::optional programName; }; @@ -121,23 +127,23 @@ public: template BaseError(unsigned int status, const Args & ... args) - : err { .level = lvlError, - .hint = hintfmt(args...) - } + : err {.level = lvlError, + .hint = hintfmt(args...) + } , status(status) { } template BaseError(const std::string & fs, const Args & ... args) - : err { .level = lvlError, - .hint = hintfmt(fs, args...) - } + : err {.level = lvlError, + .hint = hintfmt(fs, args...) + } { } BaseError(hintformat hint) - : err { .level = lvlError, - .hint = hint - } + : err {.level = lvlError, + .hint = hint + } { } BaseError(ErrorInfo && e) @@ -160,6 +166,7 @@ public: const string & msg() const { return calcWhat(); } const string & prefix() const { return prefix_; } BaseError & addPrefix(const FormatOrString & fs); + BaseError & addTrace(ErrPos e, hintformat hint); const ErrorInfo & info() { calcWhat(); return err; } }; @@ -181,7 +188,7 @@ public: template SysError(const Args & ... args) - :Error("") + : Error("") { errNo = errno; auto hf = hintfmt(args...); -- cgit v1.2.3 From 54e8f550c9d5cc88c1161035d366871bb82d4a0c Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Fri, 19 Jun 2020 13:44:08 -0600 Subject: addErrorTrace --- src/libutil/error.hh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 10bdeea1d..75ba80365 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -94,7 +94,7 @@ struct NixCode { }; struct Trace { - ErrPos pos; + std::optional pos; hintformat hint; }; @@ -116,9 +116,12 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo); class BaseError : public std::exception { protected: - string prefix_; // used for location traces etc. + // string prefix_; // used for location traces etc. mutable ErrorInfo err; + // mutable std::optional trace_; + // const string& calcTrace() const; + mutable std::optional what_; const string& calcWhat() const; @@ -164,9 +167,9 @@ public: #endif const string & msg() const { return calcWhat(); } - const string & prefix() const { return prefix_; } - BaseError & addPrefix(const FormatOrString & fs); - BaseError & addTrace(ErrPos e, hintformat hint); + // const string & trace() const { return calcTrace(); } + // BaseError & addPrefix(const FormatOrString & fs); + BaseError & addTrace(std::optional e, hintformat hint); const ErrorInfo & info() { calcWhat(); return err; } }; -- cgit v1.2.3 From 1d43a6e123e679112dfdfda393e3b6eef99eecf6 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Tue, 23 Jun 2020 15:30:13 -0600 Subject: use plain errPos instead of nixCode; fix tests --- src/libutil/error.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 75ba80365..d992c4ece 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -103,7 +103,7 @@ struct ErrorInfo { string name; string description; std::optional hint; - std::optional nixCode; + std::optional errPos; std::list traces; static std::optional programName; -- cgit v1.2.3 From 00fe653ea5bcc9b130107919d3238f6f7da0808d Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Wed, 24 Jun 2020 08:33:53 -0600 Subject: nixCode -> LinesOfCode --- src/libutil/error.hh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index d992c4ece..7185033f5 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -56,6 +56,13 @@ typedef enum { foString } FileOrigin; +// the lines of code surrounding an error. +struct LinesOfCode { + std::optional prevLineOfCode; + std::optional errLineOfCode; + std::optional nextLineOfCode; +}; + // ErrPos indicates the location of an error in a nix file. struct ErrPos { int line = 0; @@ -86,13 +93,6 @@ struct ErrPos { } }; -struct NixCode { - ErrPos errPos; - std::optional prevLineOfCode; - std::optional errLineOfCode; - std::optional nextLineOfCode; -}; - struct Trace { std::optional pos; hintformat hint; -- cgit v1.2.3 From b18ed02b768560cf5551149a983afdb1e3c8053c Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Wed, 24 Jun 2020 13:10:41 -0600 Subject: repl indenting --- src/libutil/error.hh | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 7185033f5..3879d9b5f 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -119,9 +119,6 @@ protected: // string prefix_; // used for location traces etc. mutable ErrorInfo err; - // mutable std::optional trace_; - // const string& calcTrace() const; - mutable std::optional what_; const string& calcWhat() const; @@ -167,11 +164,9 @@ public: #endif const string & msg() const { return calcWhat(); } - // const string & trace() const { return calcTrace(); } - // BaseError & addPrefix(const FormatOrString & fs); - BaseError & addTrace(std::optional e, hintformat hint); - const ErrorInfo & info() { calcWhat(); return err; } + + BaseError & addTrace(std::optional e, hintformat hint); }; #define MakeError(newClass, superClass) \ -- cgit v1.2.3 From 023912def37c8db64dec0339d39f2535e0d79e78 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Wed, 24 Jun 2020 13:46:25 -0600 Subject: convenience form of addTrace --- src/libutil/error.hh | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 3879d9b5f..09fc57fee 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -166,6 +166,12 @@ public: const string & msg() const { return calcWhat(); } const ErrorInfo & info() { calcWhat(); return err; } + template + BaseError & addTrace(std::optional e, const string &fs, const Args & ... args) + { + return addTrace(e, hintfmt(fs, args...)); + } + BaseError & addTrace(std::optional e, hintformat hint); }; -- cgit v1.2.3 From 9c0e1fd4f1151f4780c1ff3db82322d8b28d2d8b Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Wed, 24 Jun 2020 18:31:28 -0600 Subject: add trace test; error formatting refinements --- src/libutil/error.hh | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 09fc57fee..d79ff8f8c 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -173,6 +173,8 @@ public: } BaseError & addTrace(std::optional e, hintformat hint); + + bool hasTrace() const { return !err.traces.empty(); } }; #define MakeError(newClass, superClass) \ -- cgit v1.2.3 From 9ab808c92613b59a72a4d15cfda1adb6aa523e28 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Thu, 25 Jun 2020 09:23:12 -0600 Subject: showTrace flag for ErrorInfo; showTrace test. --- src/libutil/error.hh | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index d79ff8f8c..dcde28453 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -107,6 +107,7 @@ struct ErrorInfo { std::list traces; static std::optional programName; + static bool showTrace; }; std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo); -- cgit v1.2.3 From ef24a0835d16baf1aa3f19ffa7ba7af54e6e63e6 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Sat, 27 Jun 2020 12:19:31 -0600 Subject: showtrace as function arg --- src/libutil/error.hh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index dcde28453..a9da72b54 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -107,10 +107,11 @@ struct ErrorInfo { std::list traces; static std::optional programName; - static bool showTrace; + // static bool showTrace; }; -std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo); +// std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo); +std::ostream& showErrorInfo(std::ostream &out, const ErrorInfo &einfo, bool showTrace); /* BaseError should generally not be caught, as it has Interrupted as a subclass. Catch Error instead. */ -- cgit v1.2.3 From 9159dfe3d8053e0c77d32893e45f8c527ba83076 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Tue, 30 Jun 2020 16:31:55 -0600 Subject: comments and cleanup --- src/libutil/error.hh | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 4e86666fe..1b0fb43b8 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -111,10 +111,8 @@ struct ErrorInfo { std::list traces; static std::optional programName; - // static bool showTrace; }; -// std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo); std::ostream& showErrorInfo(std::ostream &out, const ErrorInfo &einfo, bool showTrace); /* BaseError should generally not be caught, as it has Interrupted as @@ -122,7 +120,6 @@ std::ostream& showErrorInfo(std::ostream &out, const ErrorInfo &einfo, bool show class BaseError : public std::exception { protected: - // string prefix_; // used for location traces etc. mutable ErrorInfo err; mutable std::optional what_; -- cgit v1.2.3 From 4055cfee3643ecf921e7c3e1b6ad7388271460d8 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 7 Jul 2020 14:37:47 +0200 Subject: Fix coverage build --- src/libutil/error.hh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/libutil/error.hh') diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 1b0fb43b8..f4b3f11bb 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -1,8 +1,8 @@ #pragma once - #include "ref.hh" #include "types.hh" +#include "fmt.hh" #include #include @@ -10,7 +10,9 @@ #include #include -#include "fmt.hh" +#include +#include +#include /* Before 4.7, gcc's std::exception uses empty throw() specifiers for * its (virtual) destructor and what() in c++11 mode, in violation of spec -- cgit v1.2.3