Actually, solve this in a lighter-weight way

The templating is very superficial
This commit is contained in:
John Ericson 2022-04-20 17:37:59 +00:00
parent 05ec0beb40
commit f63b0f4540
4 changed files with 40 additions and 46 deletions

View file

@ -10,7 +10,6 @@
#include "topo-sort.hh" #include "topo-sort.hh"
#include "finally.hh" #include "finally.hh"
#include "compression.hh" #include "compression.hh"
#include "sqlite-impl.hh"
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>

View file

@ -1,42 +0,0 @@
#include "sqlite.hh"
#include "globals.hh"
#include "util.hh"
#include <sqlite3.h>
#include <atomic>
namespace nix {
template<typename... Args>
SQLiteError::SQLiteError(const char *path, int errNo, int extendedErrNo, const Args & ... args)
: Error(""), path(path), errNo(errNo), extendedErrNo(extendedErrNo)
{
auto hf = hintfmt(args...);
err.msg = hintfmt("%s: %s (in '%s')",
normaltxt(hf.str()),
sqlite3_errstr(extendedErrNo),
path ? path : "(in-memory)");
}
template<typename... Args>
[[noreturn]] void SQLiteError::throw_(sqlite3 * db, const std::string & fs, const Args & ... args)
{
int err = sqlite3_errcode(db);
int exterr = sqlite3_extended_errcode(db);
auto path = sqlite3_db_filename(db, nullptr);
if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) {
auto exp = SQLiteBusy(path, err, exterr, fs, args...);
exp.err.msg = hintfmt(
err == SQLITE_PROTOCOL
? "SQLite database '%s' is busy (SQLITE_PROTOCOL)"
: "SQLite database '%s' is busy",
path ? path : "(in-memory)");
throw exp;
} else
throw SQLiteError(path, err, exterr, fs, args...);
}
}

View file

@ -1,5 +1,4 @@
#include "sqlite.hh" #include "sqlite.hh"
#include "sqlite-impl.hh"
#include "globals.hh" #include "globals.hh"
#include "util.hh" #include "util.hh"
@ -9,6 +8,34 @@
namespace nix { namespace nix {
SQLiteError::SQLiteError(const char *path, int errNo, int extendedErrNo, hintformat && hf)
: Error(""), path(path), errNo(errNo), extendedErrNo(extendedErrNo)
{
err.msg = hintfmt("%s: %s (in '%s')",
normaltxt(hf.str()),
sqlite3_errstr(extendedErrNo),
path ? path : "(in-memory)");
}
[[noreturn]] void SQLiteError::throw_(sqlite3 * db, hintformat && hf)
{
int err = sqlite3_errcode(db);
int exterr = sqlite3_extended_errcode(db);
auto path = sqlite3_db_filename(db, nullptr);
if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) {
auto exp = SQLiteBusy(path, err, exterr, std::move(hf));
exp.err.msg = hintfmt(
err == SQLITE_PROTOCOL
? "SQLite database '%s' is busy (SQLITE_PROTOCOL)"
: "SQLite database '%s' is busy",
path ? path : "(in-memory)");
throw exp;
} else
throw SQLiteError(path, err, exterr, std::move(hf));
}
SQLite::SQLite(const Path & path, bool create) SQLite::SQLite(const Path & path, bool create)
{ {
// useSQLiteWAL also indicates what virtual file system we need. Using // useSQLiteWAL also indicates what virtual file system we need. Using

View file

@ -102,11 +102,21 @@ struct SQLiteError : Error
int errNo, extendedErrNo; int errNo, extendedErrNo;
template<typename... Args> template<typename... Args>
[[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args); [[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args) {
throw_(db, hintfmt(fs, args...));
}
protected: protected:
SQLiteError(const char *path, int errNo, int extendedErrNo, hintformat && hf);
template<typename... Args> template<typename... Args>
SQLiteError(const char *path, int errNo, int extendedErrNo, const Args & ... args); SQLiteError(const char *path, int errNo, int extendedErrNo, const std::string & fs, const Args & ... args)
: SQLiteError(path, errNo, extendedErrNo, hintfmt(fs, args...))
{ }
[[noreturn]] static void throw_(sqlite3 * db, hintformat && hf);
}; };
MakeError(SQLiteBusy, SQLiteError); MakeError(SQLiteBusy, SQLiteError);