当前位置: 首页>>代码示例>>PHP>>正文


PHP HttpException::getCode方法代码示例

本文整理汇总了PHP中Symfony\Component\HttpKernel\Exception\HttpException::getCode方法的典型用法代码示例。如果您正苦于以下问题:PHP HttpException::getCode方法的具体用法?PHP HttpException::getCode怎么用?PHP HttpException::getCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Symfony\Component\HttpKernel\Exception\HttpException的用法示例。


在下文中一共展示了HttpException::getCode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: render

 /**
  * 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);
 }
开发者ID:sourcestream,项目名称:highcore-api,代码行数:39,代码来源:Handler.php

示例2: render

 /**
  * 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);
 }
开发者ID:bakgat,项目名称:notos-plus,代码行数:20,代码来源:Handler.php

示例3: handle

 /**
  * 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);
 }
开发者ID:AnujRNair,项目名称:anujnair.com-symfony,代码行数:28,代码来源:ExceptionHandler.php


注:本文中的Symfony\Component\HttpKernel\Exception\HttpException::getCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。