public function renderAsRestAPI($request, \Exception $e) { if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof ModelNotFoundException) { $e = new NotFoundHttpException($e->getMessage(), $e); } elseif ($e instanceof AuthorizationException) { $e = new HttpException(403, $e->getMessage()); } elseif ($e instanceof ValidationException && $e->getResponse()) { return $e->getResponse(); } $fe = FlattenException::create($e); $data = env("APP_DEBUG", false) ? $fe->toArray() : ["message" => "whoops, something wrong."]; return JsonResponse::create($data, 500); }
public function run() { if (($exception = Yii::$app->getErrorHandler()->exception) === null) { // action has been invoked not from error handler, but by direct route, so we display '404 Not Found' $exception = new HttpException(404, Yii::t('yii', 'Page not found.')); } if ($exception instanceof HttpException) { $code = $exception->statusCode; } else { $code = $exception->getCode(); } if ($exception instanceof Exception) { $name = $exception->getName(); } else { $name = $this->defaultName ?: Yii::t('yii', 'Error'); } if ($code) { $name .= " (#{$code})"; } if ($exception instanceof UserException) { $message = $exception->getMessage(); } else { $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.'); } if (Yii::$app->getRequest()->getIsAjax()) { return "{$name}: {$message}"; } else { return $this->controller->render($this->view ?: $this->id, ['name' => $name, 'message' => $message, 'exception' => $exception]); } }
/** * Converts an exception into an array. * @param \Exception $exception the exception being converted * @return array the array representation of the exception. */ protected function convertExceptionToArray($exception) { if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) { $exception = new HttpException(500, 'There was an error at the server.'); } $array = ['code' => $exception->getCode(), 'message' => $exception->getMessage()]; if ($exception instanceof LBUserException) { $array['data'] = $exception->getErrors(); } return $array; }