get JSON error

@param  Exception $exception
@return string
341829
protected function getJsonMessage($exception)
    {
        $error = array(
            'message' => 'Application Error',
        );
        
        if ($this->options['display_details']) {
            $error['exception'] = array();
            
            do {
                $error['exception'][] = array(
                    'type' => get_class($exception),
                    'code' => $exception->getCode(),
                    'message' => $exception->getMessage(),
                    'file' => $exception->getFile(),
                    'line' => $exception->getLine(),
                    'trace' => explode("\n", $exception->getTraceAsString()),
                );
            } while ($exception = $exception->getPrevious());
        }
        
        return json_encode($error, JSON_PRETTY_PRINT);
    }
1135885
protected function renderException($exception)
    {
        $contentType = static::getContentType();
        $dataType = explode('/', $contentType);
        $dataType = isset($dataType[1]) ? $dataType[1] : 'text';

        switch ($dataType) {
            case 'json':
                $output = $this->getJsonMessage($exception);
                break;
                
            case 'xml':
                $output = $this->getXmlMessage($exception);
                break;
            
            case 'html':
                $output = $this->getHtmlMessage($exception);
                break;
                
            case 'text':
            default:
                $output = $this->getTextMessage($exception);
        }
        
        echo $output;
    }
360110
protected function _code(Exception $exception)
    {
        $code = 500;

        $exception = $this->_unwrap($exception);
        $errorCode = $exception->getCode();
        if ($errorCode >= 400 && $errorCode < 600) {
            $code = $errorCode;
        }

        return $code;
    }
687659
private function _handleException(Exception $exception)
    {
        $status = $exception->getCode();
        $statusText = \Slim\Http\Response::getMessageForCode($status);

        if ($statusText === null) {
            $status = 500;
            $statusText = 'Internal Server Error';
        }

        //Get app environment variables
        $env = $this->app->environment;

        $this->app->response->setStatus($status);

        if (isset($env['auth.JSONerrors']) && $env['auth.JSONerrors'] == true) {
            $this->app->response->headers->set('Content-Type', 'application/json');
            $this->app->response->setBody(
                json_encode(
                    [
                        'status' => ($status < 500) ? 'fail' : 'error',
                        'data' => [
                            'status' => $status,
                            'statusText' => preg_replace('/^[0-9]+ (.*)$/', '$1', $statusText),
                            'description' => $exception->getMessage(),
                        ],
                    ]
                )
            );
        } else {
            $this->app->response->setBody('<h1>'.$statusText.'</h1><h4>'.$exception->getMessage().'</h4>');
        }

        $this->app->response()->finalize();

        return $this->app->response();
    }
756827
protected function renderJsonErrorMessage(Exception $exception)
    {
        $error = [
            'message' => 'Slim Application Error',
        ];

        if ($this->displayErrorDetails) {
            $error['exception'] = [];

            do {
                $error['exception'][] = [
                    'type' => get_class($exception),
                    'code' => $exception->getCode(),
                    'message' => $exception->getMessage(),
                    'file' => $exception->getFile(),
                    'line' => $exception->getLine(),
                    'trace' => explode("\n", $exception->getTraceAsString()),
                ];
            } while ($exception = $exception->getPrevious());
        }

        return json_encode($error, JSON_PRETTY_PRINT);
    }
1355730
protected function buildLogMessage(\Exception $exception)
    {
        $message = $exception->getMessage() . "({$exception->getCode()})";

        if ($exception instanceof PhprestException && $exception->getDetails()) {
            $message .= ' Details :: ' . json_encode($exception->getDetails());
        }

        $message .= ' Stack trace :: ' . $exception->getTraceAsString();

        return $message;
    }
10399
private function normalizeException(\Exception $exception) {
		$data = [
			'class'   => get_class($exception),
			'message' => $exception->getMessage(),
			'code'    => $exception->getCode(),
			'file'    => $exception->getFile() . ':' . $exception->getLine(),
		];
		$trace = $exception->getTraceAsString();
		$data['trace'] = $trace;

		$previous = $exception->getPrevious();
		if ($previous) {
			$data['previous'] = $this->normalizeException($previous);
		}

		return $data;
	}
336011
final public static function getBaseException(\Exception $exception)
    {
        while ($exception->getPrevious() !== null) {
            $exception = $exception->getPrevious();
        }

        return $exception;
    }
449373
protected function extractExceptionData(Exception $exception)
    {
        $data = [
            'exception' => get_class($exception),
            'message'   => $exception->getMessage(),
            'code'      => $exception->getCode(),
            'file'      => $exception->getFile(),
            'line'      => $exception->getLine(),
            'trace'     => []
        ];

        foreach ($exception->getTrace() as $item) {
            if (isset($item['args']) && is_array($item['args'])) {
                $item['args'] = $this->cleanTraceArgs($item['args']);
            }
            $data['trace'][] = $item;
        }

        return $data;
    }
1295168
protected function marshallResponseData(\Exception $exception)
    {
        $translator = resolve('translator');

        if ($exception instanceof ValidationException) {
            $this->collectValidationErrors($exception);
            
            return;
        }

        $namespace   = 'robrogers3/laravel-jsonaware-exception-handler';

        $className = get_class($exception);

        $info = collect($translator->get("$namespace::exceptionmessages.exceptions.$className"));
        
        $this->statusCode = $info->get('code', 500);

        $this->message = $info->get('message', 'Server Error: we cannot handle your request.');

        if ($exception instanceof MessagingException) {
            $this->setExceptionMessage($exception);
        }
    }
172355
protected function getStatusCode(Exception $exception)
    {
        if ($exception instanceof ValidationException) {
            return $exception->status;
        }

        return $this->getExceptionStatusCode($exception);
    }
137435
public static function exception_handler(\Exception $exception) {
		global $CFG, $USR;
		$errcode = $exception->getCode();
		$errmsg = $exception->getMessage();
		$errcodelabel = ($exception instanceof \pdyn\base\Exception)
			? $exception->get_string_from_code($errcode)
			: static::getinternalerrorlabel($errcode);
		if (\pdyn\base\Utils::is_cli_env() === true) {
			echo 'APP ERROR: '.$errcodelabel.': '.$errmsg."\n";
		} else {
			// Atlas exceptions' error codes are HTTP status codes, so send one.
			if (($exception instanceof \pdyn\base\Exception) && !empty($errcode) && !headers_sent()) {
				\pdyn\httputils\Utils::send_status_code($errcode, '', false);
			} else {
				\pdyn\httputils\Utils::send_status_code(500, '', false);
			}
			$LOG = new \pdyn\log\Logger($CFG->log_general);
			$LOG->error($errcodelabel.': '.$exception);
			if (isset($_GET['ajax'])) {
				header('Content-type: application/json');
				echo json_encode(['result' => 'fail', 'msg' => $errcodelabel.': '.$errmsg]);
			} else {
				$debugmode = (!empty($CFG) && (bool)$CFG->get('core', 'debugmode', false) === true) ? true : false;
				$isadmin = (!empty($USR) && $USR->is_admin === true) ? true : false;
				$backtrace = ($isadmin === true || $debugmode === true)	? static::format_backtrace($exception->getTrace()) : null;
				static::write_error_html($errcodelabel, $errmsg, $errcode, $backtrace);
			}
		}
		die();
	}
137327
public static function exception(ApiException $exception)
    {
        $returnResponse = \Response::make($exception->jsonSerialize());

        $returnResponse->setStatusCode($exception->getStatusCode());

        return $returnResponse;
    }
1146417
protected function renderJsonErrorMessage(HttpException $exception)
    {
        $error = [
            'message' => $exception->getMessage(),
            'status' => $exception->getStatusCode(),
        ];

        return json_encode($error, JSON_PRETTY_PRINT);
    }
233358
private function getJsonErrorName($error)
    {
        $matches = array_keys(get_defined_constants(), $error, true);
        $prefix = 'JSON_ERROR_';
        $prefixLength = strlen($prefix);
        $name = 'UNKNOWN_ERROR';

        foreach ($matches as $match) {
            if (is_string($match) && strncmp($match, $prefix, $prefixLength) === 0) {
                $name = $match;
                break;
            }
        }

        return $name;
    }
926449
protected function getStatusCode(Exception $exception): int
    {
        if ($exception instanceof ApiException) {
            return $exception->getStatusCode();
        }

        if ($exception instanceof HttpExceptionInterface && method_exists($exception, 'getStatusCode')) {
            return $exception->getStatusCode();
        }

        return 500;
    }
1193353
private function getExceptionData($exception)
    {
        $data = [];

        $data['enviroment'] = env('APP_ENV');
        $data['host'] = Request::server('SERVER_NAME');
        $data['method'] = Request::method();
        $data['fullUrl'] = Request::fullUrl();
        $data['exception'] = $exception->getMessage();
        $data['error'] = $exception->getTraceAsString();
        $data['line'] = $exception->getLine();
        $data['file'] = $exception->getFile();
        $data['class'] = get_class($exception);
        $data['storage'] = [
            'SERVER' => Request::server(),
            'GET' => Request::query(),
            'POST' => $_POST,
            'FILE' => Request::file(),
            'OLD' => Request::hasSession() ? Request::old() : [],
            'COOKIE' => Request::cookie(),
            'SESSION' => Request::hasSession() ? Session::all() : [],
            'HEADERS' => Request::header(),
        ];

        $data['storage'] = array_filter($data['storage']);

        $count = $this->config['count'];
        $lines = file($data['file']);
        $data['exegutor'] = [];

        for ($i = -1 * abs($count); $i <= abs($count); $i++) {
            $data['exegutor'][] = $this->getLineInfo($lines, $data['line'], $i);
        }
        $data['exegutor'] = array_filter($data['exegutor']);

        // to make symfony exception more readable
        if ($data['class'] == 'Symfony\Component\Debug\Exception\FatalErrorException') {
            preg_match("~^(.+)' in ~", $data['exception'], $matches);
            if (isset($matches[1])) {
                $data['exception'] = $matches[1];
            }
        }

        return $data;
    }