public function toArray($debug = false)
    {
        $result = [];

        if (! empty($this->errors)) {
            $errorsHandler = $this->errorsHandler ?: static function (array $errors, callable $formatter) {
                return array_map($formatter, $errors);
            };

            $result['errors'] = $errorsHandler(
                $this->errors,
                FormattedError::prepareFormatter($this->errorFormatter, $debug)
            );
        }

        if ($this->data !== null) {
            $result['data'] = $this->data;
        }

        if (! empty($this->extensions)) {
            $result['extensions'] = $this->extensions;
        }

        return $result;
    }