1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 23:03:53 -04:00

Error classname as name

This commit is contained in:
Ben Burdette 2020-04-28 21:06:08 -06:00
parent e51a757720
commit 22e6490311
5 changed files with 55 additions and 41 deletions

View file

@ -4,10 +4,12 @@
#include <iostream> #include <iostream>
#include <optional> #include <optional>
int main()
{
using namespace nix; using namespace nix;
MakeError(DemoError, Error);
int main()
{
makeDefaultLogger(); makeDefaultLogger();
verbosity = lvlVomit; verbosity = lvlVomit;
@ -15,6 +17,12 @@ int main()
// In each program where errors occur, this has to be set. // In each program where errors occur, this has to be set.
ErrorInfo::programName = std::optional("error-demo"); ErrorInfo::programName = std::optional("error-demo");
try {
throw DemoError("demo error was thrown");
} catch (Error &e) {
logger->logEI(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.
logger->logEI( logger->logEI(

View file

@ -196,7 +196,7 @@ SQLiteTxn::~SQLiteTxn()
} }
} }
void handleSQLiteBusy(const SQLiteBusy & e) void handleSQLiteBusy(SQLiteBusy & e)
{ {
static std::atomic<time_t> lastWarned{0}; static std::atomic<time_t> lastWarned{0};

View file

@ -99,7 +99,7 @@ MakeError(SQLiteBusy, SQLiteError);
[[noreturn]] void throwSQLiteError(sqlite3 * db, const FormatOrString & fs); [[noreturn]] void throwSQLiteError(sqlite3 * db, const FormatOrString & fs);
void handleSQLiteBusy(const SQLiteBusy & e); void handleSQLiteBusy(SQLiteBusy & e);
/* Convenience function for retrying a SQLite transaction when the /* Convenience function for retrying a SQLite transaction when the
database is busy. */ database is busy. */

View file

@ -4,8 +4,7 @@
#include <optional> #include <optional>
#include "serialise.hh" #include "serialise.hh"
namespace nix namespace nix {
{
const std::string nativeSystem = SYSTEM; const std::string nativeSystem = SYSTEM;

View file

@ -24,8 +24,7 @@ namespace nix {
using std::list; using std::list;
using std::vector; using std::vector;
typedef enum typedef enum {
{
lvlError = 0, lvlError = 0,
lvlWarn, lvlWarn,
lvlInfo, lvlInfo,
@ -35,8 +34,7 @@ typedef enum
lvlVomit lvlVomit
} Verbosity; } Verbosity;
struct ErrPos struct ErrPos {
{
int line; int line;
int column; int column;
string file; string file;
@ -57,8 +55,7 @@ struct ErrPos
} }
}; };
struct NixCode struct NixCode {
{
ErrPos errPos; ErrPos errPos;
std::optional<string> prevLineOfCode; std::optional<string> prevLineOfCode;
string errLineOfCode; string errLineOfCode;
@ -67,8 +64,7 @@ struct NixCode
// ------------------------------------------------- // -------------------------------------------------
// ErrorInfo. // ErrorInfo.
struct ErrorInfo struct ErrorInfo {
{
Verbosity level; Verbosity level;
string name; string name;
string description; string description;
@ -87,12 +83,20 @@ class BaseError : public std::exception
protected: protected:
string prefix_; // used for location traces etc. string prefix_; // used for location traces etc.
ErrorInfo err; ErrorInfo err;
string what_; std::optional<string> what_;
void initWhat() const string& calcWhat()
{ {
if (what_.has_value())
return *what_;
else {
err.name = sname();
std::ostringstream oss; std::ostringstream oss;
oss << err; oss << err;
what_ = oss.str(); what_ = oss.str();
return *what_;
}
} }
public: public:
unsigned int status = 1; // exit status unsigned int status = 1; // exit status
@ -103,31 +107,33 @@ public:
.hint = hintfmt(args...) .hint = hintfmt(args...)
} }
, status(status) , status(status)
{ initWhat(); } { }
template<typename... Args> template<typename... Args>
BaseError(const Args & ... args) BaseError(const Args & ... args)
: err { .level = lvlError, : err { .level = lvlError,
.hint = hintfmt(args...) .hint = hintfmt(args...)
} }
{ initWhat(); } { }
BaseError(ErrorInfo e) BaseError(ErrorInfo e)
: err(e) : err(e)
{ initWhat(); } { }
virtual const char* sname() const { return "BaseError"; }
#ifdef EXCEPTION_NEEDS_THROW_SPEC #ifdef EXCEPTION_NEEDS_THROW_SPEC
~BaseError() throw () { }; ~BaseError() throw () { };
const char * what() const throw () { return what_.c_str(); } const char * what() throw () { return calcWhat().c_str(); }
#else #else
const char * what() const noexcept { return what_.c_str(); } const char * what() noexcept { return calcWhat().c_str(); }
#endif #endif
const string & msg() const { return what_; } const string & msg() { return calcWhat(); }
const string & prefix() const { return prefix_; } const string & prefix() const { return prefix_; }
BaseError & addPrefix(const FormatOrString & fs); BaseError & addPrefix(const FormatOrString & fs);
const ErrorInfo & info() const { return err; } const ErrorInfo & info() { calcWhat(); return err; }
}; };
#define MakeError(newClass, superClass) \ #define MakeError(newClass, superClass) \
@ -135,6 +141,7 @@ public:
{ \ { \
public: \ public: \
using superClass::superClass; \ using superClass::superClass; \
virtual const char* sname() const override { return #newClass; } \
} }
MakeError(Error, BaseError); MakeError(Error, BaseError);