From 0dc9b6b7c748e125e24938964bb8de515b970ab0 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 2 Apr 2025 17:00:59 +0200 Subject: [PATCH] libutil-tests-support: Add file/line to ctx errors --- .../include/nix/util/tests/nix_api_util.hh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/libutil-test-support/include/nix/util/tests/nix_api_util.hh b/src/libutil-test-support/include/nix/util/tests/nix_api_util.hh index 006dc497c..382c7b292 100644 --- a/src/libutil-test-support/include/nix/util/tests/nix_api_util.hh +++ b/src/libutil-test-support/include/nix/util/tests/nix_api_util.hh @@ -3,6 +3,7 @@ #include "nix_api_util.h" #include +#include namespace nixC { @@ -24,7 +25,12 @@ protected: nix_c_context * ctx; - inline void assert_ctx_ok() + inline std::string loc(const char * file, int line) + { + return std::string(file) + ":" + std::to_string(line); + } + + inline void assert_ctx_ok(const char * file, int line) { if (nix_err_code(ctx) == NIX_OK) { return; @@ -32,16 +38,18 @@ protected: unsigned int n; const char * p = nix_err_msg(nullptr, ctx, &n); std::string msg(p, n); - throw std::runtime_error(std::string("nix_err_code(ctx) != NIX_OK, message: ") + msg); + throw std::runtime_error(loc(file, line) + ": nix_err_code(ctx) != NIX_OK, message: " + msg); } +#define assert_ctx_ok() assert_ctx_ok(__FILE__, __LINE__) - inline void assert_ctx_err() + inline void assert_ctx_err(const char * file, int line) { if (nix_err_code(ctx) != NIX_OK) { return; } - throw std::runtime_error("Got NIX_OK, but expected an error!"); + throw std::runtime_error(loc(file, line) + ": Got NIX_OK, but expected an error!"); } +#define assert_ctx_err() assert_ctx_err(__FILE__, __LINE__) }; }