You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 23, 2019. It is now read-only.
A Zend Expressive error handler which allows to implement different strategies to render error responses based on the accepted content-types.
10
+
A Zend Expressive error response generator which allows to implement different strategies to render error responses based on the accepted content-types.
11
11
12
12
### Context
13
13
14
-
This package has been created following this article https://blog.alejandrocelaya.com/2016/07/29/creating-a-content-based-error-handler-for-zend-expressive/.
14
+
This package was created following this article https://blog.alejandrocelaya.com/2016/07/29/creating-a-content-based-error-handler-for-zend-expressive/.
15
15
16
16
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.
17
17
18
-
After writing the article I have decided to create this package, so that everybody can install and use the provided solution in their own projects.
18
+
After writing the article I decided to create this package, so that everybody can install and use the provided solution in their own projects.
19
+
20
+
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.
19
21
20
22
### Installation
21
23
@@ -25,31 +27,32 @@ Use composer to install this package
25
27
26
28
### Usage
27
29
28
-
This package includes an error handler, the `Acelaya\ExpressiveErrorHandler\ErrorHandler\ContentBasedErrorResponseGenerator`, that can be used to replace default Zend Expressive implementations.
30
+
This package includes an error response generator, the `Acelaya\ExpressiveErrorHandler\ErrorHandler\ContentBasedErrorResponseGenerator`, that can be used to replace default Zend Expressive implementations.
29
31
30
-
It composes a plugin manager that fetches a concrete error handler at runtime, based on the Request's `Accept` header. Thus, you can use the Expressive's `TemplatedErrorHandler` to dispatch **text/html** request errors, Stratiglity's `FinalHandler` for **text/plain** errors, etc.
32
+
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.
31
33
32
34
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.
33
35
34
36
### Provided configuration
35
37
36
-
To get things easily working, a `ConfigProvider` is included, which automatically registers all the dependencies in the service container (including the `Zend\Expressive\ErroHandler` service).
38
+
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).
37
39
38
-
It also preregisters error handlers for html and plain text requests (The `TemplatedErrorHandler` and the `FinalHandler` as mentioned before).
40
+
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).
39
41
40
42
```php
41
43
<?php
44
+
use Acelaya\ExpressiveErrorHandler\ErrorHandler\Factory\PlainTextResponseGeneratorFactory;
45
+
use Zend\Expressive\Container\ErrorResponseGeneratorFactory;
@@ -69,32 +72,45 @@ In order to use the built-in ConfigProvider, create a config file with this cont
69
72
return (new Acelaya\ExpressiveErrorHandler\ConfigProvider())->__invoke();
70
73
```
71
74
72
-
If your are using the Expressive's ConfigManager ([mtymek/expressive-config-manager](https://github.com/mtymek/expressive-config-manager)), you can just pass the class name to it like this:
75
+
If your are using [zend config aggregator](https://github.com/zendframework/zend-config-aggregator), you can just pass the class name to it like this:
73
76
74
77
```php
75
-
return (new Zend\Expressive\ConfigManager([
78
+
return (new Zend\ConfigAggregator\ConfigAggregator([
Also, if you are using the [zend component installer](https://docs.zendframework.com/zend-component-installer/) package, it will ask you to register the ConfigProvider when installed.
86
+
82
87
### Override configuration
83
88
84
89
If you need to override any of the content types, its as easy as defining the same plugin with a different value.
85
90
86
-
For example, it is very likely that you want to use Expressive's `WhoopsErrorHandler` in development environments.
91
+
For example, it is very likely that you want to use Expressive's `WhoopsErrorResponseGenerator` in development environments.
87
92
88
93
Just define a local configuration file with this content and all the html requests will use it from now on:
89
94
90
95
```php
91
96
<?php
97
+
use Zend\Expressive\Container\WhoopsErrorResponseGeneratorFactory;
98
+
use Zend\Expressive\Container\WhoopsFactory;
99
+
use Zend\Expressive\Container\WhoopsPageHandlerFactory;
With this configuration, the `ContentBasedErrorResponseGenerator` will create the proper `JsonErroHandler` or `XmlErroHandler` at runtime, to dispatch json or xml errors.
148
+
With this configuration, the `ContentBasedErrorResponseGenerator` will create the proper `JsonErrorResponseGenerator` or `XmlErrorResponseGenerator` at runtime, to dispatch json or xml errors.
130
149
131
150
Similarly, you could need to override the default content type by setting the `default_content_type` property.
0 commit comments