Inheritance: implements Dingo\Api\Contract\Debug\ExceptionHandler, implements Illuminate\Contracts\Debug\ExceptionHandler
Example #1
0
 /**
  * Handle an incoming request.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     try {
         if ($this->validator->validateRequest($request)) {
             $request = $this->app->make('Dingo\\Api\\Contract\\Http\\Request')->createFromIlluminate($request);
             return $this->sendRequestThroughRouter($request);
         }
     } catch (Exception $exception) {
         return $this->exception->handle($exception);
     }
     return $next($request);
 }
Example #2
0
 /**
  * Handle an incoming request.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     try {
         if ($this->validator->validateRequest($request)) {
             $this->app->singleton('Illuminate\\Contracts\\Debug\\ExceptionHandler', function ($app) {
                 return $app['Dingo\\Api\\Exception\\Handler'];
             });
             $request = $this->app->make('Dingo\\Api\\Contract\\Http\\Request')->createFromIlluminate($request);
             return $this->sendRequestThroughRouter($request);
         }
     } catch (Exception $exception) {
         return $this->exception->handle($exception);
     }
     return $next($request);
 }
Example #3
0
File: Router.php Project: jrean/api
 /**
  * Prepare a response by transforming and formatting it correctly.
  *
  * @param mixed                   $response
  * @param \Dingo\Api\Http\Request $request
  * @param string                  $format
  * @param bool                    $raw
  *
  * @return \Dingo\Api\Http\Response
  */
 protected function prepareResponse($response, Request $request, $format)
 {
     if ($response instanceof IlluminateResponse) {
         $response = Response::makeFromExisting($response);
     }
     if ($response instanceof Response) {
         // If we try and get a formatter that does not exist we'll let the exception
         // handler deal with it. At worst we'll get a generic JSON response that
         // a consumer can hopefully deal with. Ideally they won't be using
         // an unsupported format.
         try {
             $response->getFormatter($format)->setResponse($response)->setRequest($request);
         } catch (NotAcceptableHttpException $exception) {
             return $this->exception->handle($exception);
         }
         $response = $response->morph($format);
     }
     if ($response->isSuccessful() && $this->requestIsConditional()) {
         if (!$response->headers->has('ETag')) {
             $response->setEtag(md5($response->getContent()));
         }
         $response->isNotModified($request);
     }
     return $response;
 }
Example #4
0
 /**
  * Handle an exception thrown during dispatching of an API request.
  *
  * @param \Exception $exception
  *
  * @throws \Exception
  *
  * @return \Dingo\Api\Http\Response
  */
 public function handle(Exception $exception)
 {
     if ($this->handler->willHandle($exception)) {
         $response = $this->handler->handle($exception);
         return Response::makeFromExisting($response);
     } elseif (!$exception instanceof HttpExceptionInterface) {
         throw $exception;
     }
     if (!($message = $exception->getMessage())) {
         $message = sprintf('%d %s', $exception->getStatusCode(), Response::$statusTexts[$exception->getStatusCode()]);
     }
     $response = ['message' => $message, 'status_code' => $exception->getStatusCode()];
     if ($exception instanceof ResourceException && $exception->hasErrors()) {
         $response['errors'] = $exception->getErrors();
     }
     if ($code = $exception->getCode()) {
         $response['code'] = $code;
     }
     if ($this->debug) {
         $response['debug'] = ['line' => $exception->getLine(), 'file' => $exception->getFile(), 'class' => get_class($exception), 'trace' => explode("\n", $exception->getTraceAsString())];
     }
     return new Response($response, $exception->getStatusCode(), $exception->getHeaders());
 }