mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-26 07:46:21 +02:00
Error classname as name
This commit is contained in:
parent
e51a757720
commit
22e6490311
5 changed files with 55 additions and 41 deletions
|
@ -4,10 +4,12 @@
|
|||
#include <iostream>
|
||||
#include <optional>
|
||||
|
||||
using namespace nix;
|
||||
|
||||
MakeError(DemoError, Error);
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace nix;
|
||||
|
||||
makeDefaultLogger();
|
||||
|
||||
verbosity = lvlVomit;
|
||||
|
@ -15,6 +17,12 @@ int main()
|
|||
// In each program where errors occur, this has to be set.
|
||||
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.
|
||||
// But this is maybe a heavy format for those.
|
||||
logger->logEI(
|
||||
|
@ -79,7 +87,7 @@ int main()
|
|||
.prevLineOfCode = std::nullopt,
|
||||
.errLineOfCode = "this is the problem line of code",
|
||||
.nextLineOfCode = std::nullopt
|
||||
}});
|
||||
}});
|
||||
|
||||
// Error with previous and next lines of code.
|
||||
logError(
|
||||
|
@ -93,7 +101,7 @@ int main()
|
|||
.prevLineOfCode = std::optional("previous line of code"),
|
||||
.errLineOfCode = "this is the problem line of code",
|
||||
.nextLineOfCode = std::optional("next line of code"),
|
||||
}});
|
||||
}});
|
||||
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -196,7 +196,7 @@ SQLiteTxn::~SQLiteTxn()
|
|||
}
|
||||
}
|
||||
|
||||
void handleSQLiteBusy(const SQLiteBusy & e)
|
||||
void handleSQLiteBusy(SQLiteBusy & e)
|
||||
{
|
||||
static std::atomic<time_t> lastWarned{0};
|
||||
|
||||
|
|
|
@ -99,7 +99,7 @@ MakeError(SQLiteBusy, SQLiteError);
|
|||
|
||||
[[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
|
||||
database is busy. */
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
#include <optional>
|
||||
#include "serialise.hh"
|
||||
|
||||
namespace nix
|
||||
{
|
||||
namespace nix {
|
||||
|
||||
|
||||
const std::string nativeSystem = SYSTEM;
|
||||
|
@ -149,20 +148,20 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
|
|||
|
||||
// divider.
|
||||
if (einfo.name != "")
|
||||
out << fmt("%1%%2%" ANSI_BLUE " --- %3% %4% %5%" ANSI_NORMAL,
|
||||
prefix,
|
||||
levelString,
|
||||
einfo.name,
|
||||
dashes,
|
||||
einfo.programName.value_or(""))
|
||||
<< std::endl;
|
||||
out << fmt("%1%%2%" ANSI_BLUE " --- %3% %4% %5%" ANSI_NORMAL,
|
||||
prefix,
|
||||
levelString,
|
||||
einfo.name,
|
||||
dashes,
|
||||
einfo.programName.value_or(""))
|
||||
<< std::endl;
|
||||
else
|
||||
out << fmt("%1%%2%" ANSI_BLUE " -----%3% %4%" ANSI_NORMAL,
|
||||
prefix,
|
||||
levelString,
|
||||
dashes,
|
||||
einfo.programName.value_or(""))
|
||||
<< std::endl;
|
||||
out << fmt("%1%%2%" ANSI_BLUE " -----%3% %4%" ANSI_NORMAL,
|
||||
prefix,
|
||||
levelString,
|
||||
dashes,
|
||||
einfo.programName.value_or(""))
|
||||
<< std::endl;
|
||||
|
||||
// filename.
|
||||
if (einfo.nixCode.has_value()) {
|
||||
|
|
|
@ -24,8 +24,7 @@ namespace nix {
|
|||
using std::list;
|
||||
using std::vector;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
lvlError = 0,
|
||||
lvlWarn,
|
||||
lvlInfo,
|
||||
|
@ -35,8 +34,7 @@ typedef enum
|
|||
lvlVomit
|
||||
} Verbosity;
|
||||
|
||||
struct ErrPos
|
||||
{
|
||||
struct ErrPos {
|
||||
int line;
|
||||
int column;
|
||||
string file;
|
||||
|
@ -57,8 +55,7 @@ struct ErrPos
|
|||
}
|
||||
};
|
||||
|
||||
struct NixCode
|
||||
{
|
||||
struct NixCode {
|
||||
ErrPos errPos;
|
||||
std::optional<string> prevLineOfCode;
|
||||
string errLineOfCode;
|
||||
|
@ -67,8 +64,7 @@ struct NixCode
|
|||
|
||||
// -------------------------------------------------
|
||||
// ErrorInfo.
|
||||
struct ErrorInfo
|
||||
{
|
||||
struct ErrorInfo {
|
||||
Verbosity level;
|
||||
string name;
|
||||
string description;
|
||||
|
@ -87,12 +83,20 @@ class BaseError : public std::exception
|
|||
protected:
|
||||
string prefix_; // used for location traces etc.
|
||||
ErrorInfo err;
|
||||
string what_;
|
||||
void initWhat()
|
||||
std::optional<string> what_;
|
||||
const string& calcWhat()
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << err;
|
||||
what_ = oss.str();
|
||||
if (what_.has_value())
|
||||
return *what_;
|
||||
else {
|
||||
err.name = sname();
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << err;
|
||||
what_ = oss.str();
|
||||
|
||||
return *what_;
|
||||
}
|
||||
}
|
||||
public:
|
||||
unsigned int status = 1; // exit status
|
||||
|
@ -103,31 +107,33 @@ public:
|
|||
.hint = hintfmt(args...)
|
||||
}
|
||||
, status(status)
|
||||
{ initWhat(); }
|
||||
{ }
|
||||
|
||||
template<typename... Args>
|
||||
BaseError(const Args & ... args)
|
||||
: err { .level = lvlError,
|
||||
.hint = hintfmt(args...)
|
||||
}
|
||||
{ initWhat(); }
|
||||
{ }
|
||||
|
||||
BaseError(ErrorInfo e)
|
||||
: err(e)
|
||||
{ initWhat(); }
|
||||
{ }
|
||||
|
||||
virtual const char* sname() const { return "BaseError"; }
|
||||
|
||||
#ifdef EXCEPTION_NEEDS_THROW_SPEC
|
||||
~BaseError() throw () { };
|
||||
const char * what() const throw () { return what_.c_str(); }
|
||||
const char * what() throw () { return calcWhat().c_str(); }
|
||||
#else
|
||||
const char * what() const noexcept { return what_.c_str(); }
|
||||
const char * what() noexcept { return calcWhat().c_str(); }
|
||||
#endif
|
||||
|
||||
const string & msg() const { return what_; }
|
||||
const string & msg() { return calcWhat(); }
|
||||
const string & prefix() const { return prefix_; }
|
||||
BaseError & addPrefix(const FormatOrString & fs);
|
||||
|
||||
const ErrorInfo & info() const { return err; }
|
||||
const ErrorInfo & info() { calcWhat(); return err; }
|
||||
};
|
||||
|
||||
#define MakeError(newClass, superClass) \
|
||||
|
@ -135,6 +141,7 @@ public:
|
|||
{ \
|
||||
public: \
|
||||
using superClass::superClass; \
|
||||
virtual const char* sname() const override { return #newClass; } \
|
||||
}
|
||||
|
||||
MakeError(Error, BaseError);
|
||||
|
|
Loading…
Reference in a new issue