From 20e1c69f58ba8955e2c7a99303145c68a9ea8500 Mon Sep 17 00:00:00 2001 From: Peter Ludemann Date: Tue, 6 Jan 2026 18:07:16 -0800 Subject: [PATCH] Add exception smoke tests, generalize PlUnknownError --- SWI-cpp2.cpp | 10 +++++++--- SWI-cpp2.h | 2 ++ test_cpp.cpp | 8 ++++++++ test_cpp.pl | 5 +++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/SWI-cpp2.cpp b/SWI-cpp2.cpp index 5293977..8cd5997 100644 --- a/SWI-cpp2.cpp +++ b/SWI-cpp2.cpp @@ -267,11 +267,15 @@ PlResourceError(const std::string& resource) _SWI_CPP2_CPP_inline PlException PlUnknownError(const std::string& description) -{ // For PlWrap() - return PlGeneralError(PlCompound("unknown_error", - PlTermv(PlTerm_atom(description)))); +{ return PlUnknownError(PlTerm_atom(description)); } +_SWI_CPP2_CPP_inline +PlException +PlUnknownError(PlTerm description) +{ return PlGeneralError(PlCompound("unknown_error", + PlTermv(description))); +} diff --git a/SWI-cpp2.h b/SWI-cpp2.h index 47c4db1..2ed7b5e 100644 --- a/SWI-cpp2.h +++ b/SWI-cpp2.h @@ -1306,6 +1306,8 @@ PlException PlResourceError(const std::string& resource); PlException PlUnknownError(const std::string& description); +PlException PlUnknownError(PlTerm description); + void PlWrap_fail(qid_t qid); template C_t diff --git a/test_cpp.cpp b/test_cpp.cpp index e7f1b14..0a4dea5 100644 --- a/test_cpp.cpp +++ b/test_cpp.cpp @@ -114,6 +114,14 @@ PREDICATE(hello, 2) return A2.unify_string(buffer.str()); } +PREDICATE(helloex, 0) +{ throw PlUnknownError("an exception"); +} + +PREDICATE(helloex, 1) +{ throw PlUnknownError(A1); +} + PREDICATE(hello2, 2) { PlAtom atom_a1(A1.as_atom()); std::stringstream buffer; diff --git a/test_cpp.pl b/test_cpp.pl index 8e0a5a5..a94d6ab 100644 --- a/test_cpp.pl +++ b/test_cpp.pl @@ -92,6 +92,11 @@ % hello :- write('hello hello hello') with_output_to(string(Out), hello). +test(helloex, error(unknown_error('an exception'))) :- + helloex. +test(helloex, error(unknown_error(foo(bar)))) :- + helloex(foo(bar)). + test(hello, Out == "Hello WORLD\nHello WORLD\nHello WORLD\nHello WORLD\nHello WORLD\n") :- hello("WORLD", Out). test(hello, error(representation_error(encoding))) :-