message
tuprolog.solve.exception.error.message
¶
Message errors: the errors carrying an arbitrary thrown term, fitting no ISO error family.
When Prolog code throws a term that is not an ISO error(Type, Extra) one (e.g.
throw(my_problem)), or a primitive needs to throw a custom term, 2P-Kt represents it as a
MessageError, whose content is the thrown term. Catching it in Prolog unifies the catcher
with that very term.
Build them with message_error.
Examples:
>>> from tuprolog.core import atom
>>> from tuprolog.solve.library import library
>>> from tuprolog.solve.primitive import primitive
>>> from tuprolog.solve.prolog import prolog_solver
>>> def complain(request):
... raise message_error(atom("my_problem"), request.context)
>>> lib = library("demo", primitives={"complain/0": primitive(complain)})
>>> solver = prolog_solver(libraries=lib)
>>> solver.solve_once("catch(complain, E, true)")["E"]
my_problem
See Also
2P-Kt's documentation on errors and exceptions.
MessageError
module-attribute
¶
MessageError = _errors.MessageError
The error carrying an arbitrary thrown term, used when no other LogicError fits.
The JVM class (also a tuprolog.errors.PrologMessageError). Members (snake_case aliases as
well): content, the thrown term; besides those of tuprolog.solve.exception.LogicError.
message_error
¶
message_error(
content: Term,
context: ExecutionContext,
cause: Any = None,
) -> MessageError
Creates the error throwing an arbitrary term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Term
|
the term to throw; Prolog's |
required |
context
|
ExecutionContext
|
the execution context in which the error occurs (e.g. a request's |
required |
cause
|
Any
|
the exception causing this one, if any. |
None
|
Returns:
| Type | Description |
|---|---|
MessageError
|
The error. |
Note
Calls 2P-Kt's MessageError.Companion.of, which (not being static on the JVM) is not
reachable as MessageError.of.
Examples:
>>> from tuprolog.solve import signature
>>> from tuprolog.solve.exception.error import *
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> from tuprolog.core import atom
>>> message_error(atom("hello"), context).content
hello