<commit_before>package gqlerrors

import (
	"encoding/json"
	"errors"

	"github.com/equinux/graphql/language/location"
)

type ExtendedError interface {
	error
	Extensions() map[string]interface{}
}

type FormattedError struct {
	Message    string                    `json:"message"`
	Locations  []location.SourceLocation `json:"locations"`
	Path       []interface{}             `json:"path,omitempty"`
	Extensions map[string]interface{}    `json:"extensions,omitempty"`
}

// MarshalJSON implements custom JSON marshaling for the `FormattedError` type
// in order to place the `ErrorExtensions` at the top level.
func (g FormattedError) MarshalJSON() ([]byte, error) {
	m := map[string]interface{}{}
	for k, v := range g.Extensions {
		m[k] = v
	}
	m["message"] = g.Message
	m["locations"] = g.Locations
	if g.Extensions != nil {
		m["extensions"] = g.Extensions
	}
	return json.Marshal(m)
}

func (g FormattedError) Error() string {
	return g.Message
}

func NewFormattedError(message string) FormattedError {
	err := errors.New(message)
	return FormatError(err)
}

func FormatError(err error) FormattedError {
	switch err := err.(type) {
	case FormattedError:
		return err
	case *Error:
		ret := FormattedError{
			Message:   err.Error(),
			Locations: err.Locations,
			Path:      err.Path,
		}
		if err := err.OriginalError; err != nil {
			if extended, ok := err.(ExtendedError); ok {
				ret.Extensions = extended.Extensions()
			}
		}
		return ret
	case Error:
		return FormatError(&err)
	default:
		return FormattedError{
			Message:   err.Error(),
			Locations: []location.SourceLocation{},
		}
	}
}

func FormatErrors(errs ...error) []FormattedError {
	formattedErrors := []FormattedError{}
	for _, err := range errs {
		formattedErrors = append(formattedErrors, FormatError(err))
	}
	return formattedErrors
}
<commit_msg>Fix formatted error json encoding<commit_after>package gqlerrors

import (
	"encoding/json"
	"errors"

	"github.com/equinux/graphql/language/location"
)

type ExtendedError interface {
	error
	Extensions() map[string]interface{}
}

type FormattedError struct {
	Message    string                    `json:"message"`
	Locations  []location.SourceLocation `json:"locations"`
	Path       []interface{}             `json:"path,omitempty"`
	Extensions map[string]interface{}    `json:"extensions,omitempty"`
}

// MarshalJSON implements custom JSON marshaling for the `FormattedError` type
// in order to place the `ErrorExtensions` at the top level.
func (g FormattedError) MarshalJSON() ([]byte, error) {
	m := map[string]interface{}{}
	if g.Extensions != nil {
		for k, v := range g.Extensions {
			m[k] = v
		}
		m["extensions"] = g.Extensions
	}
	m["message"] = g.Message
	m["locations"] = g.Locations
	return json.Marshal(m)
}

func (g FormattedError) Error() string {
	return g.Message
}

func NewFormattedError(message string) FormattedError {
	err := errors.New(message)
	return FormatError(err)
}

func FormatError(err error) FormattedError {
	switch err := err.(type) {
	case FormattedError:
		return err
	case *Error:
		ret := FormattedError{
			Message:   err.Error(),
			Locations: err.Locations,
			Path:      err.Path,
		}
		if err := err.OriginalError; err != nil {
			if extended, ok := err.(ExtendedError); ok {
				ret.Extensions = extended.Extensions()
			}
		}
		return ret
	case Error:
		return FormatError(&err)
	default:
		return FormattedError{
			Message:   err.Error(),
			Locations: []location.SourceLocation{},
		}
	}
}

func FormatErrors(errs ...error) []FormattedError {
	formattedErrors := []FormattedError{}
	for _, err := range errs {
		formattedErrors = append(formattedErrors, FormatError(err))
	}
	return formattedErrors
}

{'fraction_non_alphanumeric': 0.12849010571970723, 'fraction_numerical': 0.0, 'mean_word_length': 3.9069148936170213, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3356851', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1158, 'n_words': 300}