mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-30 01:26:15 +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 <iostream>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
using namespace nix;
|
||||||
|
|
||||||
|
MakeError(DemoError, Error);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
using namespace nix;
|
|
||||||
|
|
||||||
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(
|
||||||
|
@ -79,7 +87,7 @@ int main()
|
||||||
.prevLineOfCode = std::nullopt,
|
.prevLineOfCode = std::nullopt,
|
||||||
.errLineOfCode = "this is the problem line of code",
|
.errLineOfCode = "this is the problem line of code",
|
||||||
.nextLineOfCode = std::nullopt
|
.nextLineOfCode = std::nullopt
|
||||||
}});
|
}});
|
||||||
|
|
||||||
// Error with previous and next lines of code.
|
// Error with previous and next lines of code.
|
||||||
logError(
|
logError(
|
||||||
|
@ -93,7 +101,7 @@ int main()
|
||||||
.prevLineOfCode = std::optional("previous line of code"),
|
.prevLineOfCode = std::optional("previous line of code"),
|
||||||
.errLineOfCode = "this is the problem line of code",
|
.errLineOfCode = "this is the problem line of code",
|
||||||
.nextLineOfCode = std::optional("next line of code"),
|
.nextLineOfCode = std::optional("next line of code"),
|
||||||
}});
|
}});
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -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};
|
||||||
|
|
||||||
|
|
|
@ -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. */
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -149,20 +148,20 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
|
||||||
|
|
||||||
// divider.
|
// divider.
|
||||||
if (einfo.name != "")
|
if (einfo.name != "")
|
||||||
out << fmt("%1%%2%" ANSI_BLUE " --- %3% %4% %5%" ANSI_NORMAL,
|
out << fmt("%1%%2%" ANSI_BLUE " --- %3% %4% %5%" ANSI_NORMAL,
|
||||||
prefix,
|
prefix,
|
||||||
levelString,
|
levelString,
|
||||||
einfo.name,
|
einfo.name,
|
||||||
dashes,
|
dashes,
|
||||||
einfo.programName.value_or(""))
|
einfo.programName.value_or(""))
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
else
|
else
|
||||||
out << fmt("%1%%2%" ANSI_BLUE " -----%3% %4%" ANSI_NORMAL,
|
out << fmt("%1%%2%" ANSI_BLUE " -----%3% %4%" ANSI_NORMAL,
|
||||||
prefix,
|
prefix,
|
||||||
levelString,
|
levelString,
|
||||||
dashes,
|
dashes,
|
||||||
einfo.programName.value_or(""))
|
einfo.programName.value_or(""))
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
// filename.
|
// filename.
|
||||||
if (einfo.nixCode.has_value()) {
|
if (einfo.nixCode.has_value()) {
|
||||||
|
|
|
@ -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()
|
||||||
{
|
{
|
||||||
std::ostringstream oss;
|
if (what_.has_value())
|
||||||
oss << err;
|
return *what_;
|
||||||
what_ = oss.str();
|
else {
|
||||||
|
err.name = sname();
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << err;
|
||||||
|
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);
|
||||||
|
|
Loading…
Reference in a new issue