Prepares error messages returned by our XML-RPC requests to be send as debug info to {@link print_error()}

MNet client-side methods in this class return request error as serialized array.

@param string $error serialized array
@return string
433562
public function format_error_message($errormsg) {
        $errors = unserialize($errormsg);
        $output = 'mnet_xmlrpc_client request returned errors:'."\n";
        foreach ($errors as $error) {
            $output .= "$error\n";
        }
        return $output;
    }
53610
private function messageFrom($response)
    {
        if (isset($response['Fault_Code']) && isset($response['Fault_String'])) {
            return $response['Fault_String'];
        } elseif (isset($response['Error_Number']) && $response['Error_Number'] != '0') {
            return $response['Error_Description'];
        } else {
            $result = isset($response['EXact_Message']) ? $response['EXact_Message'] : '';
            if (isset($response['Bank_Message']) && !empty($response['Bank_Message'])) {
                $result .= ' - ' . $response['Bank_Message'];
            }

            return $result;
        }
    }
1025418
protected function formatThrowable($error)
    {
        $message = trim($error->getMessage()) . "\n";
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
            $file = $error->getFile();
            if ($file) {
                $message .= "\nFile:\n$file";
                $line = $error->getLine();
                if ($line) {
                    $message .= ':' . $line;
                }
                $message .= "\n";
            }
            if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
                $trace = $error->getTraceAsString();
                if ($trace) {
                    $message .= "\nTrace:\n$trace\n";
                }
            }
        }

        return $message;
    }
1221556
protected function failureResponse($error = null)
    {
        if (request()->ajax()) {
            return response()->json([
                'success' => false,
                'error'   => $error,
            ]);
        }

        return redirect()->back()->withErrors([
            'general' => $error,
        ]);
    }
104209
protected function getNotFoundPage($error)
    {
        return new Response($this->twig->render('@crud/notFound.twig', [
            'crud' => $this->service,
            'error' => $error,
            'crudEntity' => '',
            'layout' => $this->service->getTemplate('layout', '', '')
        ]), 404);
    }
860902
protected function getErrorMessage(Response $response)
    {
        if ($response->hasBody()) {
            if (is_array($response->body)) {
                return isset($response->body['error']['message']) ? $response->body['error']['message'] : $response->raw_body;
            } else {
                return isset($response->body->error->message) ? $response->body->error->message : $response->raw_body;
            }
        } else {
            return $response->raw_body;
        }
    }
588442
public function errors($errors) {
        $html = '';
        foreach ($errors as $error) {
            $html .= $this->output->notification($error);
        }
        return $html;
    }
802749
protected function buildErrorResponse($error, $code = null)
    {
        if ($code !== null && (!is_int($code) || $code < 400)) {
            $code = null;
        }
        if (is_object($error)) {
            if ($error instanceof AccessDeniedException) {
                $error = $error->getMessage();
                if ($code === null) {
                    $code = Response::HTTP_UNAUTHORIZED;
                }
            } elseif ($error instanceof UserMessageException) {
                if ($error->getCode() >= 400) {
                    $code = $error->getCode();
                }
                $error = $error->getMessage();
            } elseif ($error instanceof Exception || $error instanceof Throwable) {
                $error = t('An unexpected error occurred');
            } elseif (is_callable([$error, '__toString'])) {
                $error = (string) $error;
            } else {
                $error = get_class($error);
            }
        }
        if ($code === null) {
            $code = Response::HTTP_INTERNAL_SERVER_ERROR;
        }

        return $this->getResponseFactory()->create(
            $error,
            $code,
            [
                'Content-Type' => 'text/plain; charset=UTF-8',
            ]
        );
    }
599160
private function setError($error)
    {
        $this->errors[] = $error;
        if ($this->do_debug >= 1) {
            echo '<pre>';
            foreach ($this->errors as $error) {
                print_r($error);
            }
            echo '</pre>';
        }
    }
367574
protected function serializeError(ErrorInterface $error): array
    {
        $data = [
            'status' => $error->status(),
        ];

        if ($error->code() !== '') {
            $data['code'] = $error->code();
        }
        if ($error->title() !== '') {
            $data['title'] = $error->title();
        }
        if ($error->detail() !== '') {
            $data['detail'] = $error->detail();
        }

        if (!$error->metaInformation()->isEmpty()) {
            $data['meta'] = $error->metaInformation()->all();
        }

        if (!$error->source()->isEmpty()) {
            $data['source'] = $error->source()->all();
        }

        return $data;
    }
77486
private function retrieveErrorCode($body)
    {
        if (empty($body) || false === strpos($body, '<?xml')) {
            return '';
        }
        $xml = simplexml_load_string($body);
        if (isset($xml->Code)) {
            return strval($xml->Code);
        }
        return '';
    }
520471
protected function render($error)
    {
        $ife = function ($condition, $if, $else = null) {
            return $condition ? $if : $else;
        };

        $classes = function (...$classes) {
            return implode(' ', array_filter($classes, 'strlen'));
        };

        $showCode = $this->isExcerptOnly() === false || $this->getExcerptSize() > 0;

        if (is_readable($header = $this->getHeader())) {
            $header = $this->read($header, 'require', compact('ife', 'classes'));
        }

        if (is_readable($footer = $this->getFooter())) {
            $footer = $this->read($footer, 'require', compact('ife', 'classes'));
        }

        return $this->read(
            $this->getTemplate(), 'require', compact(
                'error', 'ife', 'classes', 'showCode', 'header', 'footer'
            )
        );
    }
1073723
protected function generate_error($ex) {
        if ($this->restformat == 'json') {
            $errorobject = new stdClass;
            $errorobject->exception = get_class($ex);
            if (isset($ex->errorcode)) {
                $errorobject->errorcode = $ex->errorcode;
            }
            $errorobject->message = $ex->getMessage();
            if (debugging() and isset($ex->debuginfo)) {
                $errorobject->debuginfo = $ex->debuginfo;
            }
            $error = json_encode($errorobject);
        } else {
            $error = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
            $error .= '<EXCEPTION class="'.get_class($ex).'">'."\n";
            if (isset($ex->errorcode)) {
                $error .= '<ERRORCODE>' . htmlspecialchars($ex->errorcode, ENT_COMPAT, 'UTF-8')
                        . '</ERRORCODE>' . "\n";
            }
            $error .= '<MESSAGE>'.htmlspecialchars($ex->getMessage(), ENT_COMPAT, 'UTF-8').'</MESSAGE>'."\n";
            if (debugging() and isset($ex->debuginfo)) {
                $error .= '<DEBUGINFO>'.htmlspecialchars($ex->debuginfo, ENT_COMPAT, 'UTF-8').'</DEBUGINFO>'."\n";
            }
            $error .= '</EXCEPTION>'."\n";
        }
        return $error;
    }
594688
private function tryResolveErrorReason(string $message): string
    {
        $key = $this->responseParser->parse($message);

        if ($key === 'invalid_phone') {
            return ErrorReasons::INVALID_PHONE_NUMBER;
        }

        return ErrorReasons::UNKNOWN;
    }
391337
public function getErrorMessage(int $error): string
    {
        switch ($error) {
            case CURLE_UNSUPPORTED_PROTOCOL:
                return 'Unsupported protocol';
            case CURLE_FAILED_INIT:
                return 'Internal cUrl error?';
            case CURLE_URL_MALFORMAT:
                return 'Malformed URL';
            case CURLE_COULDNT_RESOLVE_PROXY:
                return "Couldn't resolve proxy";
            case CURLE_COULDNT_RESOLVE_HOST:
                return "Couldn't resolve host";
            case CURLE_COULDNT_CONNECT:
                return "Couldn't connect to host, Elasticsearch down?";
            case CURLE_OPERATION_TIMEOUTED:
                return 'Operation timed out';
        }

        return 'Unknown error:'.$error;
    }
392670
private static function readError($error)
    {
        $err = new stdClass();
        $err->message = key($error);
        $err->status = true;

        return $err;
    }
420000
private function getError(string $xml)
    {
        if ([] !== ($errors = libxml_get_errors())) {
            libxml_clear_errors();
            return new XmlException($errors, sprintf("Invalid xml string \n\n%s\n", $xml));
        }
        return null;
    }