src/includes/classes/Core/Response.php
public function withError(int $status, $data, bool $as_json = null): self
    {
        $is_data_error = $data && $this->App->c::isError($data);

        if (!isset($as_json) && $is_data_error) {
            $as_json = $this->App->c::isApi() || $this->App->c::isAjax();
        }
        if ($as_json) { // Format as JSON?
            $data         = $is_data_error ? $data : null;
            $Error        = $data ?: $this->App->c::error();
            $content_type = 'application/json';
            $content      = json_encode([
                'success' => false,
                'error'   => [
                    'code'    => $status,
                    'slug'    => $Error->slug(),
                    'message' => $Error->message(),
                ],
            ], JSON_PRETTY_PRINT);
        } else {
            $data         = is_string($data) ? $data : '';
            $content      = $data ?: $this->App->c::statusHeaderMessage($status);
            $content_type = 'text/'.($this->App->c::isHtml($content, true) ? 'html' : 'plain');
        }
        $clone = $this->withStatus($status);
        $clone = $clone->withHeader('content-type', $content_type.'; charset=utf-8');

        $Body         = $this->App->c::createReponseBody($content);
        return $clone = $clone->withBody($Body);
    }
public function withError(int $status, $data, bool $as_json = null): self
    {
        $is_data_error = $data && $this->App->c::isError($data);

        if (!isset($as_json) && $is_data_error) {
            $as_json = $this->App->c::isApi() || $this->App->c::isAjax();
        }
        if ($as_json) { // Format as JSON?
            $data         = $is_data_error ? $data : null;
            $Error        = $data ?: $this->App->c::error();
            $content_type = 'application/json';
            $content      = json_encode([
                'success' => false,
                'error'   => [
                    'code'    => $status,
                    'slug'    => $Error->slug(),
                    'message' => $Error->message(),
                ],
            ], JSON_PRETTY_PRINT);
        } else {
            $data         = is_string($data) ? $data : '';
            $content      = $data ?: $this->App->c::statusHeaderMessage($status);
            $content_type = 'text/'.($this->App->c::isHtml($content, true) ? 'html' : 'plain');
        }
        $clone = $this->withStatus($status);
        $clone = $clone->withHeader('content-type', $content_type.'; charset=utf-8');

        $Body         = $this->App->c::createReponseBody($content);
        return $clone = $clone->withBody($Body);
    }
Error response.

@since 17xxxx Response utils.

@param int          $status  Status.
@param Error|string $data    Error|markup.
@param bool         $as_json As JSON?

@return Response w/ error response.