Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions SWI-cpp2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}



Expand Down
2 changes: 2 additions & 0 deletions SWI-cpp2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename C_t> C_t
Expand Down
8 changes: 8 additions & 0 deletions test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions test_cpp.pl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
% hello :- write('hello hello hello')
with_output_to(string(Out), hello).

test(helloex, error(unknown_error('an exception'))) :-
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors should always be of the shape error(Formal, Context). That is used everywhere in the system to distinguish error exceptions from other exceptions (like timeout, abort, etc.) Typically you leave the 2nd argument unbound in SWI-Prolog. The exception handler adds the context, either as a single predicate or as a stack trace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few other places where errors are of the wrong shape -- I'll review all the SWI-cpp2.* code and add another commit. This might break backwards compatibility, but I hope not.

If I can, I'll also make PlException's constructor inaccessible, to avoid making this mistake again.

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))) :-
Expand Down