Skip to content

Client errors, transport and diagnostics

A failed client call leaves your application with two questions: what can it tell the person using it, and what did the API actually keep? A documented rejection can explain why a choice was refused. A connection failure or an unreadable response can leave the result of a write unknown. Handling both as “try again” can repeat an operation that already succeeded.

This guide uses the remote clients introduced in Remote PHP SDK and JavaScript and TypeScript SDK. It focuses on the application’s response to a failure, including how to recover a known session before deciding what to send next.

Situation What the client knows What your application does
The API rejects a choice because its condition is not satisfied It received a usable error response with a code and HTTP status. Explain that the choice is unavailable and obtain the current session to present the available options.
The service cannot be contacted The transport did not provide a usable API response. Present a connection problem. For an attempted write, keep its outcome unresolved until you can check the session.
A choice was sent but its reply was lost or invalid The local client has no confirmed new state; the API may already have saved it. Query the known session ID and refresh the view before deciding whether another operation is needed.

An HTTP error status alone does not establish that every write was rolled back. Interpret the documented error and the operation that produced it. The distinction between confirmed rejection and uncertain persistence is developed in Rejected transitions and error recovery.

Read the SDK error, then choose the application message

Section titled “Read the SDK error, then choose the application message”

The PHP SDK throws exceptions derived from ExecKernel\Exceptions\ExecKernelException. Its code() is the ExecKernel code; details() contains available metadata such as http and trace_id. Transport failures use ExecKernelConnectionException or ExecKernelTimeoutException. A remote response can instead produce a mapped state exception or ExecKernelRemoteErrorException.

A code is useful together with the exception class and response metadata. For example, EK-SYS-002 can occur in a transport failure or when persistence was not confirmed. Testing only that string cannot distinguish all outcomes.

In the following fragment, $client is a configured remote PHP client, $state is its latest SessionStateDTO, and $edgeId identifies the choice submitted by your application. The application keeps an outcome it can turn into a message or the next view:

use ExecKernel\Exceptions\ExecKernelConnectionException;
use ExecKernel\Exceptions\ExecKernelException;
use ExecKernel\Exceptions\ExecKernelTimeoutException;
try {
$next = $client->navigate($state, $edgeId);
$outcome = ['kind' => 'updated', 'state' => $next];
} catch (ExecKernelConnectionException | ExecKernelTimeoutException $error) {
$outcome = ['kind' => 'check-session', 'session_id' => $state->sessionId];
} catch (ExecKernelException $error) {
$outcome = [
'kind' => $error->code() === 'EK-RULE-403' ? 'rejected' : 'error',
'code' => $error->code(),
'http' => $error->details()['http'] ?? null,
'trace_id' => $error->details()['trace_id'] ?? null,
];
}

For rejected, your application can explain that the choice is unavailable. For check-session, it can explain that it could not confirm the result and begin recovery. Keep the application message separate from raw exception text and business data in the diagnostic context.

The JavaScript SDK uses ExecKernelError, with ekCode, type, message and context. A rejected fetch becomes EK-EXTERNAL-000 with type NetworkError. Invalid JSON after a 2xx response becomes EK-SYS-003 with type InvalidResponse; after an error status, it becomes EK-EXTERNAL-000. Available HTTP status and trace metadata are kept in context.

Here is the same application-level choice with a JavaScript Session:

const { ExecKernelError } = require('@execkernel/sdk');
async function submitChoice(session, edgeId) {
try {
await session.selectEdge(edgeId);
return { kind: 'updated', payload: session.currentPayload };
} catch (error) {
if (!(error instanceof ExecKernelError)) throw error;
return {
kind: error.ekCode === 'EK-RULE-403' ? 'rejected' : 'check-session',
code: error.ekCode,
http: error.context.http ?? null,
traceId: error.context.trace_id ?? null,
};
}
}

These examples use the code returned by the standalone API for the blocked choice. Error mapping at the HTTP boundary can differ from the code raised by the directly embedded core. Consult the code for the integration you are using; the error reference provides the broader lookup.

Recover a known session after a lost reply

Section titled “Recover a known session after a lost reply”

We reuse the learning-path graph from Anatomy of a graph and its API catalogue adaptation from HTTP API integration. At node 10, edge 101 selects a guide and reaches terminal 20; edge 102 selects practice and reaches terminal 30 when practiceEnabled is true.

Start with practice disabled. Choosing 102 produces HTTP 403 and EK-RULE-403. Resuming that session returns node 10. The application can show the current options and explain the rejection.

Now send the same choice with practiceEnabled: true, but lose the reply after the API processes it. The local PHP DTO still describes node 10, while the API has saved node 30:

A lost choice reply leaves the local view at node 10 while the API has stored node 30. The application queries the known session and refreshes its view to node 30.

Use the known ID to obtain a summary and then a fresh state with its payload. Here $lang is the presentation language requested by your application:

$status = $client->status($state->sessionId);
$recovered = $client->resume($state->sessionId, $lang);
$currentNode = $status->currentNode;
$nextViewPayload = $recovered->payload;

status() supplies a summary; resume() supplies the state and presentation payload used to rebuild the view. In JavaScript, the corresponding fresh read is client.restoreSession(sessionId). Passing an exported snapshot instead hydrates a local copy, as explained in Builders, payloads and session snapshots.

The complete diagnostics-recovery.php example performs both cases against an API. Its LoseOneChoiceReply teaching transport delegates to the real HTTP transport, then deliberately discards one successful choice response. The lost reply is simulated; the API’s saved transition and the subsequent reads are real. The program sends that accepted choice once and obtains:

{
"rejected_choice": { "code": "EK-RULE-403", "http": 403 },
"node_after_rejection": 10,
"uncertain_code": "EK-SYS-002",
"choice_requests_after_rejection": 1,
"status_node": 30,
"recovered_node": 30,
"recovered_terminal": true,
"recovered_context": { "practiceEnabled": true }
}

This excerpt omits the diagnostic event with its generated trace ID. To use the complete program in an existing PHP project, place it alongside vendor/, configure EXECKERNEL_BASE_URL and, when required, EXECKERNEL_API_TOKEN, and make the learning-path graph available in the API catalogue. Run php diagnostics-recovery.php en; the program removes its temporary session afterwards.

Recovery requires an ID your application already knows. If the reply to start is lost before your application receives the new ID, these session routes do not provide a client-key lookup for that creation. Repeating start can create another session. Decide how your application reconciles that creation instead of treating another start as confirmation of the first.

Configure timeouts and retries for the actual transport

Section titled “Configure timeouts and retries for the actual transport”
Client or transport Timeout behavior Retry behavior
PHP native HttpTransport Uses Config::timeoutSeconds(); the default is 10 seconds. An open failure can be reported as a connection exception, while a timed-out response read is a timeout exception. The PHP client applies its configured policy only to inspect() and status().
PHP GuzzleTransport with an injected PSR-18 client The injected HTTP client controls its timeout. This transport does not apply the SDK config’s timeout to that client. The same PHP client policy applies, subject to the transport exception being eligible.
JavaScript built-in HttpTransport Makes one call to fetchImpl; it does not add a deadline. Your application can supply a fetch implementation with one. No automatic retry loop is provided.

PHP retries are disabled by default. This configuration permits two additional attempts, separated by 25 milliseconds, for an eligible read operation:

use ExecKernel\Config\Config;
use ExecKernel\Utils\RetryPolicy;
$config = new Config(
baseUrl: $baseUrl,
authToken: $token,
retryPolicy: new RetryPolicy(retries: 2, backoffMs: 25),
timeoutSeconds: 3,
);

The policy accepts connection or timeout exceptions unless their transport metadata explicitly marks them as non-retryable. A received HTTP 503 error is not itself retried by this loop. start(), navigate(), resume() and destroy() are not eligible. In particular, inspect() uses HTTP POST but is marked read-only by the SDK, while resume() is outside this retry policy.

For JavaScript, inject a deadline where your server-side client is configured. This example uses a runtime that supports AbortSignal.timeout:

const { ExecKernelClient } = require('@execkernel/sdk');
const client = new ExecKernelClient({
baseUrl,
apiKey,
flowId: 'learning-path',
fetchImpl: (url, init) =>
fetch(url, { ...init, signal: AbortSignal.timeout(3000) }),
});

An aborted fetch is surfaced as NetworkError; the SDK does not introduce a separate timeout exception for this wrapper. The deadline bounds how long the client waits. It does not undo a write already accepted by the API.

Use a correlation ID to associate calls with work in your application, and retain the response’s trace ID when one is available. The PHP SdkEventDispatcher and JavaScript client expose http:request, http:response and session:error diagnostic events. Their projections contain bounded operational fields such as the method, endpoint pattern, status, code and trace ID, rather than the full request.

The PHP example attaches a listener like this:

$events->on(
SdkEventDispatcher::EVENT_SESSION_ERROR,
static function (array $event) use (&$diagnostics): void {
$diagnostics[] = $event['error'];
},
);

Pass $events as the client’s dispatcher to receive those events. Listeners are best-effort observers: a failing listener does not change a confirmed SDK result. Keep business work that must succeed in your application’s explicit operation flow.

This projection applies to the diagnostic events named above. Session payload events contain application data and should be handled according to the purpose of that data. Debugging and troubleshooting covers the wider investigation once you have the operation, error code and trace reference.