C API

Errors guide

Use the C errors API: ownership, explanations and complete examples.

The generated Errors reference contains the complete declarations and individual API cards. This guide keeps the usage explanations and examples.

The C facade returns status_t from its public calls. Use these status names from <maelys/datalog.h> when handling a failure. A diagnostic can add source context; it does not turn a failed operation into a result.

Types

Defined in the reference

TypePurpose
status_tMachine-readable outcome of a public call.
diagnostic_tOptional source location, reason and typed details for a failed call.

Functions

FunctionPurpose
status_name()Print a public status for diagnostics.
diagnostic_initializer()Make a correctly initialized diagnostic value.
diagnostic_init()Initialize caller-owned diagnostic storage.
diagnostic_clear()Reuse a diagnostic after a call.
diag_code_name()Print a typed diagnostic reason.
limit_get()Read an installed capacity.

Status type

See the status_t reference.

Public diagnostics

Ask for a diagnostic when a loading, input or solve failure needs more detail than its status. Check the present mask before reading an optional section.

See the diagnostic_t reference.

The caller owns this record. Initialize it before passing it to a loader, an input insertion or a solve. Diagnostic ABI 1 requires the complete, naturally aligned object: use sizeof(maelys_datalog_diagnostic_t), not a hard-coded byte count. It measures 1328 bytes on the verified macOS arm64 build, but its size is a property of the installed header and target ABI.

Public status values

See the status_name() reference.

CODE
const char *name = maelys_datalog_status_name(status);

All values below belong to status_t in the installed <maelys/datalog.h> header. The table abbreviates the common MAELYS_DATALOG_STATUS_ prefix. A particular operation may return only a subset; its own contract remains the authority for that call.

SuffixValueMeaning
OK0Operation succeeded; for a query, inspect out_present separately.
INVALID_ARGUMENT-1Required argument or call shape is invalid.
INVALID_FIELD-2Input content violates a field or policy constraint.
NOT_FOUND-3A required item, policy, or query symbol is absent.
NOT_IMPLEMENTED-4The requested operation is recognized but not implemented.
UNSUPPORTED-5A capability, backend feature, or limit key is unavailable.
TIMEOUT-6A bounded operation exceeded its allowance.
IO-7File or other I/O failed.
INTERNAL-8The engine encountered an internal failure.
UNAUTHORIZED-9The caller is not authorized for an operation that requires it.
FORBIDDEN-10A guarded action was refused, for example loading an enabled test-only policy without permission.
RATE_LIMITED-11A provider or bounded service rejected an operation due to rate limiting.
PAYLOAD_TOO_LARGE-12A bound was exceeded or an explanation output-text buffer is too short; inspect the specific call's contract and required-length output.
INVALID_STATE-13The object's current lifetime or lease does not permit the operation.
STORAGE_TOO_SMALL-14Caller-supplied explanation workspace cannot hold the preparation.

status_name() returns the name for a status. Do not parse a human diagnostic message to determine the status, and do not treat any non-OK value as a negative membership result.

Example — document access

The example returns two different kinds of “no”: a failed API call and a successful query in which allow("mallory","roadmap.pdf") is absent. Neither authorizes access.

CODE
int present = 0;
maelys_datalog_status_t status = maelys_datalog_result_query(
    result, "allow", mallory_query, 2u, &present);
if (status != MAELYS_DATALOG_STATUS_OK) {
    fprintf(stderr, "query failed: %s\n", maelys_datalog_status_name(status));
    return DENY;
}
return present ? ALLOW : DENY;

Here mallory_query is the two-symbol array on Querying; ALLOW and DENY are application decisions, not engine status values. A load or solve failure is handled before this code can run.

Diagnostic functions

See the diagnostic_initializer() reference.

See the diagnostic_init() reference.

See the diagnostic_clear() reference.

When a diagnostic contains a typed reason code, turn it into a stable textual name for logs. Do not parse the human message to decide control flow.

See the diag_code_name() reference.

For example:

CODE
maelys_datalog_diagnostic_t diagnostic =
    maelys_datalog_diagnostic_initializer();
maelys_datalog_status_t rc =
    maelys_datalog_session_solve_edb(session, edb, &result, &diagnostic);
if (rc != MAELYS_DATALOG_STATUS_OK) {
    fprintf(stderr, "%s: %s\n",
            maelys_datalog_status_name(rc),
            maelys_datalog_diag_code_name(diagnostic.code));
}

{0} alone is not a valid diagnostic initializer in 0.10.0: it leaves struct_size and abi_version unset. Alternatively, call maelys_datalog_diagnostic_init(&diagnostic, sizeof diagnostic) on aligned storage and check its return status. Use diagnostic_clear(&diagnostic) only after initialization when reusing the same object.

The function return value remains the authority. source == NONE means no detail was supplied, even if a diagnostic object was passed. Otherwise status records the associated failure and code names the precise cause. message and hint are bounded human text; do not parse them as an error protocol or serialize the entire struct. Clearing leaves text bytes after the first NUL unspecified.

Read only the sections present

present is a bitmask, not a single choice. For example, a capacity diagnostic may also identify a predicate. Test each bit before reading its fields:

Presence bitRelevant fields
LOCATIONfile, line, column
PREDICATEpredicate, arity
CAPACITYobserved_count, limit, limit_kind
DEPTHdepth, depth_limit
COMPARISONcomparison_result, kind fields, comparison_op
ARITYexpected_arity, observed_arity, term_index
RULErule_id
CONTEXTtoken, field, domain
AGGREGATEfield, token, lhs_kind, term_index, limit

These bit names carry the MAELYS_DATALOG_DIAGNOSTIC_ prefix. Unknown future bits must be ignored; fields from absent sections must not be interpreted. term_index is a zero-based source position when its section supplies one, not a value to recover from the message.

Aggregate rejection in 0.10.0

The two new diag_code_t values have stable names:

Codediag_code_name()Meaning
DIAG_SOLVE_AGGREGATE_DOMAIN_ERRORsolve_aggregate_domain_errorA projected value for min, max or sum is not an integer in the accepted range 0–2147483647.
DIAG_SOLVE_SUM_OVERFLOWsolve_sum_overflowThe first partial sum exceeds 2147483647.

Both return INVALID_FIELD and publish no partial result. The AGGREGATE, PREDICATE and CONTEXT sections identify the source relation and value. In AGGREGATE, field names min, max or sum; token contains the offending value (or the first overflowing partial sum); term_index is the zero-based projection argument; and limit is the inclusive numeric bound 2147483647. This is not a storage-capacity error: the CAPACITY bit and limit_get() selectors describe a different kind of limit.

The 0.9.1 low-level diagnostic catalog is retained for historical integrations. New code uses this versioned public diagnostic protocol, not the 0.9.1 layout.

Installed capacity limits

See the limit_get() reference.

Errors and fail-closed behavior

Most operations return status_t; name and initializer helpers return their documented values. Convert a returned status to a stable name with status_name(). Loading and solving can additionally fill diagnostic_t with a source, code, location, phase, message, and hint.