Skip to content

Commit f958585

Browse files
authored
Optimized the exception handler which add content-type header automatically by default. (#2373)
* Optimized validation exception handler. * Added test cases.
1 parent ee0063f commit f958585

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/ValidationExceptionHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public function handle(Throwable $throwable, ResponseInterface $response)
2323
$this->stopPropagation();
2424
/** @var \Hyperf\Validation\ValidationException $throwable */
2525
$body = $throwable->validator->errors()->first();
26+
if (! $response->hasHeader('content-type')) {
27+
$response = $response->withAddedHeader('content-type', 'text/plain; charset=utf-8');
28+
}
2629
return $response->withStatus($throwable->status)->withBody(new SwooleStream($body));
2730
}
2831

tests/Cases/ValidationExceptionTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
namespace HyperfTest\Validation\Cases;
1313

1414
use Hyperf\Contract\TranslatorInterface;
15+
use Hyperf\Contract\ValidatorInterface;
16+
use Hyperf\HttpMessage\Base\Response;
1517
use Hyperf\Utils\ApplicationContext;
1618
use Hyperf\Utils\MessageBag;
1719
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
1820
use Hyperf\Validation\ValidationException;
21+
use Hyperf\Validation\ValidationExceptionHandler;
1922
use Hyperf\Validation\ValidatorFactory;
2023
use Mockery;
2124
use PHPUnit\Framework\TestCase;
@@ -49,6 +52,30 @@ public function testWithMessages()
4952
], $errors->getMessages());
5053
}
5154

55+
public function testValidationExceptionHandler()
56+
{
57+
$handler = new ValidationExceptionHandler();
58+
$validator = Mockery::mock(ValidatorInterface::class);
59+
$validator->shouldReceive('errors')->andReturnUsing(function () {
60+
$message = Mockery::mock(MessageBag::class);
61+
$message->shouldReceive('first')->andReturn('id is required');
62+
return $message;
63+
});
64+
$exception = new ValidationException($validator);
65+
$response = new Response();
66+
$response = $handler->handle($exception, $response);
67+
$this->assertSame(422, $response->getStatusCode());
68+
$this->assertSame('id is required', $response->getBody()->getContents());
69+
$this->assertSame('text/plain; charset=utf-8', $response->getHeaderLine('content-type'));
70+
71+
$response = (new Response())->withAddedHeader('Content-Type', 'application/json; charset=utf-8');
72+
$exception = new ValidationException($validator);
73+
$response = $handler->handle($exception, $response);
74+
$this->assertSame(422, $response->getStatusCode());
75+
$this->assertSame('id is required', $response->getBody()->getContents());
76+
$this->assertSame('application/json; charset=utf-8', $response->getHeaderLine('content-type'));
77+
}
78+
5279
protected function getContainer()
5380
{
5481
$container = Mockery::mock(ContainerInterface::class);

0 commit comments

Comments
 (0)