1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\SymfonyJsonRpcHttpServerDoc\Listener; |
3
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInternalErrorException; |
5
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidParamsException; |
6
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidRequestException; |
7
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException; |
8
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcParseErrorException; |
9
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc; |
10
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ArrayDoc; |
11
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ObjectDoc; |
12
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc; |
13
|
|
|
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\ServerDocCreatedEvent; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ServerDocCreatedListener |
17
|
|
|
*/ |
18
|
|
|
class ServerDocCreatedListener |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param ServerDocCreatedEvent $event |
22
|
|
|
*/ |
23
|
4 |
|
public function appendJsonRpcServerErrorsDoc(ServerDocCreatedEvent $event) : void |
24
|
|
|
{ |
25
|
4 |
|
$event->getDoc() |
26
|
4 |
|
->addServerError( // Parse Error |
27
|
4 |
|
new ErrorDoc('Parse error', JsonRpcParseErrorException::CODE) |
28
|
|
|
) |
29
|
4 |
|
->addServerError( // Invalid request |
30
|
4 |
|
(new ErrorDoc('Invalid request', JsonRpcInvalidRequestException::CODE)) |
31
|
|
|
) |
32
|
4 |
|
->addServerError( // Method not found |
33
|
4 |
|
(new ErrorDoc('Method not found', JsonRpcMethodNotFoundException::CODE)) |
34
|
|
|
) |
35
|
|
|
; |
36
|
|
|
|
37
|
4 |
|
$addParamsValidationError = true; |
38
|
|
|
// Search for existing error in server errors list (could have been already defined by an another bundle) |
39
|
|
|
// @see "yoanm/symfony-jsonrpc-params-sf-constraints-doc" package |
40
|
4 |
|
foreach ($event->getDoc()->getServerErrorList() as $serverError) { |
41
|
4 |
|
if (JsonRpcInvalidParamsException::CODE === $serverError->getCode()) { |
42
|
1 |
|
$addParamsValidationError = false; |
43
|
4 |
|
break; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
if (true === $addParamsValidationError) { |
48
|
3 |
|
$event->getDoc()->addServerError( // Params validations error |
49
|
3 |
|
(new ErrorDoc('Params validations error', JsonRpcInvalidParamsException::CODE)) |
50
|
3 |
|
->setDataDoc( |
51
|
3 |
|
(new ObjectDoc()) |
52
|
3 |
|
->setAllowMissingSibling(false) |
53
|
3 |
|
->setAllowExtraSibling(false) |
54
|
3 |
|
->setRequired(true) |
55
|
3 |
|
->addSibling( |
56
|
3 |
|
(new ArrayDoc()) |
57
|
3 |
|
->setName(JsonRpcInvalidParamsException::DATA_VIOLATIONS_KEY) |
58
|
|
|
) |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
$event->getDoc()->addServerError( // Internal error |
64
|
4 |
|
(new ErrorDoc('Internal error', JsonRpcInternalErrorException::CODE)) |
65
|
4 |
|
->setDataDoc( |
66
|
4 |
|
(new ObjectDoc()) |
67
|
4 |
|
->setAllowMissingSibling(false) |
68
|
4 |
|
->setAllowExtraSibling(false) |
69
|
4 |
|
->setRequired(false) |
70
|
4 |
|
->addSibling( |
71
|
4 |
|
(new StringDoc()) |
72
|
4 |
|
->setName(JsonRpcInternalErrorException::DATA_PREVIOUS_KEY) |
73
|
4 |
|
->setDescription('Previous error message') |
74
|
|
|
) |
75
|
|
|
) |
76
|
|
|
); |
77
|
4 |
|
} |
78
|
|
|
} |
79
|
|
|
|