refactor: preserve exception chain in validation error handling

When catching and re-throwing exceptions, preserve the original exception
chain by passing the caught exception as the third parameter to new Exception.
This retains the full stack trace for debugging while keeping descriptive
error messages.

Changes:
- validateDockerComposeForInjection(): 4 locations fixed
- validateVolumeStringForInjection(): 3 locations fixed

Before:
  throw new \Exception('Invalid Docker volume definition: '.$e->getMessage());

After:
  throw new \Exception('Invalid Docker volume definition: '.$e->getMessage(), 0, $e);

Benefits:
- Full stack trace preserved for debugging
- Original exception context retained
- Better error diagnostics in production logs

All 60 security tests pass (176 assertions).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Andras Bacsai 2025-10-15 22:09:48 +02:00
parent 334559bb0b
commit 3700f78355

View File

@ -29,7 +29,7 @@ function validateDockerComposeForInjection(string $composeYaml): void
try {
$parsed = Yaml::parse($composeYaml);
} catch (\Exception $e) {
throw new \Exception('Invalid YAML format: '.$e->getMessage());
throw new \Exception('Invalid YAML format: '.$e->getMessage(), 0, $e);
}
if (! is_array($parsed) || ! isset($parsed['services']) || ! is_array($parsed['services'])) {
@ -42,7 +42,9 @@ function validateDockerComposeForInjection(string $composeYaml): void
} catch (\Exception $e) {
throw new \Exception(
'Invalid Docker Compose service name: '.$e->getMessage().
' Service names must not contain shell metacharacters.'
' Service names must not contain shell metacharacters.',
0,
$e
);
}
@ -64,7 +66,9 @@ function validateDockerComposeForInjection(string $composeYaml): void
} catch (\Exception $e) {
throw new \Exception(
'Invalid Docker volume definition (array syntax): '.$e->getMessage().
' Please use safe path names without shell metacharacters.'
' Please use safe path names without shell metacharacters.',
0,
$e
);
}
}
@ -78,7 +82,9 @@ function validateDockerComposeForInjection(string $composeYaml): void
} catch (\Exception $e) {
throw new \Exception(
'Invalid Docker volume definition (array syntax): '.$e->getMessage().
' Please use safe path names without shell metacharacters.'
' Please use safe path names without shell metacharacters.',
0,
$e
);
}
}
@ -107,7 +113,9 @@ function validateVolumeStringForInjection(string $volumeString): void
} catch (\Exception $e) {
throw new \Exception(
'Invalid Docker volume definition: '.$e->getMessage().
' Please use safe names without shell metacharacters.'
' Please use safe names without shell metacharacters.',
0,
$e
);
}
@ -125,7 +133,9 @@ function validateVolumeStringForInjection(string $volumeString): void
} catch (\Exception $e) {
throw new \Exception(
'Invalid Docker volume definition: '.$e->getMessage().
' Please use safe path names without shell metacharacters.'
' Please use safe path names without shell metacharacters.',
0,
$e
);
}
}
@ -136,7 +146,9 @@ function validateVolumeStringForInjection(string $volumeString): void
} catch (\Exception $e) {
throw new \Exception(
'Invalid Docker volume definition: '.$e->getMessage().
' Please use safe path names without shell metacharacters.'
' Please use safe path names without shell metacharacters.',
0,
$e
);
}
}