Download the PHP package alcamo/exception without Composer

On this page you can find all versions of the php package alcamo/exception. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package exception

Usage examples

Predefined exception classes

use alcamo\exception\InvalidEnumerator;

try {
    throw (new InvalidEnumerator())->setMessageContext(
        [
            'value' => 'fox',
            'expectedOneOf' => [ 'foo', 'bar', 'baz' ],
            'atLine' => 42
        ]
    );
} catch (\Exception $e) {
    echo $e->getMessage();
}

This will output

Invalid value "fox", expected one of ["foo", "bar", "baz"] at line 42.

The first part up to the comma is taken from InvalidEnumerator::NORMALIZED_MESSAGE, the rest is created from the context based on settings in MessageFactory.

A number of such predefined exception classes with default messages is provided, as well as a number of context item definitions. Both may easily be extended or modified.

Since the mechanism is based on the interface ExceptionInterface and the trait ExceptionTrait, it can easily be integrated into any existing exception class hierarchy.

Error handler

use alcamo\exception\ErrorHandler;

public function foo()
{
    $handler = new ErrorHandler();

... any error occurring here will throw an ErrorException ...

}

After leaving the scope, the $handler has been destroyed, hence previous error handling is restored.

Exception dumper

use alcamo\exception\Dumper;

try {

...

} catch (\Throwable $e) {
    echo (new Dumper())->dump($e);
}

Reports the details of the exception. Useful, for instance, in command-line interfaces.

Supplied generic interfaces, traits and classes

The following only explains what is needed to implement the mechanism illustrated above. The provided predefined exception classes are basically all trivial because they mainly consist of a class constant with a normalized message.

Interface MessageFactoryInterface

Simple interface requiring a method createMessage() which takes a normalized message and a context array and returns a string.

Class MessageFactory

Implementation of MessageFactoryInterface which extends the functionality of Wikimedia's NormalizedExceptionTrait::getMessageFromNormalizedMessage() in that it not only replaces placeholders by context elements but generates more message text from the context.

The generation of messages is configured by class constants which can be extended or modified in derived classes. The present implementation focuses on information where in the input data an exception was thrown.

Interface ExceptionInterface

Extends Wikimedia's INormalizedException with two methods to set the message context and to add data to an existing message context.

Trait ExceptionTrait

Extends Wikimedia's NormalizedExceptionTrait, implementing ExceptionInterface. Uses MessageFactory (or any injected object that implements MessageFactoryInterface) to generate a message. The underlying normalized message can be taken from a class constant.

Class ErrorHandler

Implements the RAII pattern to establish an error handler function as long as the ErrorHandler object exists. The provided error handler function throws an ErrorException and can be overridden in derived classes.

Class Dumper

Provides a method dump() to dump the details of a Throwable as multiline text. The pieces of output are created by individual methods so that it can easily be customized in derived classes.


All versions of exception with dependencies

PHP Build Version
Package Version
Requires php Version 7.3 - 8.5
wikimedia/normalized-exception Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package alcamo/exception contains the following files

Loading the files please wait ...