Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 97 additions & 5 deletions ProcessMaker/Jobs/ErrorHandling.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,24 @@ public static function convertResponseToException($result)
$message = $rawMessage ?: 'Script execution failed with unknown error';
}

// Keep executor diagnostics before reducing the response to a UI exception.
// Do not log the response wholesale: metadata/output can contain request data or secrets.
Log::error('Script microservice execution failed', [
'message' => $message,
'diagnostics' => self::scriptErrorDiagnostics(Arr::only($result, [
'error_message', 'error', 'exception', 'message',
])),
'output_diagnostics' => self::scriptErrorDiagnostics(
is_array($result['output'] ?? null)
? Arr::only($result['output'], ['error_message', 'error', 'exception', 'code', 'file', 'line', 'trace'])
: []
),
'correlation' => self::scriptErrorDiagnostics(Arr::only(
is_array($result['metadata'] ?? null) ? $result['metadata'] : [],
['script_id', 'executor_uuid', 'executor_type', 'executor_version', 'instance_uuid', 'script_task']
)),
]);

throw new ScriptException($message);
}
}
Expand All @@ -243,14 +261,77 @@ private static function extractScriptErrorMessage(array $result): string
$result['message'] ?? null,
];

$fallback = '';
foreach ($candidates as $candidate) {
$message = self::extractMessageCandidate($candidate);
if (!empty($message)) {
return $message;
// Microservice 1.2.1 returns the exit status in error_message and the
// actionable cause in error.error (1.0.0 used message/output.error).
// Contract fixtures cover both formats; do not gate this on an executor image tag.
if (!preg_match('/^Script execution failed(?: with exit code:\s*-?\d+| with unknown error)?[.!]?$/i', $message)) {
return $message;
}
$fallback = $fallback ?: $message;
}
}

return '';
return $fallback;
}

/**
* Retain bounded, sanitized executor diagnostics and identifiers, never stack arguments.
*/
private static function scriptErrorDiagnostics(mixed $value, int $depth = 0): mixed
{
if (is_string($value)) {
return mb_strcut(self::redactScriptErrorDetails($value), 0, 8000, 'UTF-8');
}
if (!is_array($value) || $depth >= 4) {
return is_int($value) || is_float($value) || is_bool($value) ? $value : null;
}

$allowed = ['error_message', 'message', 'detail', 'error', 'exception', 'code', 'file', 'line',
'trace', 'class', 'function', 'type', 'script_id', 'executor_uuid', 'executor_type',
'executor_version', 'instance_uuid', 'script_task', 'definition_id', 'instance_id', 'token_id'];
$diagnostics = [];
foreach (array_slice($value, 0, 30, true) as $key => $item) {
if ($key === 'trace') {
// Text traces may contain unlabeled arguments. Keep only structured frames.
$frames = self::scriptTraceDiagnostics($item, $depth + 1);
if ($frames !== []) {
$diagnostics[$key] = $frames;
}
continue;
}
if (is_int($key) || in_array($key, $allowed, true)) {
$diagnostics[$key] = self::scriptErrorDiagnostics($item, $depth + 1);
}
}

return $diagnostics;
}

private static function scriptTraceDiagnostics(mixed $trace, int $depth): array
{
if (!is_array($trace) || $depth >= 4) {
return [];
}

$frames = [];
foreach (array_slice(array_is_list($trace) ? $trace : [$trace], 0, 30) as $frame) {
if (!is_array($frame)) {
continue;
}
$fields = array_filter(
Arr::only($frame, ['file', 'line', 'class', 'function', 'type']),
fn ($field) => is_string($field) || is_int($field)
);
if ($fields !== []) {
$frames[] = self::scriptErrorDiagnostics($fields, $depth + 1);
}
}

return $frames;
}

/**
Expand Down Expand Up @@ -313,14 +394,25 @@ public static function sanitizeScriptErrorMessage(string $message): string
*/
public static function redactScriptErrorDetails(string $details): string
{
$redacted = preg_replace(
[
'/("(?:authorization|api[_-]?(?:token|key)|password)"\s*:\s*)"(?:\\\\.|[^"\\\\\r\n]++)*+"/i',
'/("(?:(?:access|refresh|callback)[_-]?token|client[_-]?secret)"\s*:\s*)"(?:\\\\.|[^"\\\\\r\n]++)*+"/i',
],
'$1"[REDACTED]"',
$details
) ?? '[REDACTED]';
$redacted = preg_replace(
'/\bauthorization\b\s*[:=]\s*[^\r\n]*/i',
'Authorization=[REDACTED]',
$details
) ?? $details;
$redacted
) ?? $redacted;
$redacted = preg_replace('/\bBearer\s+\S+/i', 'Bearer [REDACTED]', $redacted) ?? $redacted;
$redacted = preg_replace(
'/\b(api[_-]?token|access[_-]?token|client[_-]?secret|password)\b\s*[:=]\s*[^\s,;]+/i',
[
'/\b(api[_-]?(?:token|key)|password)\b\s*[:=]\s*[^\s,;]+/i',
'/\b((?:access|refresh|callback)[_-]?token|client[_-]?secret)\b\s*[:=]\s*[^\s,;]+/i',
],
'$1=[REDACTED]',
$redacted
) ?? $redacted;
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixtures/script-microservice/1.0.0/php-validation-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"status": "error",
"metadata": {},
"message": "Script execution failed with exit code: 1",
"output": {
"error": "variableName is required",
"code": 0,
"file": "/opt/executor/script.php",
"line": 88,
"trace": "#0 /opt/executor/bootstrap.php: validateConfig()"
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/script-microservice/1.2.1/php-validation-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"status": "error",
"metadata": {
"script_id": 42,
"executor_uuid": "00000000-0000-4000-8000-000000000001",
"executor_version": "v2026.12.2",
"instance_uuid": "00000000-0000-4000-8000-000000000002"
},
"output": {},
"error_message": "Script execution failed with exit code: 1",
"error": {
"error": "variableName is required",
"code": 0,
"file": "/opt/executor/script.php",
"line": 88,
"trace": "#0 /opt/executor/bootstrap.php: validateConfig()"
}
}
24 changes: 24 additions & 0 deletions tests/Fixtures/script-microservice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Script microservice error contracts

These sanitized fixtures preserve the response structures observed during FOUR-34463
validation. Identifiers and executor diagnostics are illustrative; no credentials or
request documents are included.

- API 1.0.0: the generic execution message is in `message`, with diagnostics in `output`.
- API 1.2.1 (`script-executor-service:1.2.1-develop`): the generic execution message is
in `error_message`, with diagnostics in `error`. The actionable cause is `error.error`.

`ScriptMicroserviceErrorContractTest` verifies that both formats expose the actionable
cause while preserving sanitized diagnostics in application logs. An actionable top-level
message retains precedence. An exit-status message is a fallback when no cause is available.
Exit codes 1 and 255 follow the same policy. API versions and executor image versions are
separate; response interpretation must not depend on an executor image tag.

Text traces are deliberately omitted from diagnostic logs because positional arguments
can contain document content or unlabeled secrets. Only structured frames with scalar
`file`, `line`, `class`, `function`, and `type` fields are retained; arguments, objects,
text entries, and unknown trace formats are excluded. The fixtures retain text traces
to verify this omission for both response contracts.

Future microservice upgrades should validate these contracts and add fixtures for changed
responses. A valid error JSON response alone does not ensure compatible message selection.
Loading
Loading