This repository was archived by the owner on Jan 29, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork192
Provide a dedicated service for server request error response generation#562
Merged
weierophinney merged 5 commits intozendframework:release-3.0.0fromweierophinney:hotfix/use-constants-not-virtual-servicesFeb 22, 2018
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
6329c34 Provide a dedicated service for the server request error response gen…
weierophinney84dc641 Adds tests covering ServerRequestErrorResponseGenerator behavior
weierophinneyf99aa57 Adds missing import statement in test case
weierophinney9f948f3 Refactors for better type safety
weierophinney457bc1f Adds CHANGELOG entries for #562
weierophinneyFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
23 changes: 21 additions & 2 deletionsCHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletionsrc/ConfigProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 6 additions & 7 deletionssrc/Container/RequestHandlerRunnerFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
27 changes: 19 additions & 8 deletionssrc/Container/ServerRequestErrorResponseGeneratorFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
102 changes: 17 additions & 85 deletionssrc/Middleware/ErrorResponseGenerator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletionssrc/Response/ErrorResponseGeneratorTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| <?php | ||
| /** | ||
| * @see https://github.com/zendframework/zend-expressive for the canonical source repository | ||
| * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
| * @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License | ||
| */ | ||
| declare(strict_types=1); | ||
| namespace Zend\Expressive\Response; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Throwable; | ||
| use Zend\Expressive\Template\TemplateRendererInterface; | ||
| trait ErrorResponseGeneratorTrait | ||
| { | ||
| /** | ||
| * Whether or not we are in debug/development mode. | ||
| * | ||
| * @var bool | ||
| */ | ||
| private $debug; | ||
| /** | ||
| * @param TemplateRendererInterface | ||
| */ | ||
| private $renderer; | ||
| /** | ||
| * @var string | ||
| */ | ||
| private $stackTraceTemplate = <<<'EOT' | ||
| %s raised in file %s line %d: | ||
| Message: %s | ||
| Stack Trace: | ||
| %s | ||
| EOT; | ||
| /** | ||
| * Name of the template to render. | ||
| * | ||
| * @var string | ||
| */ | ||
| private $template; | ||
| private function prepareTemplatedResponse( | ||
| Throwable $e, | ||
| TemplateRendererInterface $renderer, | ||
| array $templateData, | ||
| bool $debug, | ||
| ResponseInterface $response | ||
| ) : ResponseInterface { | ||
| if ($debug) { | ||
| $templateData['error'] = $e; | ||
| } | ||
| $response->getBody() | ||
| ->write($renderer->render($this->template, $templateData)); | ||
| return $response; | ||
| } | ||
| private function prepareDefaultResponse( | ||
| Throwable $e, | ||
| bool $debug, | ||
| ResponseInterface $response | ||
| ) : ResponseInterface { | ||
| $message = 'An unexpected error occurred'; | ||
| if ($debug) { | ||
| $message .= "; stack trace:\n\n" . $this->prepareStackTrace($e); | ||
| } | ||
| $response->getBody()->write($message); | ||
| return $response; | ||
| } | ||
| /** | ||
| * Prepares a stack trace to display. | ||
| */ | ||
| private function prepareStackTrace(Throwable $e) : string | ||
| { | ||
| $message = ''; | ||
| do { | ||
| $message .= sprintf( | ||
| $this->stackTraceTemplate, | ||
| get_class($e), | ||
| $e->getFile(), | ||
| $e->getLine(), | ||
| $e->getMessage(), | ||
| $e->getTraceAsString() | ||
| ); | ||
| } while ($e = $e->getPrevious()); | ||
| return $message; | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.