Creates ApiException from any other exception.

@param \Exception $e

@return static
556487
public static function makeFromException($e)
    {
        if ($e instanceof self) {
            return $e;
        }

        return new static($e->getMessage(), $e->getCode(), $e);
    }
267240
public function exceptionHandler($e) {
        if (method_exists($e, 'getType')) {
            $this->addError($e->getMessage(), $e->getFile(), $e->getLine(), $e->getType());
        }
        else {
            $this->addError($e->getMessage(), $e->getFile(), $e->getLine(), gettype($e));
        }
    }
867985
protected function prepareException(Exception $e)
    {
        $e = parent::prepareException($e);

        if ($e instanceof MethodNotAllowedHttpException) {
            $e = new HttpException(405, 'Method not allowed', $e->getPrevious(), $e->getHeaders(), $e->getCode());
        }

        return $e;
    }
560597
private function translateExceptionCode($e)
    {
        if (! in_array($e->getCode(), [0, -1, null, ''])) {
            return $e->getCode();
        }

        if (method_exists($e, 'getStatusCode')) {
            if (! in_array($e->getStatusCode(), [0, -1, null, ''])) {
                return $e->getStatusCode();
            }

            if (($statusCode = $this->getStatusCode()) != StatusCode::OK) {
                return $statusCode;
            }
        }

        if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException
            or $e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
        ) {
            return StatusCode::NOT_FOUND;
        }

        return StatusCode::BAD_REQUEST;
    }
1250093
public function andThrow($e)
    {
        if (! $e instanceof \Exception) {
            if (is_scalar($e) && class_exists($e) && is_a($e, '\Exception', true)) {
                $e = new $e("Generic message");
            } else {
                throw new \LogicException("Can only create exceptions on-the-fly with a scalar message");
            }
        }

        $this->andReturn(function() use ($e){
            if (! $e instanceof \Exception) {
                throw new \Exception($e);
            } else {
                throw $e;
            }
        });
        
        return $this;
    }
986584
protected function createErrorPage(\Throwable $e){
        $response   = $this->factory->createResponse(
            $e instanceof HttpStatus ? $e->getStatusCode() : 500
        );

        if($e instanceof \Fratily\Http\Message\Status\MethodNotAllowed){
            $response   = $response->withHeader(
                "Allow",
                implode(", ", $e->getAllowed())
            );
        }

        return $response;
    }
835076
protected function convertExceptionToServiceException(Exception $e): ServiceException
    {
        $exception = get_class($e);
        $conversion = $this->container['config']->get('exception.conversion', []);

        if (isset($conversion[$exception])) {
            return new $conversion[$exception]($e->getMessage(), $e->getCode(), $e);
        } /*else if (in_array($exception, $conversion, true)) {
            return new ServiceException($e->getMessage(), 400, $e);
        }*/ else {
            $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 400;

            return new ServiceException($e->getMessage(), $statusCode, $e);
        }
    }
418490
public static function createFromException(\Exception $e, $http_status = Response::STATUS_ERROR)
    {
        return new Error(
            $e->getMessage(),
            $http_status,
            $e->getFile(),
            $e->getLine(),
            $e
        );
    }
1018642
private function convertToGoogleException(\Exception $ex)
    {
        switch ($ex->getCode()) {
            case 400:
                $exception = Exception\BadRequestException::class;
                break;

            case 404:
                $exception = Exception\NotFoundException::class;
                break;

            case 409:
                $exception = Exception\ConflictException::class;
                break;

            case 412:
                $exception = Exception\FailedPreconditionException::class;
                break;

            case 500:
                $exception = Exception\ServerException::class;
                break;

            case 504:
                $exception = Exception\DeadlineExceededException::class;
                break;

            default:
                $exception = Exception\ServiceException::class;
                break;
        }

        return new $exception($this->getExceptionMessage($ex), $ex->getCode(), $ex);
    }
1167634
public static function exceptionHandler(\Exception $e)
    {
//echo __METHOD__;
        $error = Error::createFromException($e, Response::STATUS_ERROR);
        $status = method_exists($e, 'getStatus') ? $e->getStatus() : Response::STATUS_ERROR;
        Container::get('response')
            ->setStatus($status);
        Container::get('controller')
            ->addError($error)
            ->serve();
    }
1050441
public static function wrapException(RequestInterface $request, \Exception $e)
    {
        return $e instanceof RequestException
            ? $e
            : new RequestException($e->getMessage(), $request, null, $e);
    }
397084
protected function convertExceptionToResponse(Exception $e)
    {
        $e = FlattenException::create($e);

        $handler = new SymfonyExceptionHandler(config('app.debug'));

        $decorated = $this->decorate($handler->getContent($e), $handler->getStylesheet($e));

        return SymfonyResponse::create($decorated, $e->getStatusCode(), $e->getHeaders());
    }
930618
protected function convertExceptionToResponse(Exception $e, Request $request)
    {
        $debug = Config::get('app.debug');

        //
        $e = FlattenException::create($e);

        $handler = new SymfonyExceptionHandler($debug);

        return SymfonyResponse::create($handler->getHtml($e), $e->getStatusCode(), $e->getHeaders());
    }
358903
public static function fromError(\Error $e)
    {
        return new self($e->getMessage(), $e->getCode(), 1, $e->getFile(), $e->getLine(), $e);
    }
202315
private function handleExternalError(\Exception $e) {
        $reflection = new \ReflectionClass($e);
        echo '<header>';
            echo '<h1>Exception: ' . $reflection->getName() . '</h1>';
            echo '<h2>Message: ' . $e->getMessage() . '</h2>';
            echo '<h3>' . $e->getFile() . ':' . $e->getLine() . '</h3>';
        echo '</header>';
        if ( method_exists($e, 'getContext') ) {
            echo 'Debug:';
            !d( $e->getContext() );
        }
    }
947582
protected function convertExceptionToArray(Exception $e)
    {
        return $this->container->make('config')->get('app.debug') ? [
            'message' => $e->getMessage(),
            'exception' => get_class($e),
            'file' => $e->getFile(),
            'line' => $e->getLine(),
            'trace' => collect($e->getTrace())->map(function ($trace) {
                return Arr::except($trace, ['args']);
            })->all(),
        ] : [
            'message' => $this->isServiceException($e) ? $e->getMessage() : 'Server Error',
        ];
    }
875600
protected function convertDatabaseException(\Exception $e)
	{
		if($e instanceof DatabaseException)
		{
			return $e;
		}
		
		// Check for PDO exception in order to determine FK violation:
		$cause = $e;
		
		if(!$cause instanceof \PDOException)
		{
			while(NULL !== ($cause = $cause->getPrevious()))
			{
				if($cause instanceof \PDOException)
				{
					break;
				}
			}
		}
		
		if($cause instanceof \PDOException)
		{
			switch($this->driverName)
			{
				case DB::DRIVER_MYSQL:
					return $this->convertMySqlException($cause, $e);
				case DB::DRIVER_POSTGRESQL:
					return $this->convertPostgresException($cause, $e);
				case DB::DRIVER_SQLITE:
					return $this->convertSqliteException($cause, $e);
			}
		}
		
		return new DatabaseException($e->getMessage(), 0, $e);
	}