1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\Restful\Application\Exceptions; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\Restful\Validation\Error; |
7
|
|
|
use Nette; |
8
|
|
|
use Throwable; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* BadRequestException |
13
|
|
|
* @package kalanis\Restful\Application |
14
|
|
|
*/ |
15
|
1 |
|
class BadRequestException extends Nette\Application\BadRequestException |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** @var Error[] Some other errors appear in request */ |
19
|
|
|
public array $errors = []; |
20
|
|
|
|
21
|
|
|
/****************** Simple factories ******************/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Is thrown when trying to reach secured resource without authentication |
25
|
|
|
*/ |
26
|
|
|
public static function unauthorized(string $message = '', ?Throwable $previous = null): self |
27
|
|
|
{ |
28
|
|
|
return new self($message, 401, $previous); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Is thrown when access to this resource is forbidden |
33
|
|
|
*/ |
34
|
|
|
public static function forbidden(string $message = '', ?Throwable $previous = null): self |
35
|
|
|
{ |
36
|
|
|
return new self($message, 403, $previous); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Is thrown when resource's not found |
41
|
|
|
*/ |
42
|
|
|
public static function notFound(string $message = '', ?Throwable $previous = null): self |
43
|
|
|
{ |
44
|
1 |
|
return new self($message, 404, $previous); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Is thrown when request method (e.g. POST or PUT) is not allowed for this resource |
49
|
|
|
*/ |
50
|
|
|
public static function methodNotSupported(string $message = '', ?Throwable $previous = null): self |
51
|
|
|
{ |
52
|
1 |
|
return new self($message, 405, $previous); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Is thrown when this resource is not no longer available (e.g. with new API version) |
57
|
|
|
*/ |
58
|
|
|
public static function gone(string $message = '', ?Throwable $previous = null): self |
59
|
|
|
{ |
60
|
|
|
return new self($message, 410, $previous); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Is thrown when incorrect (or unknown) Content-Type was provided in request |
65
|
|
|
*/ |
66
|
|
|
public static function unsupportedMediaType(string $message = '', ?Throwable $previous = null): self |
67
|
|
|
{ |
68
|
|
|
return new self($message, 415, $previous); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Is thrown when validation problem appears |
73
|
|
|
* @param Error[] $errors during validation |
74
|
|
|
* @param string $message |
75
|
|
|
* @param Throwable|null $previous |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public static function unprocessableEntity(array $errors, string $message = '', ?Throwable $previous = null): self |
79
|
|
|
{ |
80
|
|
|
$e = new self($message, 422, $previous); |
81
|
|
|
$e->errors = $errors; |
82
|
|
|
return $e; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Is thrown to reject request due to rate limiting |
87
|
|
|
*/ |
88
|
|
|
public static function tooManyRequests(string $message = '', ?Throwable $previous = null): self |
89
|
|
|
{ |
90
|
|
|
return new self($message, 429, $previous); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|