protected function convertHttpExceptionToJsonResponse(HttpException $exc) { $data = json_decode($exc->getMessage(), true); if (!is_array($data)) { $data = ['_message' => $exc->getMessage()]; } return new JsonResponse($data, $exc->getStatusCode()); }
/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { if ($e instanceof NotosException) { $e = new HttpException($e->getStatus(), $e->getMessage(), $e->getPrevious(), [], $e->getCode()); $response = new JsonResponse($e->getMessage(), $e->getStatusCode(), []); $response->exception = $e; return $response; } if ($e instanceof ModelNotFoundException) { $e = new NotFoundHttpException($e->getMessage(), $e); } return parent::render($request, $e); }
/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { /* * Notification on TokenMismatchException */ if ($e instanceof TokenMismatchException) { Notification::error(trans('global.Security token expired. Please, repeat your request.')); return redirect()->back()->withInput(); } 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(); } if ($this->isHttpException($e)) { return $this->toIlluminateResponse($this->renderHttpException($e), $e); } else { // Custom error 500 view on production if (app()->environment() == 'production') { return response()->view('errors.500', [], 500); } return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); } }
/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { // If this exception is listed for code forwarding if ($this->shouldConvertToHttpException($e)) { $e = new HttpException($e->getCode(), $e->getMessage(), $e); } // If the request wants JSON (AJAX doesn't always want JSON) if ($request->wantsJson()) { // Define the response $response = ['errors' => 'Sorry, something went wrong.']; // If the app is in debug mode if (config('app.debug')) { // Add the exception class name, message and stack trace to response $response['exception'] = get_class($e); // Reflection might be better here $response['message'] = $e->getMessage(); $response['trace'] = $e->getTrace(); } // Default response of 400 $status = 400; // If this exception is an instance of HttpException if ($this->isHttpException($e)) { // Grab the HTTP status code from the Exception $status = $e->getStatusCode(); } // Return a JSON response with the response array and status code unset($response['trace']); return response()->json($response, $status); } // Default to the parent class' implementation of handler return parent::render($request, $e); }
/** * Render an exception into an HTTP response. Should conform to RFC "Problem Details for HTTP APIs": * https://tools.ietf.org/html/rfc7807 * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { if ($e instanceof HttpResponseException) { // return $e->getResponse(); $title = 'An exception happened when preparing the HTTP response'; } elseif ($e instanceof ModelNotFoundException) { $e = new NotFoundHttpException($e->getMessage(), $e); $statusCode = HttpStatus::NotFound; $title = 'The entity does not exist'; } elseif ($e instanceof AuthorizationException) { $e = new HttpException(HttpStatus::Forbidden, $e->getMessage()); $title = 'You are not authorized to access this information'; } elseif ($e instanceof ValidationException && $e->getResponse()) { $errors = json_decode($e->getResponse()->content()); $statusCode = HttpStatus::UnprocessableEntity; $title = 'Data validation error'; } $response = []; /* $response = [ 'type' => '', 'title' => 'Something went wrong', 'detail' => $e->getMessage() ?: 'No more information is known', 'instance' => '' ]; */ if (isset($title)) { $response['title'] = $title; } if ($e->getMessage()) { $response['detail'] = $e->getMessage(); } if (isset($errors)) { $response['invalid-fields'] = $errors; } if (env('APP_DEBUG', false)) { $response['debug'] = ['exception' => get_class($e), 'trace' => $e->getTrace(), 'response' => method_exists($e, 'getResponse') ? $e->getResponse() : '']; } if (!isset($statusCode)) { if (method_exists($e, 'getStatusCode')) { $statusCode = $e->getStatusCode(); } else { $statusCode = HttpStatus::InternalServerError; } } return response()->json($response, $statusCode); // return parent::render($request, $e); }
/** * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpException $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpException $e) { if (view()->exists('errors.' . $e->getStatusCode())) { return response()->view('errors.' . $e->getStatusCode(), ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'debug' => config('app.debug')], $e->getStatusCode()); } else { return (new SymfonyDisplayer(config('app.debug')))->createResponse($e); } }
/** * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpException $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpException $e) { $status = $e->getStatusCode(); if (view()->exists("errors.{$status}")) { return response()->view("errors.{$status}", ['message' => $e->getMessage()], $status); } return parent::renderHttpException($e); }
/** * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpException $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpException $e) { // load base JS JavaScript::put(['base_url' => url('/'), 'site_name' => config('settings.app_name_' . config('app.locale'))]); $seo_meta = ['page_title' => 'Erreur ' . $e->getStatusCode(), 'meta_desc' => $e->getMessage(), 'meta_keywords' => '']; $data = ['code' => $e->getStatusCode(), 'seo_meta' => $seo_meta, 'css' => elixir('css/app.error.css')]; return response()->view('templates.common.errors.errors', $data); }
/** * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpException $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpException $e) { $status = $e->getStatusCode(); if (!config('app.debug') && view()->exists("streams::errors.{$status}")) { return response()->view("streams::errors.{$status}", ['message' => $e->getMessage()], $status); } else { return (new SymfonyDisplayer(config('app.debug')))->handle($e); } }
/** * Render the given HttpException. * * @param \Symfony\Component\HttpKernel\Exception\HttpException $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpException $e) { $status = $e->getStatusCode(); if (view()->exists("csm::errors.{$status}")) { return response()->view("errors.{$status}", ['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'code' => $status, 'bodyId' => 'error.' . $status], $status); } else { return (new SymfonyDisplayer(config('app.debug')))->createResponse($e); } }
/** * Функция для отображения сообщений на страницах ошибок (404, 500 etc.) * @param HttpException $e * @return \Illuminate\Http\Response */ protected function renderHttpException(HttpException $e) { $status = $e->getStatusCode(); if (view()->exists("errors.{$status}")) { return response()->view("errors.{$status}", ['message' => $e->getMessage(), 'status' => $status, 'headers' => $e->getHeaders()], $status); } else { return $status; } }
/** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { if ($this->isUnauthorizedException($e)) { $e = new HttpException(403, $e->getMessage()); } if ($this->isHttpException($e)) { return $this->toIlluminateResponse($this->renderHttpException($e), $e); } else { return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); } }
/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { /* TODO: criar página de 404 generica */ if ($exception instanceof ModelNotFoundException) { $exception = new HttpException($exception->getMessage(), $exception); } if ($exception instanceof TokenMismatchException) { Flash::error('Sua sessão expirou.\\n Atualize a página usando o Ctrl+F5 e tente novamente.'); return redirect()->back()->withInput($request->except('password')); } #if ($exception instanceof AdmixException) { # Flash::error($exception->getMessage()); # return redirect() # ->back(); #} return parent::render($request, $exception); }
/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($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); $handler = new SymfonyExceptionHandler(env('APP_DEBUG', false)); $decorated = $this->decorate($handler->getContent($fe), $handler->getStylesheet($fe)); $response = new Response($decorated, $fe->getStatusCode(), $fe->getHeaders()); $response->exception = $e; return $response; }
/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { if ($e instanceof FormValidationException) { if ($request->wantsJson()) { return \Response::json($e->getErrors(), 422); } \Notification::error("Something wasn't right, please check the form for errors", $e->getErrors()); return redirect()->back()->withInput(); } if ($e instanceof ValidationException) { if ($request->wantsJson()) { return \Response::json($e->getMessage(), 422); } \Notification::error($e->getMessage()); return redirect()->back()->withInput(); } if ($e instanceof NotImplementedException) { \Notification::error("NotImplementedException: " . $e->getMessage()); \Log::warning($e); return redirect()->back()->withInput(); } if ($e instanceof AuthenticationException) { if ($request->wantsJson()) { return \Response::json(['error' => $e->getMessage()], 403); } $userString = \Auth::guest() ? "A guest" : \Auth::user()->name; \Log::warning($userString . " tried to access something they weren't supposed to."); return \Response::view('errors.403', [], 403); } if ($e instanceof ModelNotFoundException) { $e = new HttpException(404, $e->getMessage()); } if (config('app.debug') && $this->shouldReport($e) && !$request->wantsJson()) { return $this->renderExceptionWithWhoops($e); } if ($request->wantsJson()) { if ($this->isHttpException($e)) { return \Response::json(['error' => $e->getMessage()], $e->getStatusCode()); } } return parent::render($request, $e); }
/** * Handle an exception and display the correct error message. Firstly check * for a errorXXX.format.twig file, otherwise default to error.html.twig * @param GetResponseEvent $event * @param HttpException $exception * @param string $format */ public function handle(GetResponseEvent $event, $exception, $format = 'html') { $message = $exception->getMessage(); if ($exception instanceof HttpException) { $statusCode = $exception->getStatusCode(); } elseif ($exception instanceof AccessDeniedException) { $statusCode = $exception->getCode(); } else { $statusCode = 500; } $error = FlattenException::create($exception); $baseDirectory = 'AnujNairBundle:Error:'; try { $renderedView = $this->template->render("{$baseDirectory}error{$statusCode}.{$format}.twig", ['statusCode' => $statusCode, 'message' => $message, 'error' => $error]); } catch (\Exception $e) { $renderedView = $this->template->render("{$baseDirectory}error.html.twig", ['statusCode' => $statusCode, 'message' => $message, 'error' => $error]); } $response = Response::create($renderedView, $statusCode); $event->stopPropagation(); $event->setResponse($response); }
/** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($request, Exception $e) { $fullurl = $request->fullUrl(); if (isset(Auth::User()->name)) { $username = Auth::User()->name; } else { $username = '******'; } $ip_address = 'Unspecified IP Address'; if (!empty($request->ip())) { $ip_address = $request->ip(); } if ($e instanceof ModelNotFoundException) { $e = new NotFoundHttpException($e->getMessage(), $e); } if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof ModelNotFoundException) { $e = new NotFoundHttpException($e->getMessage(), $e); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof AuthorizationException) { $e = new HttpException(403, $e->getMessage()); } elseif ($e instanceof ValidationException && $e->getResponse()) { return $e->getResponse(); } $e->debug = TRUE; if ($this->isHttpException($e)) { return $this->toIlluminateResponse($this->renderHttpException($e), $e); } else { Mail::send('emails.error', ['error' => $this->convertExceptionToResponse($e)], function ($message) use($fullurl, $username, $ip_address) { $message->to('*****@*****.**'); $message->subject('Polanco Error @' . $fullurl . ' by: ' . $username . ' from: ' . $ip_address); $message->from('*****@*****.**'); }); return view('errors.default'); } }
/** * Create a http response. * * @param HttpException $exception The exception to create a response for. * * @return JsonResponse */ private function createHttpExceptionResponse(HttpException $exception) { return new JsonResponse(['status' => 'ERROR', 'message' => $exception->getMessage()], $exception->getStatusCode(), $exception->getHeaders()); }
/** * Render an exception into a response. * * @param \Illuminate\Http\Request $request * @param \Exception $e * @return \Illuminate\Http\Response */ public function render($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(); } if ($this->isHttpException($e)) { return $this->toIlluminateResponse($this->renderHttpException($e), $e); } else { return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e); } }
protected function renderException($request, $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 $this->response()->error($data); }
/** * @param \Exception $exception * * @return \Exception */ protected function prepareException(Exception $exception) { if ($exception instanceof ModelNotFoundException) { $exception = new NotFoundHttpException($exception->getMessage(), $exception); } elseif ($exception instanceof AuthorizationException) { $exception = new HttpException(403, $exception->getMessage()); } return $exception; }
/** * @param HttpException $e * @return ErrorInterface */ protected function getHttpError(HttpException $e) { return new Error(null, null, $e->getStatusCode(), null, null, $e->getMessage() ?: null); }
/** * @param HttpException $exception * @return array */ public function transform(HttpException $exception) { return ['status_code' => $exception->getStatusCode(), 'message' => $exception->getMessage()]; }
private function handleHttpException(HttpException $e, $code) { $message = array('status' => $e->getStatusCode(), 'code' => $code, 'message' => $e->getMessage()); return $this->app->json($message, $e->getStatusCode(), $e->getHeaders()); }
public function onHttpException(HttpException $e) { $errorMessage = ['message' => $e->getMessage(), 'error' => ['errorCode' => $e->getStatusCode()] + $this->dumpExceptionsRecursively($e)]; return new JsonResponse($errorMessage, $e->getStatusCode(), $e->getHeaders()); }