13
13
14
14
namespace ApiPlatform \Laravel \ApiResource ;
15
15
16
- use ApiPlatform \JsonLd \ ContextBuilderInterface ;
16
+ use ApiPlatform \JsonSchema \ SchemaFactory ;
17
17
use ApiPlatform \Metadata \ApiProperty ;
18
18
use ApiPlatform \Metadata \Error as Operation ;
19
19
use ApiPlatform \Metadata \ErrorResource ;
20
+ use ApiPlatform \Metadata \ErrorResourceInterface ;
20
21
use ApiPlatform \Metadata \Exception \HttpExceptionInterface ;
21
22
use ApiPlatform \Metadata \Exception \ProblemExceptionInterface ;
23
+ use ApiPlatform \State \ErrorProvider ;
22
24
use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface as SymfonyHttpExceptionInterface ;
23
25
use Symfony \Component \Serializer \Annotation \Groups ;
24
26
use Symfony \Component \Serializer \Annotation \Ignore ;
25
27
use Symfony \Component \Serializer \Annotation \SerializedName ;
26
28
use Symfony \Component \WebLink \Link ;
27
29
28
30
#[ErrorResource(
29
- types: [ ' hydra:Error ' ] ,
31
+ uriTemplate: ' /errors/{status}{._format} ' ,
30
32
openapi: false ,
31
33
uriVariables: ['status ' ],
32
34
operations: [
33
35
new Operation (
36
+ errors: [],
34
37
name: '_api_errors_problem ' ,
35
- outputFormats: ['json ' => ['application/problem+json ' ]],
38
+ routeName: '_api_errors ' ,
39
+ outputFormats: ['json ' => ['application/problem+json ' , 'application/json ' ]],
40
+ hideHydraOperation: true ,
36
41
normalizationContext: [
42
+ SchemaFactory::OPENAPI_DEFINITION_NAME => '' ,
37
43
'groups ' => ['jsonproblem ' ],
38
44
'skip_null_values ' => true ,
45
+ 'ignored_attributes ' => ['trace ' , 'file ' , 'line ' , 'code ' , 'message ' , 'traceAsString ' , 'previous ' ],
39
46
],
40
- uriTemplate: '/errors/{status} '
41
47
),
42
48
new Operation (
49
+ errors: [],
43
50
name: '_api_errors_hydra ' ,
44
- outputFormats: ['jsonld ' => ['application/problem+json ' ]],
51
+ routeName: '_api_errors ' ,
52
+ outputFormats: ['jsonld ' => ['application/problem+json ' , 'application/ld+json ' ]],
45
53
normalizationContext: [
54
+ SchemaFactory::OPENAPI_DEFINITION_NAME => '' ,
46
55
'groups ' => ['jsonld ' ],
47
56
'skip_null_values ' => true ,
57
+ 'ignored_attributes ' => ['trace ' , 'file ' , 'line ' , 'code ' , 'message ' , 'traceAsString ' , 'previous ' ],
48
58
],
49
- links: [new Link (rel: ContextBuilderInterface::JSONLD_NS .'error ' , href: 'http://www.w3.org/ns/hydra/error ' )],
50
- uriTemplate: '/errors/{status}.jsonld '
59
+ links: [new Link (rel: 'http://www.w3.org/ns/json-ld#error ' , href: 'http://www.w3.org/ns/hydra/error ' )],
51
60
),
52
61
new Operation (
62
+ errors: [],
53
63
name: '_api_errors_jsonapi ' ,
64
+ routeName: '_api_errors ' ,
65
+ hideHydraOperation: true ,
54
66
outputFormats: ['jsonapi ' => ['application/vnd.api+json ' ]],
55
- normalizationContext: ['groups ' => ['jsonapi ' ], 'skip_null_values ' => true ],
56
- uriTemplate: '/errors/{status}.jsonapi '
67
+ normalizationContext: [
68
+ SchemaFactory::OPENAPI_DEFINITION_NAME => '' ,
69
+ 'disable_json_schema_serializer_groups ' => false ,
70
+ 'groups ' => ['jsonapi ' ],
71
+ 'skip_null_values ' => true ,
72
+ 'ignored_attributes ' => ['trace ' , 'file ' , 'line ' , 'code ' , 'message ' , 'traceAsString ' , 'previous ' ],
73
+ ],
74
+ ),
75
+ new Operation (
76
+ name: '_api_errors ' ,
77
+ hideHydraOperation: true ,
78
+ extraProperties: ['_api_disable_swagger_provider ' => true ],
79
+ outputFormats: ['html ' => ['text/html ' ], 'jsonapi ' => ['application/vnd.api+json ' ], 'jsonld ' => ['application/ld+json ' ], 'json ' => ['application/problem+json ' , 'application/json ' ]],
57
80
),
58
81
],
59
- graphQlOperations: []
82
+ outputFormats: ['jsonapi ' => ['application/vnd.api+json ' ], 'jsonld ' => ['application/ld+json ' ], 'json ' => ['application/problem+json ' , 'application/json ' ]],
83
+ provider: ErrorProvider::class,
84
+ graphQlOperations: [],
85
+ description: 'A representation of common errors. ' ,
60
86
)]
61
- class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface
87
+ #[ApiProperty(property: 'previous ' , hydra: false , readable: false )]
88
+ #[ApiProperty(property: 'traceAsString ' , hydra: false , readable: false )]
89
+ #[ApiProperty(property: 'string ' , hydra: false , readable: false )]
90
+ class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface, ErrorResourceInterface
62
91
{
63
92
/**
64
93
* @var array<int, mixed>
@@ -73,7 +102,7 @@ public function __construct(
73
102
private readonly string $ title ,
74
103
private readonly string $ detail ,
75
104
#[ApiProperty(identifier: true )] private int $ status ,
76
- array $ originalTrace ,
105
+ array $ originalTrace = [] ,
77
106
private readonly ?string $ instance = null ,
78
107
private string $ type = 'about:blank ' ,
79
108
private array $ headers = [],
0 commit comments