<commit_before>package middleware

import (
	"net/http"

	"github.com/prasannavl/goerror/httperror"

	"github.com/prasannavl/mchain"
)

func PanicRecoveryMiddleware(next mchain.Handler) mchain.Handler {
	f := func(w http.ResponseWriter, r *http.Request) (err error) {
		defer mchain.RecoverIntoError(&err)
		err = next.ServeHTTP(w, r)
		return err
	}
	return mchain.HandlerFunc(f)
}

func ErrorHandlerMiddleware(next mchain.Handler) mchain.Handler {
	f := func(w http.ResponseWriter, r *http.Request) (err error) {
		err = next.ServeHTTP(w, r)
		if err != nil {
			switch e := err.(type) {
			case httperror.HttpError:
				w.WriteHeader(e.Code())
			case error:
				w.WriteHeader(http.StatusInternalServerError)
			}
		}<commit_msg>fix: error handler middleware - set status only when conn not hijacked<commit_after>package middleware

import (
	"net/http"

	"github.com/prasannavl/goerror/httperror"

	"github.com/prasannavl/mchain"
)

func PanicRecoveryMiddleware(next mchain.Handler) mchain.Handler {
	f := func(w http.ResponseWriter, r *http.Request) (err error) {
		defer mchain.RecoverIntoError(&err)
		err = next.ServeHTTP(w, r)
		return err
	}
	return mchain.HandlerFunc(f)
}

func ErrorHandlerMiddleware(next mchain.Handler) mchain.Handler {
	f := func(w http.ResponseWriter, r *http.Request) (err error) {
		err = next.ServeHTTP(w, r)
		if err != nil {
			if _, err2 := w.Write(nil); err2 == http.ErrHijacked {
				return err
			}
			switch e := err.(type) {
			case httperror.HttpError:
				w.WriteHeader(e.Code())
			case error:
				w.WriteHeader(http.StatusInternalServerError)
			}
		}
{'fraction_non_alphanumeric': 0.12632895559724827, 'fraction_numerical': 0.0012507817385866166, 'mean_word_length': 4.079365079365079, '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': '7428460', 'n_tokens_mistral': 632, 'n_tokens_neox': 583, 'n_words': 121}