Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions docs/book/v3/features/container/factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ When the `config` service is present, the factory can utilize two values:
- `zend-expressive.error_handler.template_error`, a name of an alternate
template to use (instead of the default represented in the
`Zend\Expressive\Middleware\ErrorResponseGenerator::TEMPLATE_DEFAULT`
constant).
constant, which evaluates to `error::error`).
- **Since 3.1.0**: `zend-expressive.error_handler.layout`, a name of an
alternate layout to use (instead of the default represented in the
`Zend\Expressive\Middleware\ErrorResponseGenerator::LAYOUT_DEFAULT` constant,
which evaluates to `layout::default`).

As an example:

Expand All @@ -90,6 +94,7 @@ As an example:
'zend-expressive' => [
'error_handler' => [
'template_error' => 'name of error template',
'layout' => 'layout::alternate',
],
],
```
Expand Down Expand Up @@ -150,14 +155,21 @@ When the `config` service is present, the factory can utilize two values:

- `zend-expressive.error_handler.template_404`, a name of an alternate
template to use (instead of the default represented in the
`Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant).
`Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant, which
evaluates to `error::404`).

- `zend-expressive.error_handler.layout`, a name of an alternate
template to use (instead of the default represented in the
`Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant, which
evaluates to `layout::default`).

As an example:

```php
'zend-expressive' => [
'error_handler' => [
'template_404' => 'name of 404 template',
'layout' => 'layout::alternate',
],
],
```
Expand Down
3 changes: 3 additions & 0 deletions docs/book/v3/features/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ We provide two error response generators for you:
otherwise, a plain text response is generated that notes the request method
and URI.

Since version 3.1.0, it also accepts a layout name, if you want to use one
other than `layout::default`.

- `Zend\Expressive\Middleware\WhoopsErrorResponseGenerator`, which uses
[whoops](http://filp.github.io/whoops/) to present detailed exception
and request information; this implementation is intended for development
Expand Down
5 changes: 4 additions & 1 deletion src/Container/ErrorResponseGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ public function __invoke(ContainerInterface $container) : ErrorResponseGenerator
$template = $config['zend-expressive']['error_handler']['template_error']
?? ErrorResponseGenerator::TEMPLATE_DEFAULT;

$layout = $config['zend-expressive']['error_handler']['layout']
?? ErrorResponseGenerator::LAYOUT_DEFAULT;

$renderer = $container->has(TemplateRendererInterface::class)
? $container->get(TemplateRendererInterface::class)
: null;

return new ErrorResponseGenerator($debug, $renderer, $template);
return new ErrorResponseGenerator($debug, $renderer, $template, $layout);
}
}
6 changes: 5 additions & 1 deletion src/Middleware/ErrorResponseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ class ErrorResponseGenerator
use ErrorResponseGeneratorTrait;

public const TEMPLATE_DEFAULT = 'error::error';
public const LAYOUT_DEFAULT = 'layout::default';

public function __construct(
bool $isDevelopmentMode = false,
TemplateRendererInterface $renderer = null,
string $template = self::TEMPLATE_DEFAULT
string $template = self::TEMPLATE_DEFAULT,
string $layout = self::LAYOUT_DEFAULT
) {
$this->debug = $isDevelopmentMode;
$this->renderer = $renderer;
$this->template = $template;
$this->layout = $layout;
}

public function __invoke(
Expand All @@ -49,6 +52,7 @@ public function __invoke(
'uri' => (string) $request->getUri(),
'status' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
'layout' => $this->layout,
],
$this->debug,
$response
Expand Down
7 changes: 7 additions & 0 deletions src/Response/ErrorResponseGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ trait ErrorResponseGeneratorTrait
*/
private $template;

/**
* Name of the layout to render.
*
* @var string
*/
private $layout;

private function prepareTemplatedResponse(
Throwable $e,
TemplateRendererInterface $renderer,
Expand Down
5 changes: 5 additions & 0 deletions test/Container/ErrorResponseGeneratorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testNoConfigurationCreatesInstanceWithDefaults()
$this->assertAttributeEquals(false, 'debug', $generator);
$this->assertAttributeEmpty('renderer', $generator);
$this->assertAttributeEquals('error::error', 'template', $generator);
$this->assertAttributeEquals('layout::default', 'layout', $generator);
}

public function testUsesDebugConfigurationToSetDebugFlag()
Expand All @@ -56,6 +57,7 @@ public function testUsesDebugConfigurationToSetDebugFlag()
$this->assertAttributeEquals(true, 'debug', $generator);
$this->assertAttributeEmpty('renderer', $generator);
$this->assertAttributeEquals('error::error', 'template', $generator);
$this->assertAttributeEquals('layout::default', 'layout', $generator);
}

public function testUsesConfiguredTemplateRenderToSetGeneratorRenderer()
Expand All @@ -70,6 +72,7 @@ public function testUsesConfiguredTemplateRenderToSetGeneratorRenderer()
$this->assertAttributeEquals(false, 'debug', $generator);
$this->assertAttributeSame($this->renderer->reveal(), 'renderer', $generator);
$this->assertAttributeEquals('error::error', 'template', $generator);
$this->assertAttributeEquals('layout::default', 'layout', $generator);
}

public function testUsesTemplateConfigurationToSetTemplate()
Expand All @@ -79,6 +82,7 @@ public function testUsesTemplateConfigurationToSetTemplate()
'zend-expressive' => [
'error_handler' => [
'template_error' => 'error::custom',
'layout' => 'layout::custom',
],
],
]);
Expand All @@ -90,5 +94,6 @@ public function testUsesTemplateConfigurationToSetTemplate()
$this->assertAttributeEquals(false, 'debug', $generator);
$this->assertAttributeEmpty('renderer', $generator);
$this->assertAttributeEquals('error::custom', 'template', $generator);
$this->assertAttributeEquals('layout::custom', 'layout', $generator);
}
}
2 changes: 2 additions & 0 deletions test/Middleware/ErrorResponseGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function testRendersTemplateWithoutErrorDetailsWhenRendererPresentAndNotI
'uri' => 'https://example.com/foo',
'status' => StatusCode::STATUS_INTERNAL_SERVER_ERROR,
'reason' => 'Internal Server Error',
'layout' => 'layout::default',
])
->willReturn('TEMPLATED CONTENTS');

Expand Down Expand Up @@ -198,6 +199,7 @@ public function testRendersTemplateWithErrorDetailsWhenRendererPresentAndInDebug
'status' => StatusCode::STATUS_INTERNAL_SERVER_ERROR,
'reason' => 'Network Connect Timeout Error',
'error' => $error,
'layout' => 'layout::default',
])
->willReturn('TEMPLATED CONTENTS');

Expand Down