Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge c424ca4 into 4a710c6
Browse files Browse the repository at this point in the history
  • Loading branch information
PBXg33k committed Oct 19, 2017
2 parents 4a710c6 + c424ca4 commit 66afd75
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/EventListener/VndErrorExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Ramsey\VndError\VndError;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
Expand Down Expand Up @@ -77,24 +76,17 @@ public function onKernelException(GetResponseForExceptionEvent $event)
$statusCode = Response::HTTP_BAD_REQUEST;
$vndError = $this->validationErrorFactory->create($request, $exception, $logRef);
} else {
switch(true) {
case $exception instanceof NotFoundHttpException:
$statusCode = Response::HTTP_NOT_FOUND;
$severity = LogLevel::INFO;
break;
case $exception instanceof MethodNotAllowedHttpException:
if ($exception instanceof NotFoundHttpException) {
$statusCode = Response::HTTP_NOT_FOUND;
$severity = LogLevel::INFO;
} else {
if ($exception instanceof MethodNotAllowedHttpException) {
$statusCode = Response::HTTP_METHOD_NOT_ALLOWED;
$severity = LogLevel::WARNING;
break;
case $exception instanceof AuthenticationException:
} elseif ($exception instanceof AuthenticationException) {
$statusCode = Response::HTTP_UNAUTHORIZED;
$severity = LogLevel::WARNING;
break;
case $exception instanceof AccessDeniedHttpException:
$statusCode = Response::HTTP_FORBIDDEN;
$severity = LogLevel::WARNING;
break;
default:
} else {
$is3Digits = strlen($code) === 3;
$class = (int)substr($code, 0, 1);
if (!$is3Digits) {
Expand All @@ -115,8 +107,8 @@ public function onKernelException(GetResponseForExceptionEvent $event)
$severity = LogLevel::CRITICAL;
}
}
}
}

$message = Response::$statusTexts[$statusCode];
$vndError = new VndError($message, $logRef);
$vndError->addLink('help', $request->get('_definition'), ['title' => 'Error Information']);
Expand Down
3 changes: 3 additions & 0 deletions src/Response/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public function createResponse(Request $request, $data)
foreach (array_keys((array)$operationDefinition->responses) as $statusCode) {
if ($statusCode == 204) {
$understands204 = true;
break;
}
}
foreach (array_keys((array)$operationDefinition->responses) as $statusCode) {
if (2 == substr($statusCode, 0, 1)) {
$responseCode = $statusCode;
break;
Expand Down
16 changes: 16 additions & 0 deletions src/Tests/Functional/PetStore/app/swagger/composite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ paths:
description: "The requested resource"
schema:
type: object
/maintenance:
get:
summary: 'Check for maintenance messages'
description: ''
operationId: getMaintenance
produces:
- application/json
responses:
200:
description: 'There is a maintenance message'
schema:
type: object
additionalProperties:
type: string
204:
description: 'Succesfull operation, but no maintenance message to show'

responses:
Created:
Expand Down
8 changes: 8 additions & 0 deletions src/Tests/Response/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function willNotUse204ForNullResponsesWhenNotInDocument()
$this->assertNotEquals(204, $this->createResponse(null, '/pet/{id}', 'PUT')->getStatusCode());
}

/**
* @test
*/
public function willReturn204OnEmptyResponseWithMultipl2xxStatusCodesFromDocument()
{
$this->assertEquals(204, $this->createResponse(null, '/maintenance', 'GET')->getStatusCode());
}

/**
* @param mixed $data
* @param string $path
Expand Down

0 comments on commit 66afd75

Please sign in to comment.