|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the FreshCommonApiBundle |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Artem Genvald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Fresh\CommonApiBundle\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use Fresh\CommonApiBundle\Exception\ServerInternalErrorException; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ExceptionHelperTrait. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Artem Genvald <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
trait ExceptionHelperTrait |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Returns a BadRequestHttpException. |
|
26
|
|
|
* |
|
27
|
|
|
* This will result in a 400 response code. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $message A message |
|
30
|
|
|
* @param \Exception|null $previous The previous exception |
|
31
|
|
|
* |
|
32
|
|
|
* @return BadRequestHttpException |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function createBadRequestException($message = 'Wrong Request', \Exception $previous = null) |
|
35
|
|
|
{ |
|
36
|
|
|
return new BadRequestHttpException($message, $previous); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns a UnauthorizedHttpException. |
|
41
|
|
|
* |
|
42
|
|
|
* This will result in a 401 response code. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $message A message |
|
45
|
|
|
* @param \Exception|null $previous The previous exception |
|
46
|
|
|
* |
|
47
|
|
|
* @return UnauthorizedHttpException |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function createUnauthorizedException($message = 'Invalid Credentials', \Exception $previous = null) |
|
50
|
|
|
{ |
|
51
|
|
|
return new UnauthorizedHttpException('Basic realm="My Realm"', $message, $previous); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns a ServerInternalErrorException. |
|
56
|
|
|
* |
|
57
|
|
|
* This will result in a 500 response code. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $message A message |
|
60
|
|
|
* @param \Exception|null $previous The previous exception |
|
61
|
|
|
* |
|
62
|
|
|
* @return ServerInternalErrorException |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function createInternalServerErrorException($message = 'Internal Server Error', \Exception $previous = null) |
|
65
|
|
|
{ |
|
66
|
|
|
return new ServerInternalErrorException($message, $previous); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|