6
6
use Tobyz \JsonApiServer \Exception \BadRequestException ;
7
7
use Tobyz \JsonApiServer \Exception \ConflictException ;
8
8
use Tobyz \JsonApiServer \Exception \ForbiddenException ;
9
+ use Tobyz \JsonApiServer \Exception \Sourceable ;
9
10
use Tobyz \JsonApiServer \Exception \UnprocessableEntityException ;
10
11
11
12
use function Tobyz \JsonApiServer \get_value ;
@@ -27,39 +28,49 @@ private function parseData(Context $context): array
27
28
$ body = (array ) $ context ->body ();
28
29
29
30
if (!isset ($ body ['data ' ]) || !is_array ($ body ['data ' ])) {
30
- throw new BadRequestException ('data must be an object ' , ['pointer ' => '/data ' ]);
31
+ throw (new BadRequestException ('data must be an object ' ))->setSource ([
32
+ 'pointer ' => '/data ' ,
33
+ ]);
31
34
}
32
35
33
36
if (!isset ($ body ['data ' ]['type ' ])) {
34
- throw new BadRequestException ('data.type must be present ' , ['pointer ' => '/data/type ' ]);
37
+ throw (new BadRequestException ('data.type must be present ' ))->setSource ([
38
+ 'pointer ' => '/data/type ' ,
39
+ ]);
35
40
}
36
41
37
42
if (isset ($ context ->model )) {
38
43
if (!isset ($ body ['data ' ]['id ' ])) {
39
- throw new BadRequestException ('data.id must be present ' , ['pointer ' => '/data/id ' ]);
44
+ throw (new BadRequestException ('data.id must be present ' ))->setSource ([
45
+ 'pointer ' => '/data/id ' ,
46
+ ]);
40
47
}
41
48
42
49
if ($ body ['data ' ]['id ' ] !== $ context ->resource ->getId ($ context ->model , $ context )) {
43
- throw new ConflictException ('data.id does not match the resource ID ' , [
50
+ throw ( new ConflictException ('data.id does not match the resource ID ' ))-> setSource ( [
44
51
'pointer ' => '/data/id ' ,
45
52
]);
46
53
}
47
54
} elseif (isset ($ body ['data ' ]['id ' ])) {
48
- throw new ForbiddenException ('Client-generated IDs are not supported ' );
55
+ throw (new ForbiddenException ('Client-generated IDs are not supported ' ))->setSource ([
56
+ 'pointer ' => '/data/id ' ,
57
+ ]);
49
58
}
50
59
51
60
if (!in_array ($ body ['data ' ]['type ' ], $ context ->collection ->resources ())) {
52
- throw new ConflictException ('collection does not support this resource type ' );
61
+ throw (new ConflictException (
62
+ 'collection does not support this resource type ' ,
63
+ ))->setSource (['pointer ' => '/data/type ' ]);
53
64
}
54
65
55
66
if (isset ($ body ['data ' ]['attributes ' ]) && !is_array ($ body ['data ' ]['attributes ' ])) {
56
- throw new BadRequestException ('data.attributes must be an object ' , [
67
+ throw ( new BadRequestException ('data.attributes must be an object ' ))-> setSource ( [
57
68
'pointer ' => '/data/attributes ' ,
58
69
]);
59
70
}
60
71
61
72
if (isset ($ body ['data ' ]['relationships ' ]) && !is_array ($ body ['data ' ]['relationships ' ])) {
62
- throw new BadRequestException ('data.relationships must be an object ' , [
73
+ throw ( new BadRequestException ('data.relationships must be an object ' ))-> setSource ( [
63
74
'pointer ' => '/data/relationships ' ,
64
75
]);
65
76
}
@@ -88,7 +99,7 @@ private function assertFieldsExist(Context $context, array $data): void
88
99
foreach (['attributes ' , 'relationships ' ] as $ location ) {
89
100
foreach ($ data [$ location ] as $ name => $ value ) {
90
101
if (!isset ($ fields [$ name ]) || $ location !== location ($ fields [$ name ])) {
91
- throw new BadRequestException ("Unknown field [ $ name] " , [
102
+ throw ( new BadRequestException ("Unknown field [ $ name] " ))-> setSource ( [
92
103
'pointer ' => "/data/ $ location/ $ name " ,
93
104
]);
94
105
}
@@ -109,7 +120,9 @@ private function assertFieldsWritable(Context $context, array $data): void
109
120
}
110
121
111
122
if (!$ field ->isWritable ($ context ->withField ($ field ))) {
112
- throw new ForbiddenException ("Field [ $ field ->name ] is not writable " );
123
+ throw (new ForbiddenException ("Field [ $ field ->name ] is not writable " ))->setSource ([
124
+ 'pointer ' => '/data/ ' . location ($ field ) . '/ ' . $ field ->name ,
125
+ ]);
113
126
}
114
127
}
115
128
}
@@ -128,7 +141,7 @@ private function deserializeValues(Context $context, array &$data): void
128
141
129
142
try {
130
143
set_value ($ data , $ field , $ field ->deserializeValue ($ value , $ context ));
131
- } catch (BadRequestException $ e ) {
144
+ } catch (Sourceable $ e ) {
132
145
throw $ e ->prependSource ([
133
146
'pointer ' => '/data/ ' . location ($ field ) . '/ ' . $ field ->name ,
134
147
]);
0 commit comments