66use Tobyz \JsonApiServer \Exception \BadRequestException ;
77use Tobyz \JsonApiServer \Exception \ConflictException ;
88use Tobyz \JsonApiServer \Exception \ForbiddenException ;
9+ use Tobyz \JsonApiServer \Exception \Sourceable ;
910use Tobyz \JsonApiServer \Exception \UnprocessableEntityException ;
1011
1112use function Tobyz \JsonApiServer \get_value ;
@@ -27,39 +28,49 @@ private function parseData(Context $context): array
2728 $ body = (array ) $ context ->body ();
2829
2930 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+ ]);
3134 }
3235
3336 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+ ]);
3540 }
3641
3742 if (isset ($ context ->model )) {
3843 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+ ]);
4047 }
4148
4249 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 ( [
4451 'pointer ' => '/data/id ' ,
4552 ]);
4653 }
4754 } 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+ ]);
4958 }
5059
5160 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 ' ]);
5364 }
5465
5566 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 ( [
5768 'pointer ' => '/data/attributes ' ,
5869 ]);
5970 }
6071
6172 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 ( [
6374 'pointer ' => '/data/relationships ' ,
6475 ]);
6576 }
@@ -88,7 +99,7 @@ private function assertFieldsExist(Context $context, array $data): void
8899 foreach (['attributes ' , 'relationships ' ] as $ location ) {
89100 foreach ($ data [$ location ] as $ name => $ value ) {
90101 if (!isset ($ fields [$ name ]) || $ location !== location ($ fields [$ name ])) {
91- throw new BadRequestException ("Unknown field [ $ name] " , [
102+ throw ( new BadRequestException ("Unknown field [ $ name] " ))-> setSource ( [
92103 'pointer ' => "/data/ $ location/ $ name " ,
93104 ]);
94105 }
@@ -109,7 +120,9 @@ private function assertFieldsWritable(Context $context, array $data): void
109120 }
110121
111122 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+ ]);
113126 }
114127 }
115128 }
@@ -128,7 +141,7 @@ private function deserializeValues(Context $context, array &$data): void
128141
129142 try {
130143 set_value ($ data , $ field , $ field ->deserializeValue ($ value , $ context ));
131- } catch (BadRequestException $ e ) {
144+ } catch (Sourceable $ e ) {
132145 throw $ e ->prependSource ([
133146 'pointer ' => '/data/ ' . location ($ field ) . '/ ' . $ field ->name ,
134147 ]);
0 commit comments