zf1/zend-exception
Zend Framework 1 Exception component repackaged for Composer. Lets you install only the exception-related pieces of ZF1 with optimized autoloading and smaller footprint, easing incremental migration without pulling the full framework.
Install the Package
Add to your composer.json:
"require": {
"zf1/zend-exception": "^1.12"
}
Run:
composer require zf1/zend-exception
Autoloading
Ensure your composer.json autoload section includes:
"autoload": {
"psr-0": {
"Zend_": "vendor/zf1/zend-exception/library/"
}
}
Run:
composer dump-autoload
First Usage
Use Zend_Exception in your Laravel application:
use Zend_Exception;
try {
throw new Zend_Exception('This is a legacy exception');
} catch (Zend_Exception $e) {
// Handle the exception (e.g., log or render)
Log::error($e->getMessage());
return response()->json(['error' => $e->getMessage()], 500);
}
Basic Configuration
If you need to customize exception behavior (e.g., stack trace formatting), extend Zend_Exception:
class CustomZendException extends Zend_Exception {
public function __construct($message, $code = 0, Exception $previous = null) {
parent::__construct($message, $code, $previous);
// Custom logic here
}
}
Legacy Code Integration
Use Zend_Exception in legacy controllers or services that cannot be refactored immediately:
// In a legacy controller
public function legacyAction() {
if ($error) {
throw new Zend_Exception('Legacy error occurred', 500);
}
}
Exception Wrapping
Convert Zend_Exception to Laravel-compatible exceptions for consistency:
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpKernel\Exception\HttpException;
try {
// Legacy code throwing Zend_Exception
} catch (Zend_Exception $e) {
Log::error($e->getMessage(), ['exception' => $e]);
throw new HttpException(500, $e->getMessage());
}
Custom Exception Hierarchy
Extend Zend_Exception to create domain-specific exceptions:
class PaymentException extends Zend_Exception {}
class InvalidPaymentMethodException extends PaymentException {}
throw new InvalidPaymentMethodException('Card declined');
Global Exception Handling
Register a custom exception handler in Laravel’s App\Exceptions\Handler:
public function render($request, Throwable $exception) {
if ($exception instanceof Zend_Exception) {
return response()->json([
'error' => 'Legacy Error',
'message' => $exception->getMessage(),
'code' => $exception->getCode()
], $exception->getCode());
}
return parent::render($request, $exception);
}
Incremental Migration
Zend_Exception.Zend_Exception with Laravel’s Exception or custom exceptions.Logging and Monitoring
Zend_Exception instances with additional context:
Log::error('Legacy exception caught', [
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString()
]);
API Responses
Standardize API error responses for Zend_Exception:
public function render($request, Zend_Exception $exception) {
return response()->json([
'success' => false,
'errors' => [
'code' => $exception->getCode(),
'message' => $exception->getMessage()
]
], $exception->getCode());
}
Avoid Namespace Collisions Use aliases or namespaces to prevent conflicts with Laravel’s built-in exceptions:
// In config/app.php
'aliases' => [
'LegacyException' => 'Zend_Exception',
];
Then use:
use LegacyException as Zend_Exception;
Leverage Laravel’s Service Container
Bind Zend_Exception to a custom factory if needed:
$this->app->bind('legacy.exception', function () {
return new Zend_Exception('Default legacy error');
});
Testing Legacy Exceptions
Write tests to ensure Zend_Exception behaves as expected in Laravel:
public function test_zend_exception_handling() {
$exception = new Zend_Exception('Test error');
$this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
throw_if(true, $exception);
}
Namespace Conflicts
Zend_Exception may conflict with Laravel’s Zend aliases or other packages using Zend namespaces.\Zend_Exception::class // Explicit namespace
Missing Throwable Interface
Zend_Exception does not natively implement PHP’s Throwable interface, which Laravel’s ExceptionHandler expects.Zend_Exception to implement Throwable:
class LaravelCompatibleZendException extends Zend_Exception implements Throwable {}
Autoloading Issues
composer.json autoload maps Zend_ to the correct path:
"autoload": {
"psr-0": {
"Zend_": "vendor/zf1/zend-exception/library/"
}
}
Stack Trace Formatting
Zend_Exception stack traces may not render correctly in Laravel’s debug tools.getTraceAsString() or use a custom renderer:
public function getTraceAsString() {
return 'Custom trace: ' . parent::getTraceAsString();
}
Dependency Bloat
zf1/zend-exception may pull in other ZF1 components via transitive dependencies.composer why zf1/zend-exception to audit dependencies and remove unused ones.Check Exception Type
Use instanceof to verify exception types in catches:
if ($exception instanceof Zend_Exception) {
// Handle legacy exception
}
Log Full Exception Details Log the entire exception object for debugging:
Log::debug('Full exception', [
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString()
]);
Test in Isolation
Test Zend_Exception handling in a controlled environment before deploying to production:
// In a test case
$this->expectException(Zend_Exception::class);
throw new Zend_Exception('Test exception');
Use for Legacy Code Only
Restrict Zend_Exception to legacy modules or services to avoid polluting new code.
Document Legacy Usage Add PHPDoc comments to mark legacy exception usage:
/**
* @throws Zend_Exception Legacy exception (to be replaced)
*/
public function legacyMethod() {}
Plan for Deprecation
Set a timeline to replace Zend_Exception with Laravel’s exceptions in future sprints.
Leverage Laravel’s Exception Features
Combine Zend_Exception with Laravel’s exception handling:
// In App\Exceptions\Handler
public function report(Throwable $exception) {
if ($exception instanceof Zend_Exception) {
// Custom reporting logic
}
parent::report($exception);
}
Custom Exception Classes
Create Laravel-compatible wrappers for Zend_Exception:
class LegacyException extends \Exception {
public function __construct(Zend_Exception $e) {
parent::__construct($e->getMessage(),
How can I help you explore Laravel packages today?