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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 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'),
×
UNCOV
54
        ],
×
UNCOV
55
    )]
×
56
    class Book
57
    {
58
        public function __construct(
59
            public readonly int $id = 1,
60
            public readonly string $name = 'Anon',
61
        ) {
UNCOV
62
        }
×
63

64
        public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
65
        {
66
            // We throw our domain exception
UNCOV
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
        {
UNCOV
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([
×
UNCOV
88
                '@id' => '/my_domain_exceptions',
×
UNCOV
89
                '@type' => 'MyDomainException',
×
UNCOV
90
                'type' => '/errors/418',
×
UNCOV
91
                'title' => 'Teapot error',
×
UNCOV
92
                'detail' => 'I am teapot',
×
UNCOV
93
                'myCustomField' => 'I usually prefer coffee.'
×
UNCOV
94
            ]);
×
95
        }
96
    }
97
}
98

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

102
    function request(): Request
103
    {
UNCOV
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