mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Add additional tests cases, implemented custom error formatter
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace api\modules\mojang\behaviors;
|
||||
|
||||
use Closure;
|
||||
use Yii;
|
||||
use yii\base\Behavior;
|
||||
use yii\base\Event;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\web\Response;
|
||||
use yii\web\UnauthorizedHttpException;
|
||||
|
||||
final class ServiceErrorConverterBehavior extends Behavior {
|
||||
|
||||
public function events(): array {
|
||||
return [
|
||||
Response::EVENT_BEFORE_SEND => Closure::fromCallable([$this, 'beforeSend']),
|
||||
];
|
||||
}
|
||||
|
||||
private function beforeSend(Event $event): void {
|
||||
/** @var Response $response */
|
||||
$response = $event->sender;
|
||||
$data = $response->data;
|
||||
if ($data === null || !isset($data['status'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$request = Yii::$app->request;
|
||||
$type = $data['type'];
|
||||
switch ($type) {
|
||||
case UnauthorizedHttpException::class:
|
||||
$response->data = [
|
||||
'path' => '/' . $request->getPathInfo(),
|
||||
'errorType' => 'UnauthorizedOperationException',
|
||||
'error' => 'UnauthorizedOperationException',
|
||||
'errorMessage' => 'Unauthorized',
|
||||
'developerMessage' => 'Unauthorized',
|
||||
];
|
||||
break;
|
||||
case NotFoundHttpException::class:
|
||||
$response->data = [
|
||||
'path' => '/' . $request->getPathInfo(),
|
||||
'errorType' => 'NOT_FOUND',
|
||||
'error' => 'NOT_FOUND',
|
||||
'errorMessage' => 'The server has not found anything matching the request URI',
|
||||
'developerMessage' => 'The server has not found anything matching the request URI',
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user