Docs

Logging & Error Handling

Context Reference - Logging & Error Handling Add logs to API responses and throw HTTP errors with proper status codes. Logging Call @LOGS(...) or $ctx.$logs(...) to record script checkpoints. Output is retained in enfyra_user_log and can be inspected under Settings Server Logs Us

Context Reference - Logging & Error Handling

Add logs to API responses and throw HTTP errors with proper status codes.

Logging

Call @LOGS(...) or $ctx.$logs(...) to record script checkpoints. Output is retained in enfyra_user_log and can be inspected under Settings Server Logs User logs. Use the request correlation ID to find related system errors. See Trace Errors and Script Logs for the interface workflow, permissions, retention, and limits.

Basic Logging

$ctx.$logs('Operation started');
$ctx.$logs('User created successfully');
$ctx.$logs(`Processing order: ${orderId}`);

Logging Variables

const userId = $ctx.$user.id;
const orderId = 123;

$ctx.$logs(`User ID: ${userId}`);
$ctx.$logs(`Order ID: ${orderId}`);
$ctx.$logs(`Processing data:`, dataObject);

Logging in Different Phases

// In preHook
$ctx.$logs('Validation started');

// In handler
$ctx.$logs('Creating user...');

// In postHook
$ctx.$logs('User created successfully');

Note: Logs appear automatically in API responses. You don't need to manually include them in the return value.

Error Handling

Throw HTTP errors with proper status codes.

Throw HTTP Status Errors

// Bad Request
$ctx.$throw['400']('Invalid input data');

// Unauthorized
$ctx.$throw['401']('Authentication required');

// Forbidden
$ctx.$throw['403']('Insufficient permissions');

// Not Found
$ctx.$throw['404']('Resource not found');

// Conflict
$ctx.$throw['409']('Email already exists');

// Unprocessable Entity
$ctx.$throw['422']('Validation failed');

// Too Many Requests (Rate Limiting)
$ctx.$throw['429']('Rate limit exceeded. Try again later');

// Internal Server Error
$ctx.$throw['500']('Internal server error');

// Service Unavailable
$ctx.$throw['503']('Service temporarily unavailable');

Error Handling in postHook

In postHook, you can check for errors that occurred during the request:

// Check if error occurred
if ($ctx.$api.error) {
  // Error case
  $ctx.$logs(`Error occurred: ${$ctx.$api.error.message}`);
  $ctx.$logs(`Error status: ${$ctx.$api.error.statusCode}`);

  // $ctx.$data will be null when error occurs
  // $ctx.$statusCode will be the error status code
} else {
  // Success case
  $ctx.$logs('Operation completed successfully');
  // $ctx.$data contains the response data
  // $ctx.$statusCode contains success status (200, 201, etc.)
}

Note: $ctx.$api.error is only available in postHook, not in preHook.

Next Steps