Avoid fmt when constructor already does it

There is a correctnes issue here, but #3724 will fix that. This is just
a cleanup for brevity's sake.
This commit is contained in:
John Ericson 2020-06-18 17:54:16 +00:00
parent b135de2b5f
commit 75b62e5260
8 changed files with 73 additions and 51 deletions

View file

@ -36,7 +36,7 @@ static std::string runHg(const Strings & args, const std::optional<std::string>
auto res = runProgram(std::move(opts)); auto res = runProgram(std::move(opts));
if (!statusOk(res.first)) if (!statusOk(res.first))
throw ExecError(res.first, fmt("hg %1%", statusToString(res.first))); throw ExecError(res.first, "hg %1%", statusToString(res.first));
return res.second; return res.second;
} }
@ -273,7 +273,7 @@ struct MercurialInputScheme : InputScheme
runHg({ "recover", "-R", cacheDir }); runHg({ "recover", "-R", cacheDir });
runHg({ "pull", "-R", cacheDir, "--", actualUrl }); runHg({ "pull", "-R", cacheDir, "--", actualUrl });
} else { } else {
throw ExecError(e.status, fmt("'hg pull' %s", statusToString(e.status))); throw ExecError(e.status, "'hg pull' %s", statusToString(e.status));
} }
} }
} else { } else {

View file

@ -60,37 +60,37 @@ void printMissing(ref<Store> store, const StorePathSet & willBuild,
{ {
if (!willBuild.empty()) { if (!willBuild.empty()) {
if (willBuild.size() == 1) if (willBuild.size() == 1)
printMsg(lvl, fmt("this derivation will be built:")); printMsg(lvl, "this derivation will be built:");
else else
printMsg(lvl, fmt("these %d derivations will be built:", willBuild.size())); printMsg(lvl, "these %d derivations will be built:", willBuild.size());
auto sorted = store->topoSortPaths(willBuild); auto sorted = store->topoSortPaths(willBuild);
reverse(sorted.begin(), sorted.end()); reverse(sorted.begin(), sorted.end());
for (auto & i : sorted) for (auto & i : sorted)
printMsg(lvl, fmt(" %s", store->printStorePath(i))); printMsg(lvl, " %s", store->printStorePath(i));
} }
if (!willSubstitute.empty()) { if (!willSubstitute.empty()) {
const float downloadSizeMiB = downloadSize / (1024.f * 1024.f); const float downloadSizeMiB = downloadSize / (1024.f * 1024.f);
const float narSizeMiB = narSize / (1024.f * 1024.f); const float narSizeMiB = narSize / (1024.f * 1024.f);
if (willSubstitute.size() == 1) { if (willSubstitute.size() == 1) {
printMsg(lvl, fmt("this path will be fetched (%.2f MiB download, %.2f MiB unpacked):", printMsg(lvl, "this path will be fetched (%.2f MiB download, %.2f MiB unpacked):",
downloadSizeMiB, downloadSizeMiB,
narSizeMiB)); narSizeMiB);
} else { } else {
printMsg(lvl, fmt("these %d paths will be fetched (%.2f MiB download, %.2f MiB unpacked):", printMsg(lvl, "these %d paths will be fetched (%.2f MiB download, %.2f MiB unpacked):",
willSubstitute.size(), willSubstitute.size(),
downloadSizeMiB, downloadSizeMiB,
narSizeMiB)); narSizeMiB);
} }
for (auto & i : willSubstitute) for (auto & i : willSubstitute)
printMsg(lvl, fmt(" %s", store->printStorePath(i))); printMsg(lvl, " %s", store->printStorePath(i));
} }
if (!unknown.empty()) { if (!unknown.empty()) {
printMsg(lvl, fmt("don't know how to build these paths%s:", printMsg(lvl, "don't know how to build these paths%s:",
(settings.readOnlyMode ? " (may be caused by read-only store access)" : ""))); (settings.readOnlyMode ? " (may be caused by read-only store access)" : ""));
for (auto & i : unknown) for (auto & i : unknown)
printMsg(lvl, fmt(" %s", store->printStorePath(i))); printMsg(lvl, " %s", store->printStorePath(i));
} }
} }

View file

@ -443,14 +443,13 @@ struct curlFileTransfer : public FileTransfer
: httpStatus != 0 : httpStatus != 0
? FileTransferError(err, ? FileTransferError(err,
std::move(response), std::move(response),
fmt("unable to %s '%s': HTTP error %d ('%s')", "unable to %s '%s': HTTP error %d%s",
request.verb(), request.uri, httpStatus, statusMsg) request.verb(), request.uri, httpStatus,
+ (code == CURLE_OK ? "" : fmt(" (curl error: %s)", curl_easy_strerror(code))) code == CURLE_OK ? "" : fmt(" (curl error: %s)", curl_easy_strerror(code)))
)
: FileTransferError(err, : FileTransferError(err,
std::move(response), std::move(response),
fmt("unable to %s '%s': %s (%d)", "unable to %s '%s': %s (%d)",
request.verb(), request.uri, curl_easy_strerror(code), code)); request.verb(), request.uri, curl_easy_strerror(code), code);
/* If this is a transient error, then maybe retry the /* If this is a transient error, then maybe retry the
download after a while. If we're writing to a download after a while. If we're writing to a
@ -704,7 +703,7 @@ struct curlFileTransfer : public FileTransfer
auto s3Res = s3Helper.getObject(bucketName, key); auto s3Res = s3Helper.getObject(bucketName, key);
FileTransferResult res; FileTransferResult res;
if (!s3Res.data) if (!s3Res.data)
throw FileTransferError(NotFound, {}, "S3 object '%s' does not exist", request.uri); throw FileTransferError(NotFound, "S3 object '%s' does not exist", request.uri);
res.data = std::move(*s3Res.data); res.data = std::move(*s3Res.data);
callback(std::move(res)); callback(std::move(res));
#else #else

View file

@ -482,18 +482,18 @@ void LocalStore::openDB(State & state, bool create)
SQLiteStmt stmt; SQLiteStmt stmt;
stmt.create(db, "pragma main.journal_mode;"); stmt.create(db, "pragma main.journal_mode;");
if (sqlite3_step(stmt) != SQLITE_ROW) if (sqlite3_step(stmt) != SQLITE_ROW)
throwSQLiteError(db, "querying journal mode"); SQLiteError::throw_(db, "querying journal mode");
prevMode = std::string((const char *) sqlite3_column_text(stmt, 0)); prevMode = std::string((const char *) sqlite3_column_text(stmt, 0));
} }
if (prevMode != mode && if (prevMode != mode &&
sqlite3_exec(db, ("pragma main.journal_mode = " + mode + ";").c_str(), 0, 0, 0) != SQLITE_OK) sqlite3_exec(db, ("pragma main.journal_mode = " + mode + ";").c_str(), 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "setting journal mode"); SQLiteError::throw_(db, "setting journal mode");
/* Increase the auto-checkpoint interval to 40000 pages. This /* Increase the auto-checkpoint interval to 40000 pages. This
seems enough to ensure that instantiating the NixOS system seems enough to ensure that instantiating the NixOS system
derivation is done in a single fsync(). */ derivation is done in a single fsync(). */
if (mode == "wal" && sqlite3_exec(db, "pragma wal_autocheckpoint = 40000;", 0, 0, 0) != SQLITE_OK) if (mode == "wal" && sqlite3_exec(db, "pragma wal_autocheckpoint = 40000;", 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "setting autocheckpoint interval"); SQLiteError::throw_(db, "setting autocheckpoint interval");
/* Initialise the database schema, if necessary. */ /* Initialise the database schema, if necessary. */
if (create) { if (create) {

View file

@ -8,22 +8,35 @@
namespace nix { namespace nix {
[[noreturn]] void throwSQLiteError(sqlite3 * db, const FormatOrString & fs) 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 err = sqlite3_errcode(db);
int exterr = sqlite3_extended_errcode(db); int exterr = sqlite3_extended_errcode(db);
auto path = sqlite3_db_filename(db, nullptr); auto path = sqlite3_db_filename(db, nullptr);
if (!path) path = "(in-memory)";
if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) { if (err == SQLITE_BUSY || err == SQLITE_PROTOCOL) {
throw SQLiteBusy( auto exp = SQLiteBusy(path, err, exterr, fs, args...);
exp.err.msg = hintfmt(
err == SQLITE_PROTOCOL err == SQLITE_PROTOCOL
? fmt("SQLite database '%s' is busy (SQLITE_PROTOCOL)", path) ? "SQLite database '%s' is busy (SQLITE_PROTOCOL)"
: fmt("SQLite database '%s' is busy", path)); : "SQLite database '%s' is busy",
} path ? path : "(in-memory)");
else throw exp;
throw SQLiteError("%s: %s (in '%s')", fs.s, sqlite3_errstr(exterr), path); } else
throw SQLiteError(path, err, exterr, fs, args...);
} }
SQLite::SQLite(const Path & path, bool create) SQLite::SQLite(const Path & path, bool create)
@ -37,7 +50,7 @@ SQLite::SQLite(const Path & path, bool create)
throw Error("cannot open SQLite database '%s'", path); throw Error("cannot open SQLite database '%s'", path);
if (sqlite3_busy_timeout(db, 60 * 60 * 1000) != SQLITE_OK) if (sqlite3_busy_timeout(db, 60 * 60 * 1000) != SQLITE_OK)
throwSQLiteError(db, "setting timeout"); SQLiteError::throw_(db, "setting timeout");
exec("pragma foreign_keys = 1"); exec("pragma foreign_keys = 1");
} }
@ -46,7 +59,7 @@ SQLite::~SQLite()
{ {
try { try {
if (db && sqlite3_close(db) != SQLITE_OK) if (db && sqlite3_close(db) != SQLITE_OK)
throwSQLiteError(db, "closing database"); SQLiteError::throw_(db, "closing database");
} catch (...) { } catch (...) {
ignoreException(); ignoreException();
} }
@ -62,7 +75,7 @@ void SQLite::exec(const std::string & stmt)
{ {
retrySQLite<void>([&]() { retrySQLite<void>([&]() {
if (sqlite3_exec(db, stmt.c_str(), 0, 0, 0) != SQLITE_OK) if (sqlite3_exec(db, stmt.c_str(), 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, format("executing SQLite statement '%s'") % stmt); SQLiteError::throw_(db, "executing SQLite statement '%s'", stmt);
}); });
} }
@ -76,7 +89,7 @@ void SQLiteStmt::create(sqlite3 * db, const std::string & sql)
checkInterrupt(); checkInterrupt();
assert(!stmt); assert(!stmt);
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, 0) != SQLITE_OK) if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, 0) != SQLITE_OK)
throwSQLiteError(db, fmt("creating statement '%s'", sql)); SQLiteError::throw_(db, "creating statement '%s'", sql);
this->db = db; this->db = db;
this->sql = sql; this->sql = sql;
} }
@ -85,7 +98,7 @@ SQLiteStmt::~SQLiteStmt()
{ {
try { try {
if (stmt && sqlite3_finalize(stmt) != SQLITE_OK) if (stmt && sqlite3_finalize(stmt) != SQLITE_OK)
throwSQLiteError(db, fmt("finalizing statement '%s'", sql)); SQLiteError::throw_(db, "finalizing statement '%s'", sql);
} catch (...) { } catch (...) {
ignoreException(); ignoreException();
} }
@ -109,7 +122,7 @@ SQLiteStmt::Use & SQLiteStmt::Use::operator () (std::string_view value, bool not
{ {
if (notNull) { if (notNull) {
if (sqlite3_bind_text(stmt, curArg++, value.data(), -1, SQLITE_TRANSIENT) != SQLITE_OK) if (sqlite3_bind_text(stmt, curArg++, value.data(), -1, SQLITE_TRANSIENT) != SQLITE_OK)
throwSQLiteError(stmt.db, "binding argument"); SQLiteError::throw_(stmt.db, "binding argument");
} else } else
bind(); bind();
return *this; return *this;
@ -119,7 +132,7 @@ SQLiteStmt::Use & SQLiteStmt::Use::operator () (const unsigned char * data, size
{ {
if (notNull) { if (notNull) {
if (sqlite3_bind_blob(stmt, curArg++, data, len, SQLITE_TRANSIENT) != SQLITE_OK) if (sqlite3_bind_blob(stmt, curArg++, data, len, SQLITE_TRANSIENT) != SQLITE_OK)
throwSQLiteError(stmt.db, "binding argument"); SQLiteError::throw_(stmt.db, "binding argument");
} else } else
bind(); bind();
return *this; return *this;
@ -129,7 +142,7 @@ SQLiteStmt::Use & SQLiteStmt::Use::operator () (int64_t value, bool notNull)
{ {
if (notNull) { if (notNull) {
if (sqlite3_bind_int64(stmt, curArg++, value) != SQLITE_OK) if (sqlite3_bind_int64(stmt, curArg++, value) != SQLITE_OK)
throwSQLiteError(stmt.db, "binding argument"); SQLiteError::throw_(stmt.db, "binding argument");
} else } else
bind(); bind();
return *this; return *this;
@ -138,7 +151,7 @@ SQLiteStmt::Use & SQLiteStmt::Use::operator () (int64_t value, bool notNull)
SQLiteStmt::Use & SQLiteStmt::Use::bind() SQLiteStmt::Use & SQLiteStmt::Use::bind()
{ {
if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK) if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK)
throwSQLiteError(stmt.db, "binding argument"); SQLiteError::throw_(stmt.db, "binding argument");
return *this; return *this;
} }
@ -152,14 +165,14 @@ void SQLiteStmt::Use::exec()
int r = step(); int r = step();
assert(r != SQLITE_ROW); assert(r != SQLITE_ROW);
if (r != SQLITE_DONE) if (r != SQLITE_DONE)
throwSQLiteError(stmt.db, fmt("executing SQLite statement '%s'", sqlite3_expanded_sql(stmt.stmt))); SQLiteError::throw_(stmt.db, fmt("executing SQLite statement '%s'", sqlite3_expanded_sql(stmt.stmt)));
} }
bool SQLiteStmt::Use::next() bool SQLiteStmt::Use::next()
{ {
int r = step(); int r = step();
if (r != SQLITE_DONE && r != SQLITE_ROW) if (r != SQLITE_DONE && r != SQLITE_ROW)
throwSQLiteError(stmt.db, fmt("executing SQLite query '%s'", sqlite3_expanded_sql(stmt.stmt))); SQLiteError::throw_(stmt.db, fmt("executing SQLite query '%s'", sqlite3_expanded_sql(stmt.stmt)));
return r == SQLITE_ROW; return r == SQLITE_ROW;
} }
@ -185,14 +198,14 @@ SQLiteTxn::SQLiteTxn(sqlite3 * db)
{ {
this->db = db; this->db = db;
if (sqlite3_exec(db, "begin;", 0, 0, 0) != SQLITE_OK) if (sqlite3_exec(db, "begin;", 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "starting transaction"); SQLiteError::throw_(db, "starting transaction");
active = true; active = true;
} }
void SQLiteTxn::commit() void SQLiteTxn::commit()
{ {
if (sqlite3_exec(db, "commit;", 0, 0, 0) != SQLITE_OK) if (sqlite3_exec(db, "commit;", 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "committing transaction"); SQLiteError::throw_(db, "committing transaction");
active = false; active = false;
} }
@ -200,7 +213,7 @@ SQLiteTxn::~SQLiteTxn()
{ {
try { try {
if (active && sqlite3_exec(db, "rollback;", 0, 0, 0) != SQLITE_OK) if (active && sqlite3_exec(db, "rollback;", 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "aborting transaction"); SQLiteError::throw_(db, "aborting transaction");
} catch (...) { } catch (...) {
ignoreException(); ignoreException();
} }

View file

@ -96,10 +96,20 @@ struct SQLiteTxn
}; };
MakeError(SQLiteError, Error); struct SQLiteError : Error
MakeError(SQLiteBusy, SQLiteError); {
const char *path;
int errNo, extendedErrNo;
[[noreturn]] void throwSQLiteError(sqlite3 * db, const FormatOrString & fs); template<typename... Args>
[[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args);
protected:
template<typename... Args>
SQLiteError(const char *path, int errNo, int extendedErrNo, const Args & ... args);
};
MakeError(SQLiteBusy, SQLiteError);
void handleSQLiteBusy(const SQLiteBusy & e); void handleSQLiteBusy(const SQLiteBusy & e);

View file

@ -1062,7 +1062,7 @@ std::string runProgram(Path program, bool searchPath, const Strings & args,
auto res = runProgram(RunOptions {.program = program, .searchPath = searchPath, .args = args, .input = input}); auto res = runProgram(RunOptions {.program = program, .searchPath = searchPath, .args = args, .input = input});
if (!statusOk(res.first)) if (!statusOk(res.first))
throw ExecError(res.first, fmt("program '%1%' %2%", program, statusToString(res.first))); throw ExecError(res.first, "program '%1%' %2%", program, statusToString(res.first));
return res.second; return res.second;
} }
@ -1190,7 +1190,7 @@ void runProgram2(const RunOptions & options)
if (source) promise.get_future().get(); if (source) promise.get_future().get();
if (status) if (status)
throw ExecError(status, fmt("program '%1%' %2%", options.program, statusToString(status))); throw ExecError(status, "program '%1%' %2%", options.program, statusToString(status));
} }

View file

@ -119,7 +119,7 @@ std::string runNix(Path program, const Strings & args,
}); });
if (!statusOk(res.first)) if (!statusOk(res.first))
throw ExecError(res.first, fmt("program '%1%' %2%", program, statusToString(res.first))); throw ExecError(res.first, "program '%1%' %2%", program, statusToString(res.first));
return res.second; return res.second;
} }