This repository was archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.php
More file actions
167 lines (146 loc) · 4.42 KB
/
errors.php
File metadata and controls
167 lines (146 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Response;
use Noherczeg\RestExt\Exceptions\ErrorMessageException;
use Noherczeg\RestExt\Exceptions\NotFoundException;
use Noherczeg\RestExt\Exceptions\PermissionException;
/*
|--------------------------------------------------------------------------
| Error Logging
|--------------------------------------------------------------------------
|
| We log any errors in our App.
|
*/
App::error(function(Exception $exception, $code)
{
Log::error($exception);
});
/*
|--------------------------------------------------------------------------
| HTTP Exceptions
|--------------------------------------------------------------------------
|
| HTTP Exceptions are translated to proper REST Responses.
|
| TODO Content should be loaded with the Localization tool
|
*/
App::error(function(Symfony\Component\HttpKernel\Exception\HttpException $e, $code)
{
$headers = $e->getHeaders();
$content = ["content" => null, "links" => [
["rel" => "self", "href" => URL::full()],
]];
switch ($code)
{
case 401:
$content['content'] = 'Invalid API key';
$headers['WWW-Authenticate'] = 'Basic realm="' . Config::get('restext::realm') . '"';
break;
case 403:
$content['content'] = 'Access denied';
break;
case 404:
$content['content'] = 'Requested Resource not found';
break;
case 406:
$content['content'] = 'Given Content-Type not acceptable';
break;
default:
$content['content'] = 'An unknown error occured';
}
return Response::json($e->getMessage() ?: $content, $code, $headers);
});
/*
|--------------------------------------------------------------------------
| Application Error Messages
|--------------------------------------------------------------------------
|
| Error Messages set ususaly when a user error occures.
|
*/
App::error(function(ErrorMessageException $e)
{
return Response::json([
'reason' => $e->getMessages()->all(),
'links' => [['rel' => 'self', 'href' => URL::full()]]
], 400);
});
/*
|--------------------------------------------------------------------------
| 404 Error
|--------------------------------------------------------------------------
|
|
*/
App::error(function(NotFoundException $e)
{
return Response::json([
'reason' => $e->getMessage() ?: 'Requested page not found',
'links' => [['rel' => 'self', 'href' => URL::full()]]
], 404);
});
/*
|--------------------------------------------------------------------------
| Permission Error
|--------------------------------------------------------------------------
|
| Unauthorized errors are handled here.
|
*/
App::error(function(PermissionException $e)
{
return Response::json([
'reason' => $e->getMessage() ?: 'Access denied',
'links' => [['rel' => 'self', 'href' => URL::full()]]
], 403);
});
/*
|--------------------------------------------------------------------------
| Repository Error
|--------------------------------------------------------------------------
|
| Sent when an Entity couldn't be found in the Repositories.
|
*/
App::error(function(ModelNotFoundException $e)
{
return Response::json([
'reason' => 'Requested Resource not found',
'links' => [['rel' => 'self', 'href' => URL::full()]]
], 404);
});
/*
|--------------------------------------------------------------------------
| Database Error
|--------------------------------------------------------------------------
|
| Redis doesn't respond to Requests
|
*/
App::error(function(\Predis\Connection\ConnectionException $e)
{
return Response::json([
'reason' => 'The Cache server is not responding',
'links' => [['rel' => 'self', 'href' => URL::full()]]
], 500);
});
/*
|--------------------------------------------------------------------------
| Database Error
|--------------------------------------------------------------------------
|
| The Relational Database doesn't respond to Requests.
|
*/
App::error(function(\Doctrine\DBAL\ConnectionException $e)
{
return Response::json([
'reason' => 'The Database server is not responding',
'links' => [['rel' => 'self', 'href' => URL::full()]]
], 500);
});