Skip to content

Commit c1bea6c

Browse files
committed
Added some error response methods
1 parent a827d63 commit c1bea6c

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

src/Endpoint/Concerns/BuildsOpenApiPaths.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,107 @@ private function buildPaginatableParameters(): array
9999
],
100100
];
101101
}
102+
103+
public function buildBadRequestErrorResponse(): array
104+
{
105+
return $this->buildErrorResponse(
106+
'A bad request.',
107+
400,
108+
'Bad Request',
109+
'Please try again with a valid request.',
110+
);
111+
}
112+
113+
public function buildUnauthorizedErrorResponse(): array
114+
{
115+
return $this->buildErrorResponse(
116+
'An unauthorised error.',
117+
401,
118+
'Unauthorized',
119+
'Please login and try again.',
120+
);
121+
}
122+
123+
public function buildForbiddenErrorResponse(): array
124+
{
125+
return $this->buildErrorResponse(
126+
'A forbidden error.',
127+
403,
128+
'Forbidden',
129+
);
130+
}
131+
132+
public function buildNotFoundErrorResponse(): array
133+
{
134+
return $this->buildErrorResponse(
135+
'A bad request.',
136+
404,
137+
'Not Found',
138+
'The requested resource could not be found.',
139+
);
140+
}
141+
142+
public function buildInternalServerErrorResponse(): array
143+
{
144+
return $this->buildErrorResponse(
145+
'A bad request.',
146+
500,
147+
'Internal Server Error',
148+
'Please try again later.',
149+
);
150+
}
151+
152+
public function buildErrorResponse(string $description, int $status, string $title, ?string $detail = null): array
153+
{
154+
return [
155+
'description' => $description,
156+
'content' => [
157+
JsonApi::MEDIA_TYPE => [
158+
'schema' => [
159+
'type' => 'object',
160+
'properties' => [
161+
'errors' => [
162+
'type' => 'array',
163+
'items' => [
164+
'type' => 'object',
165+
'required' => [
166+
'status',
167+
'title',
168+
],
169+
'properties' => array_filter([
170+
'status' => [
171+
'type' => 'string',
172+
'example' => (string)$status,
173+
],
174+
'title' => [
175+
'type' => 'string',
176+
'example' => $title,
177+
],
178+
'detail' => [
179+
'type' => 'string',
180+
'example' => $detail,
181+
],
182+
'source' => [
183+
'type' => 'object',
184+
'properties' => [
185+
'pointer' => [
186+
'type' => 'string',
187+
],
188+
'parameter' => [
189+
'type' => 'string',
190+
],
191+
'header' => [
192+
'type' => 'string',
193+
],
194+
],
195+
],
196+
]),
197+
],
198+
],
199+
],
200+
],
201+
],
202+
],
203+
];
204+
}
102205
}

0 commit comments

Comments
 (0)