Parse response data by specified type

@param string $data
@param null $exception A variable to store exception when parsing error
@return mixed
974963
protected function parseResponse($data, &$exception)
    {
        switch ($this->dataType) {
            case 'json' :
            case 'jsonObject' :
                $result = json_decode($data, $this->dataType === 'json');
                if (null === $result && json_last_error() != JSON_ERROR_NONE) {
                    $exception = new \ErrorException('JSON parsing error, the data is ' . $data, json_last_error());
                }
                break;

            case 'xml' :
            case 'serialize' :
                $methods = array(
                    'xml' => 'simplexml_load_string',
                    'serialize' => 'unserialize',
                );
                $result = @$methods[$this->dataType]($data);
                if (false === $result && $e = error_get_last()) {
                    $exception = new \ErrorException($e['message'], $e['type'], 0, $e['file'], $e['line']);
                }
                break;

            case 'query' :
                // Parse $data(string) and assign the result to $result(array)
                parse_str($data, $result);
                break;

            case 'text':
            default :
                $result = $data;
                break;
        }
        return $result;
    }
414256
protected function parse($data = null, $filename = null)
    {
        if (!$data) {
            $error = error_get_last();

            // Parse functions may return NULL but set no error if the string contains no parsable data
            if (!is_array($error)) {
                $error["message"] = "No parsable content in data.";
            }

            $error["file"] = $filename;

            // if string contains no parsable data, no error is set, resulting in any previous error
            // persisting in error_get_last(). in php 7 this can be addressed with error_clear_last()
            if (function_exists("error_clear_last")) {
                error_clear_last();
            }

            throw new ParseException($error);
        }

        return $this->expandDottedKey($data);
    }
779541
public function execute($data = null)
    {
        if (class_exists(DebuggerHelper::class)) {
            DebuggerHelper::disableConsole();
        }

        try {
            $result = $this->next->execute($data);

            if ($result instanceof AbstractView) {
                $result = $result->getHandledData();
            }

            $message = $this->getMessage();

            return new JsonBuffer($message, $result);
        } catch (\Exception $e) {
            return $this->handleException($e);
        } catch (\Throwable $t) {
            return $this->handleException(new \ErrorException(
                $t->getMessage(),
                $t->getCode(),
                E_ERROR,
                $t->getFile(),
                $t->getLine(),
                $t
            ));
        }
    }
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;
    }
1266749
public function parseReadData($data)
	{
		$results = array();

		foreach ($data as $row) {
			if (is_array($row)) {
				$key = $row['key'];
				$value = $row['value'];
			} elseif (is_object($row)) {
				$key = $row->key;
				$value = $row->value;
			} else {
				$msg = 'Expected array or object, got '.gettype($row);
				throw new \UnexpectedValueException($msg);
			}

			SettingsUtilities::set($results, $key, $value);
		}

		return $results;
	}
1095739
private function callOnException(\Exception $exception)
    {
        if (is_null($this->onException)) {
            throw  $exception;
        }

        $closure = $this->onException;

        $message = $closure($this, $exception);

        return $message;
    }
96033
public function parse($data)
    {
        if ($data === null || !is_string($data)) {
            return array();
        }

        if (substr($data, 0, 1) !== '{' &&
            substr($data, 0, 1) !== '[') {
            return array();
        }

        return (array) json_decode($data, true);
    }
120213
private function error(HttpAdapterInterface $httpAdapter, HttpAdapterException $exception)
    {
        $request = $this->getTimer()->stop($exception->getRequest());

        $this->logger->error(
            sprintf(
                'Unable to send "%s %s".',
                $exception->getRequest()->getMethod(),
                (string) $exception->getRequest()->getUri()
            ),
            [
                'adapter'   => $httpAdapter->getName(),
                'exception' => $this->getFormatter()->formatException($exception),
                'request'   => $this->getFormatter()->formatRequest($request),
                'response'  => $exception->hasResponse()
                    ? $this->getFormatter()->formatResponse($exception->getResponse())
                    : null,
            ]
        );

        return $request;
    }
570962
protected function genericResponse(Exception $exception)
    {
        $replacements = $this->prepareReplacements($exception);

        $response = $this->newResponseArray();

        array_walk_recursive($response, function (&$value, $key) use ($exception, $replacements) {
            if (starts_with($value, ':') && isset($replacements[$value])) {
                $value = $replacements[$value];
            }
        });

        $response = $this->recursivelyRemoveEmptyReplacements($response);

        return new Response($response, $this->getExceptionStatusCode($exception), $this->getHeaders($exception));
    }
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);
        }
    }
512023
private function getExceptionStatusCode(\Exception $exception): int
    {
        if ($exception instanceof HttpExceptionInterface) {
            return $exception->getStatusCode();
        }

        if ($exception instanceof AccessDeniedException) {
            return $exception->getCode();
        }

        return Response::HTTP_INTERNAL_SERVER_ERROR;
    }
159709
public function parse()
    {
        $error = (string) $this->data;
        $exceptionClass = $this->getExceptionClass($error);
        return new $exceptionClass($error);
    }
1105362
protected static function getFirstException(\Exception $exception)
    {
        if (null !== $exception->getPrevious()) {
            return static::getFirstException($exception->getPrevious());
        }

        return $exception;
    }
1074247
public function reportException(\Exception $exception)
    {
        $data = [

            // Request info.
            'method'    => $this->request->getMethod(),
            'ip'        => $this->request->getClientIps(),
            'path'      => $this->request->path(),
            'query'     => array_except($this->request->all(), ['_pjax', '_token', '_method', '_previous_']),
            'body'      => $this->request->getContent(),
            'cookies'   => $this->request->cookies->all(),
            'headers'   => array_except($this->request->headers->all(), 'cookie'),

            // Exception info.
            'exception' => get_class($exception),
            'code'      => $exception->getCode(),
            'file'      => $exception->getFile(),
            'line'      => $exception->getLine(),
            'message'   => $exception->getMessage(),
            'trace'     => $exception->getTraceAsString(),
        ];

        $data = $this->stringify($data);

        try {
            $this->store($data);
        } catch (\Exception $e) {
//            $result = $this->reportException($e);
        }

//        return $result;
    }
280555
public static function getErrorResponder(Request $request, \Exception $exception)
    {
        if ($request instanceof HttpRequest) {
            return self::getHttpErrorResponder($exception, $request);
        } else {
            $requestType = get_class($request);
            throw new \InvalidArgumentException("Requests of type '{$requestType}' are not supported.");
        }
    }
1054347
public function render($exception)
    {
        if ($exception instanceof HTTPException) {
            return new Response($exception->getCode(), [], $exception->getMessage());
        }

        return null;
    }
401820
public function makeThrowable(Data $data): ?ExceptionFormat
    {
        if (!$data->isThrowable()) {
            return null;
        }

        return ExceptionFormat::make($data->getValue());
    }