• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13724522058

07 Mar 2025 04:04PM UTC coverage: 8.175% (-0.3%) from 8.518%
13724522058

Pull #7005

github

web-flow
Merge 322407532 into 1e0bc9dc8
Pull Request #7005: fix(validation): deprecate string message for ValidationException con…

4 of 6 new or added lines in 1 file covered. (66.67%)

159 existing lines in 24 files now uncovered.

12839 of 157045 relevant lines covered (8.18%)

13.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/docs/guides/error-resource.php
1
<?php
2
// ---
3
// slug: error-resource
4
// name: Error resource for domain exceptions
5
// position: 7
6
// executable: true
7
// tags: design, state
8
// ---
9

10
namespace App\ApiResource {
11
    use ApiPlatform\Metadata\ErrorResource;
12
    use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
13

14
    // We create a `MyDomainException` marked as an `ErrorResource`
15
    // It implements ProblemExceptionInterface as we want to be compatible with the [JSON Problem rfc7807](https://datatracker.ietf.org/doc/rfc7807/)
16
    #[ErrorResource]
17
    class MyDomainException extends \Exception implements ProblemExceptionInterface
18
    {
19
        public function getType(): string
20
        {
21
            return '/errors/418';
×
22
        }
23

24
        public function getTitle(): ?string
25
        {
26
            return 'Teapot error';
×
27
        }
28

29
        public function getStatus(): ?int
30
        {
31
            return 418;
×
32
        }
33

34
        public function getDetail(): ?string
35
        {
36
            return $this->getMessage();
×
37
        }
38

39
        public function getInstance(): ?string
40
        {
41
            return null;
×
42
        }
43

44
        public string $myCustomField = 'I usually prefer coffee.';
45
    }
46

47
    use ApiPlatform\Metadata\ApiResource;
48
    use ApiPlatform\Metadata\Get;
49
    use ApiPlatform\Metadata\Operation;
50

51
    #[ApiResource(
52
        operations: [
×
53
            new Get(provider: Book::class.'::provide'),
×
54
        ],
×
55
    )]
×
56
    class Book
57
    {
58
        public function __construct(
59
            public readonly int $id = 1,
60
            public readonly string $name = 'Anon',
61
        ) {
62
        }
×
63

64
        public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
65
        {
66
            // We throw our domain exception
67
            throw new MyDomainException('I am teapot');
×
68
        }
69
    }
70
}
71

72
namespace App\Tests {
73
    use ApiPlatform\Playground\Test\TestGuideTrait;
74
    use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
75

76
    final class BookTest extends ApiTestCase
77
    {
78
        use TestGuideTrait;
79

80
        public function testBookDoesNotExists(): void
81
        {
82
            static::createClient()->request('GET', '/books/1', options: ['headers' => ['accept' => 'application/ld+json']]);
×
83
            // We expect the status code returned by our `getStatus` and the message inside `detail`
84
            // for security reasons 500 errors will get their "detail" changed by our Error Provider
85
            // you can override this by looking at the [Error Provider guide](/docs/guides/error-provider).
86
            $this->assertResponseStatusCodeSame(418);
×
87
            $this->assertJsonContains([
×
88
                '@id' => '/my_domain_exceptions',
×
89
                '@type' => 'MyDomainException',
×
90
                'type' => '/errors/418',
×
91
                'title' => 'Teapot error',
×
92
                'detail' => 'I am teapot',
×
93
                'myCustomField' => 'I usually prefer coffee.'
×
94
            ]);
×
95
        }
96
    }
97
}
98

99
namespace App\Playground {
100
    use Symfony\Component\HttpFoundation\Request;
101

102
    function request(): Request
103
    {
104
        return Request::create('/books/1.jsonld', 'GET');
×
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc