# Trapping fatal exceptions in a PEngines instance and producing a stack trace?

**URL:** <https://swi-prolog.discourse.group/t/trapping-fatal-exceptions-in-a-pengines-instance-and-producing-a-stack-trace/804>\
**Category:** Help!\
**Created:** [June 13, 2019, 1:11am UTC](https://swi-prolog.discourse.group/t/trapping-fatal-exceptions-in-a-pengines-instance-and-producing-a-stack-trace/804 "2019-06-13T01:11:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![prodog](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prodog](https://swi-prolog.discourse.group/u/prodog)\
**Post date:** [June 13, 2019, 1:11am UTC](https://swi-prolog.discourse.group/t/trapping-fatal-exceptions-in-a-pengines-instance-and-producing-a-stack-trace/804/1 "2019-06-13T01:11:40Z")

</div>

I’m using: SWI-Prolog version 8.0.2

Occasionally I get fatal errors in my code running under PEngines, usually with the predicate `atomics-to_string` due to an **Arguments are not sufficiently instantiated** error.

PEngines catches the error without crashing, that’s good. But I am wondering if there is a way to do the equivalent of a _try/catch_ around functions like `atomics_to_string` that can cause a fatal error like that, so I can handle the error myself, or at least add some context to help with debugging?

Also, is there a way to get PEngines to deliver a **full stack trace** instead of just an error message?:

```prolog
Object received: {
    "code": "instantiation_error",
    "data": "atomics_to_string/2: Arguments are not sufficiently instantiated",
    "event": "error",
    "id": "21b36912-e160-4898-9a40-d83b3d10b410",
    "pengine": {
        "options": {
            "server": "http://localhost:3030/pengine",
            "application": "tsll",
            "destroy": false,
            "src_text": "(removed)",
            "format": "json"
        },
        "id": "21b36912-e160-4898-9a40-d83b3d10b410",
        "request": {}
    }
}

```

In the meantime I am using code like that below to help my debugging efforts, but I am hoping there are things intrinsic to SWi-Prolog to replace or augment what the code below does:

```prolog
% ----->>>>> is_any_list_element_var

% This predicate succeeds if any element in the list is not ground.

% Did not find any var() list elements. Fail the function.
is_any_list_element_var_1([]) :- !, fail.

is_any_list_element_var_1([H | _]) :-
    var(H),
    !.

is_any_list_element_var_1([_ | T]) :-
    !,
    is_any_list_element_var_1(T).

is_any_list_element_var(List) :-
    is_list(List),
    !,
    is_any_list_element_var_1(List).

is_any_list_element_var(_) :-
    x_write('(is_any_list_element_var) ERROR: The list parameter is not a list.'),
    !.

% ----->>>>> wrap_atomics_to_string

% Wrap the atomics_to_string predicate in an attempt to achieve better debugging
% capabilities when calling that predicate.

% Validate the Caller parameter.
wrap_atomics_to_string(Caller, _, _) :-
    var(Caller),
    x_write('(wrap_atomics_to_string) ERROR: The caller parameter should be ground.'),
    !.

% Validate the List parameter as being a list.
wrap_atomics_to_string(Caller, List, _) :-
    is_list(List),
    x_write('(wrap_atomics_to_string) ERROR: The list parameter should be ground. Called by: '),
    x_write(Caller),
    !.

% Check for an empty list.
wrap_atomics_to_string(Caller, List, _) :-
    is_list(List),
    length(List, 0),
    x_write('(wrap_atomics_to_string) ERROR: The list parameter is empty. Called by: '),
    x_write(Caller),
    !.

% Make sure every element in the list to be passed to atomics_to_string is ground.
wrap_atomics_to_string(Caller, List, _) :-
    is_any_list_element_var(List),
    x_write('(wrap_atomics_to_string) ERROR: The list parameter contains one or more elements that are not ground. Called by: '),
    x_write(Caller),
    !.

% Should be OK to call atomics_to_string now.
wrap_atomics_to_string(Caller, List, StrOut) :-
    x_write('(wrap_atomics_to_string) Calling atomics_to_string from: '),
    x_write(Caller),
    x_write(', with list content: '),
    x_write(List),
    atomics_to_string(List, StrOut),
    x_write('(wrap_atomics_to_string) Successful call.'),
    !.

```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 13, 2019, 8:58am UTC](https://swi-prolog.discourse.group/t/trapping-fatal-exceptions-in-a-pengines-instance-and-producing-a-stack-trace/804/2 "2019-06-13T08:58:15Z")

</div>

See catch/3 and catch\_with\_backtrace/3. The latter is quite recent and should probably be used in the pengines library.
