Debugging & Metadata
Metadata Flow
- Metadata is an array that travels with the
Result. then()andotherwise()propagate updated metadata when a step returns aResultwith new meta.mergeMeta()shallow merges;mapMeta()replaces;tapMeta()observes without mutation.failWithValue($error, $failedValue, $meta = [])stores the failed input atmeta['failed_value']for later logging.
Debug-Safe Output
toDebugArray(?callable $sanitizer = null) produces a sanitized structure:
php
[
'ok' => bool,
'value_type' => string|null,
'error_type' => string|null,
'error_message' => string|null, // Throwable message or string error
'meta' => [...], // sanitized recursively
]Behavior:
- Sensitive keys (password, token, api_key, ssn, card, etc.) are redacted to
***REDACTED***. - Long strings are truncated (
max_string_length, default 200) whentruncate_stringsis true. - When Laravel's
config()helper is available, settings are read fromconfig('result-flow.debug'). Otherwise defaults are used.
Pass a custom sanitizer to override the defaults:
php
$result->toDebugArray(function ($value) {
// redact anything under "secret" keys and leave everything else unchanged
return is_array($value) && array_key_exists('secret', $value)
? ['secret' => '***']
: $value;
});Observability Hooks
Use side-effect helpers to log without mutating the pipeline:
php
Result::ok($payload, ['request_id' => $rid])
->tap(fn($value, $error, $meta) => Log::info('result', compact('value', 'error', 'meta')))
->onSuccess(fn($value, $meta) => Metrics::increment('result.ok'))
->onFailure(fn($error, $meta) => Log::warning('result.fail', ['error' => $error] + $meta));