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; } } }