/** * Log an error. * * @param string $level The level name of the log. * @param array $data Array of error data. * @return bool */ protected function _logError($level, $data) { $message = sprintf('%s (%s): %s in [%s, line %s]', $data['error'], $data['code'], $data['description'], $data['file'], $data['line']); if (!empty(Configuration::getInstance()->get("Error/printTrace", false))) { $trace = Debugger::trace(['start' => 1, 'format' => 'log']); $request = Router::getInstance()->request(); if ($request) { $message .= $this->_requestContext($request); } $message .= "\nTrace:\n" . $trace . "\n"; } $message .= "\n\n"; return Logger::getInstance()->write($level, $message); }
/** * Display an error. * * Template method of BaseErrorHandler. * * Only when debug > 2 will a formatted error be displayed. * * @param array $error An array of error data. * @param bool $debug Whether or not the app is in debug mode. * @return void */ protected function _displayError($error, $debug) { if (!$debug) { return; } Debugger::getInstance()->outputError($error); }
/** * Add an output format or update a format in Debugger. * * ``` * Debugger::addFormat('custom', $data); * ``` * * Where $data is an array of strings that use Text::insert() variable * replacement. The template vars should be in a `{:id}` style. * An error formatter can have the following keys: * * - 'error' - Used for the container for the error message. Gets the following template * variables: `id`, `error`, `code`, `description`, `path`, `line`, `links`, `info` * - 'info' - A combination of `code`, `context` and `trace`. Will be set with * the contents of the other template keys. * - 'trace' - The container for a stack trace. Gets the following template * variables: `trace` * - 'context' - The container element for the context variables. * Gets the following templates: `id`, `context` * - 'links' - An array of HTML links that are used for creating links to other resources. * Typically this is used to create javascript links to open other sections. * Link keys, are: `code`, `context`, `help`. See the js output format for an * example. * - 'traceLine' - Used for creating lines in the stacktrace. Gets the following * template variables: `reference`, `path`, `line` * * Alternatively if you want to use a custom callback to do all the formatting, you can use * the callback key, and provide a callable: * * ``` * Debugger::addFormat('custom', ['callback' => [$foo, 'outputError']]; * ``` * * The callback can expect two parameters. The first is an array of all * the error data. The second contains the formatted strings generated using * the other template strings. Keys like `info`, `links`, `code`, `context` and `trace` * will be present depending on the other templates in the format type. * * @param string $format Format to use, including 'js' for JavaScript-enhanced HTML, 'html' for * straight HTML output, or 'txt' for unformatted text. * @param array $strings Template strings, or a callback to be used for the output format. * @return array The resulting format string set. */ public static function addFormat($format, array $strings) { $self = Debugger::getInstance(); if (isset($self->_templates[$format])) { if (isset($strings['links'])) { $self->_templates[$format]['links'] = array_merge($self->_templates[$format]['links'], $strings['links']); unset($strings['links']); } $self->_templates[$format] = $strings + $self->_templates[$format]; } else { $self->_templates[$format] = $strings; } return $self->_templates[$format]; }
public function processError($request, $exception) { $response = new Response(); $viewVars = ["exception" => $exception]; $code = 500; $errorCode = $exception->getCode(); if ($errorCode >= 400 && $errorCode < 506) { $code = $errorCode; } $response->statusCode($code); $viewVars["code"] = $code; if (method_exists($exception, 'responseHeader')) { $response->header($exception->responseHeader()); } if ($request) { $viewVars["url"] = $request->url(); } $isDebug = Configuration::getInstance()->get("debug"); if ($isDebug) { $viewVars['trace'] = Debugger::formatTrace($exception->getTrace(), ['format' => 'array', 'args' => false]); } $message = $exception->getMessage(); $isHttpException = $exception instanceof HttpException; if (!$isDebug && !$isHttpException) { if ($code < 500) { $message = \CoreTyson\tr('cake', 'Not Found'); } else { $message = \CoreTyson\tr('cake', 'An Internal Error Has Occurred.'); } } $viewVars["message"] = $message; $template = "error" . $code; if (!$isDebug && !$isHttpException) { $template = 'error500'; if ($code < 500) { $template = 'error400'; } } if ($isHttpException) { $template = 'error500'; if ($code < 500) { $template = 'error400'; } } if ($exception instanceof PDOException) { $template = 'pdo_error'; } try { $view = new View(); $response->body($view->render("Error/" . $template)); } catch (MissingTemplateException $e) { return $this->_outputMessageSafe('error500'); } catch (MissingPluginException $e) { $attributes = $e->getAttributes(); if (isset($attributes['plugin']) && $attributes['plugin'] === $this->controller->plugin) { $this->controller->plugin = null; } return $this->_outputMessageSafe('error500'); } catch (Exception $e) { return $this->_outputMessageSafe('error500'); } }