<commit_before>package middlewares

import (
	"fmt"
	"net/http"

	"github.com/sirupsen/logrus"
	"github.com/urfave/negroni"
)

type textPanicFormatter struct{}

func (t *textPanicFormatter) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *negroni.PanicInformation) {
	if rw.Header().Get("Content-Type") == "" {
		rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
	}
	fmt.Fprintf(rw, http.StatusText(http.StatusInternalServerError))
}

func NewRecovery(logger logrus.FieldLogger) negroni.Handler {
	middleware := negroni.NewRecovery()
	middleware.PrintStack = false
	middleware.Logger = logger
	middleware.Formatter = &textPanicFormatter{}
	return middleware
}<commit_msg>Create JSON response for panic recovery middleware, create responsePanic struct<commit_after>package middlewares

import (
	"encoding/json"
	"net/http"

	"github.com/sirupsen/logrus"
	"github.com/urfave/negroni"
)

type responsePanic struct {
	Code int    `json:"code"`
	Text string `json:"text"`
}

type textPanicFormatter struct {
	logger logrus.FieldLogger
}

// Implement PanicFormatter interface
func (t *textPanicFormatter) FormatPanicError(rw http.ResponseWriter, r *http.Request, infos *negroni.PanicInformation) {
	t.writeResponseJSON(rw, http.StatusInternalServerError, &responsePanic{
		Code: http.StatusInternalServerError,
		Text: "panic occurred",
	})
}

func (t *textPanicFormatter) writeResponseJSON(w http.ResponseWriter, statusCode int, response interface{}) {
	w.WriteHeader(statusCode)
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	if err := json.NewEncoder(w).Encode(response); err != nil {
		t.logger.WithError(err).Error("cannot send response on panic")
	}
}

func NewRecovery(logger logrus.FieldLogger) negroni.Handler {
	middleware := negroni.NewRecovery()
	middleware.PrintStack = false
	middleware.Logger = logger
	middleware.Formatter = &textPanicFormatter{
		logger: logger,
	}
	return middleware
}
{'fraction_non_alphanumeric': 0.12268993839835729, 'fraction_numerical': 0.001026694045174538, 'mean_word_length': 5.862676056338028, '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': '626059', 'n_tokens_mistral': 658, 'n_tokens_neox': 646, 'n_words': 129}