]> Repositorios git - scryer-prolog.git/commitdiff
ADDED: call_with_error_context/2
authorMarkus Triska <[email protected]>
Sat, 8 Mar 2025 08:12:17 +0000 (09:12 +0100)
committerMarkus Triska <[email protected]>
Sat, 8 Mar 2025 08:12:17 +0000 (09:12 +0100)
See https://github.com/mthom/scryer-prolog/discussions/2839 for more information.

src/lib/error.pl

index fc3b9712d85330fbb1683e43b11a4f337c7094ae..81c676df00f7ccb268b67ab16557f1e52650f0e2 100644 (file)
@@ -1,5 +1,5 @@
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-   Written 2018-2023 by Markus Triska ([email protected])
+   Written 2018-2025 by Markus Triska ([email protected])
    I place this code in the public domain. Use it in any way you want.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
@@ -7,7 +7,8 @@
                   can_be/2,
                   instantiation_error/1,
                   domain_error/3,
-                  type_error/3
+                  type_error/3,
+                  call_with_error_context/2
                   ]).
 
 
@@ -217,3 +218,20 @@ domain_error(Type, Term, Context) :-
 
 type_error(Type, Term, Context) :-
     throw(error(type_error(Type, Term), Context)).
+
+/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+   call_with_error_context/2
+
+   See https://github.com/mthom/scryer-prolog/discussions/2839 .
+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+%% call_with_error_context(+Goal, +Pair)
+%
+% Call _Goal_ with error context _Pair_.
+%
+% Examples of error contexts: `predicate-PI`, `file-Filename` etc.
+
+:- meta_predicate(call_with_error_context(0,+)).
+call_with_error_context(G_0, Pair) :-
+   must_be(pair, Pair),
+   catch(G_0, error(E,Pairs), throw(error(E,[Pair|Pairs]))).