Download the PHP package acelaya/ze-content-based-error-handler without Composer

On this page you can find all versions of the php package acelaya/ze-content-based-error-handler. 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 ze-content-based-error-handler

:warning: This project is deprecated :warning:

If you need to return different kinds of responses based on the request accepted content type, its easier to pipe different middlewares for every type, and let the next handler to be called if one is not able to generate the response.

Zend Expressive ContentBasedErrorResponseGenerator

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License Paypal Donate

A Zend Expressive error response generator which allows to implement different strategies to render error responses based on the accepted content-types.

Context

This package was created following this article https://blog.alejandrocelaya.com/2016/07/29/creating-a-content-based-error-handler-for-zend-expressive/.

On it, I demonstrate how to implement an strategy-based system which generates different error responses by taking into account the request's Accept header.

After writing the article I decided to create this package, so that everybody can install and use the provided solution in their own projects.

The package has then evolved to support expressive 2, which completely drops the concept of error handlers. Instead, from v2, this provides error response generators.

Installation

Use composer to install this package

composer require acelaya/ze-content-based-error-handler

Usage

This package includes an error response generator, the Acelaya\ExpressiveErrorHandler\ErrorHandler\ContentBasedErrorResponseGenerator, that can be used to replace default Zend Expressive implementations.

It composes a plugin manager that fetches a concrete error response generator at runtime, based on the Request's Accept header. Thus, you can use the Expressive's ErrorResponseGenerator to dispatch text/html request errors, Stratiglity's ErrorResponseGenerator for text/plain errors, etc.

You can also provide your own implementations for other content-types, like application/json or text/xml. The ContentBasedErrorResponseGenerator will automatically use the proper implementation.

Provided configuration

To get things easily working, a ConfigProvider is included, which automatically registers all the dependencies in the service container (including the Zend\Expressive\Middleware\ErrorResponseGenerator service).

It also preregisters error handlers for html and plain text requests (The Zend\Expressive\Middleware\ErrorResponseGenerator and the Zend\Stratigility\Middleware\ErrorResponseGenerator as mentioned before).

The plugins block is the one consumed by the plugin manager. For more information on how plugin managers work, read this.

In order to use the built-in ConfigProvider, create a config file with this contents:

If your are using zend config aggregator, you can just pass the class name to it like this:

Also, if you are using the zend component installer package, it will ask you to register the ConfigProvider when installed.

Override configuration

If you need to override any of the content types, its as easy as defining the same plugin with a different value.

For example, it is very likely that you want to use Expressive's WhoopsErrorResponseGenerator in development environments.

Just define a local configuration file with this content and all the html requests will use it from now on:

You will probably need to define other error handlers for different content types. You can do it by using the same structure.

With this configuration, the ContentBasedErrorResponseGenerator will create the proper JsonErrorResponseGenerator or XmlErrorResponseGenerator at runtime, to dispatch json or xml errors.

Similarly, you could need to override the default content type by setting the default_content_type property.

This way, when no Accept header was provided from the client or none of the accepted content types is registered, the application/json content type will be used, instead of text/html, which is the default behavior.

Log errors

This package allows you to provided a psr-3 logger to the ContentBasedErrorResponseGenerator, in order to get errors logged.

By default a Psr\Log\NullLogger is used, so no errors will be logged, but if a logger is registered under the Psr\Log\LoggerInterface service name, it will be injected in the ContentBasedErrorResponseGenerator when created.

The logged message can be customized too. The ContentBasedErrorResponseGenerator expects an object implementing Acelaya\ExpressiveErrorHandler\Log\LogMessageBuilderInterface to be injected on it.

A base implementation is provided, the Acelaya\ExpressiveErrorHandler\Log\BasicLogMessageBuilder, which basically logs the message "Error occurred while dispatching request" and appends the error on a new line.

You can easily override that by creating your own service implementing the interface, and registering it with the Acelaya\ExpressiveErrorHandler\Log\LogMessageBuilderInterface name.


All versions of ze-content-based-error-handler with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2
psr/log Version ^1.0
zendframework/zend-expressive Version ^3.0
zendframework/zend-servicemanager Version ^3.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 acelaya/ze-content-based-error-handler contains the following files

Loading the files please wait ....