dev-master
9999999-devExceptions
propietary
The Requires
by Matias Pino
Wallogit.com
2017 © Pedro Peláez
Exceptions
BaseException is an abstract class., (*1)
Run, (*2)
composer require aivo/exceptions
Or edit composer.json and add, (*3)
"require": {
"aivo/exceptions": "^1"
}
\Aivo\BaseException::__construct ([\Psr\Log\LoggerInterface $logger = null, $previous = null, $previousLevel = null])
A PSR-3 compatible object. If provided, it will call the logger's method matching the error level when the exception is . It's possible to add a logger after the object has been instantiated using \$\Aivo\BaseException->setLogger(\Psr\Log\LoggerInterface $logger) and \$\Aivo\BaseException->log()., (*4)
It is impossible to add a previous exception runtime so it has to be added while creating a new BaseException object., (*5)
If provided, it will log the previous exception provided using this level. This is useful for logging exceptions that do not extend \Aivo\BaseException, such as PDOException., (*6)
It is suggested that you create a new class for every message and code you need to send. Each code should also be unique, ideally on a global scope. This is because the error messages could change at any time so applications should not depend on the actual description text., (*7)
public function responseException(\Exception $exception, Response $response)
{
if ($exception instanceof \Aivo\BaseException) {
$data = $exception->__toArray();
$httpCode = $exception->getHttpCode();
} else {
$data = [
'class' => get_class($exception),
'error' => $exception->getCode(),
'message' => $exception->getMessage(),
];
$httpCode = 409;
}
return $response->withJson($data)
->withStatus($httpCode);
}
To log a previous exception (provided in the constructor), simply add a third parameter with the desired level. Level has to be PSR-3 compliant. Example:, (*8)
try {
throw new \Exception('Super secreta');
}
catch (\Exception $e) {
throw new \Aivo\Exceptions\Word\NotFound(
$this->logger(),
$e,
\Aivo\BaseException::ERROR
);
}
Matias Pino - mpino@aivo.co, (*9)
This project uses Semantic Versioning 2.0.0, (*10)
Exceptions
propietary