2017 © Pedro Peláez
 

library exceptions

Exceptions

image

aivo/exceptions

Exceptions

  • Tuesday, October 25, 2016
  • by Pnoexz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10,075 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 16 % Grown

The README.md

Exceptions

BaseException is an abstract class., (*1)

Installation

Run, (*2)

composer require aivo/exceptions

Or edit composer.json and add, (*3)

    "require": {
        "aivo/exceptions": "^1"
    }

Function Declaration

\Aivo\BaseException::__construct ([\Psr\Log\LoggerInterface $logger = null, $previous = null, $previousLevel = null])

\Psr\Log\LoggerInterface $logger

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)

\Exception $previous

It is impossible to add a previous exception runtime so it has to be added while creating a new BaseException object., (*5)

string $previousLevel

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)

Extending the object

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)

Catching the object

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);
}

Logging the previous exception

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
    );
}

Author

Matias Pino - mpino@aivo.co, (*9)

This project uses Semantic Versioning 2.0.0, (*10)

The Versions

25/10 2016

dev-master

9999999-dev

Exceptions

  Sources   Download

propietary

The Requires

 

by Matias Pino

25/10 2016

1.0.0

1.0.0.0

Exceptions

  Sources   Download

propietary

The Requires

 

by Matias Pino

25/10 2016

1.0.0-RC

1.0.0.0-RC

Exceptions

  Sources   Download

propietary

The Requires

 

by Matias Pino