<commit_before>package tt_goji_middleware

import (
	"fmt"
	"log"
	"net/http"
	"runtime/debug"
	// "strings"
	"runtime"

	"github.com/zenazn/goji/web"

	"github.com/kisielk/raven-go/raven"
)

func BuildErrorCatcher(sentryDSN string) func(c *web.C, h http.Handler) http.Handler {
	var sentryClient *raven.Client
	if sentryDSN != "" {
		var err error
		sentryClient, err = raven.NewClient(sentryDSN)
		if err != nil {
			log.Printf("Couldn't connect to sentry %v\n", err)
		}
	}

	return func(c *web.C, h http.Handler) http.Handler {
		handler := func(w http.ResponseWriter, r *http.Request) {

			defer func() {
				err := recover()
				if err == nil {
					return
				}
				if sentryClient != nil {
					// Send the error to sentry
					const size = 1 << 12
					buf := make([]byte, size)
					n := runtime.Stack(buf, false)
					sentryClient.CaptureMessage(fmt.Sprintf("%v\nStacktrace:\n%s", err, buf[:n]))
				}

				switch err := err.(type) {
				case HttpError:
					err.WriteResponse(w)
					return
				default:
					http.Error(w, http.StatusText(500), 500)
					log.Printf("Panic: %v\n", err)
					debug.PrintStack()
					return
				}
			}()

			h.ServeHTTP(w, r)
		}
		return http.HandlerFunc(handler)
	}
}

type HttpError struct {
	StatusCode int
	Message    string<commit_msg>additional logs from error catcher<commit_after>package tt_goji_middleware

import (
	"fmt"
	"log"
	"net/http"
	"runtime/debug"
	// "strings"
	"runtime"

	"github.com/zenazn/goji/web"

	"github.com/kisielk/raven-go/raven"
)

func BuildErrorCatcher(sentryDSN string) func(c *web.C, h http.Handler) http.Handler {
	var sentryClient *raven.Client
	if sentryDSN != "" {
		var err error
		sentryClient, err = raven.NewClient(sentryDSN)
		if err != nil {
			log.Printf("Couldn't connect to sentry %v\n", err)
		}
	}

	return func(c *web.C, h http.Handler) http.Handler {
		handler := func(w http.ResponseWriter, r *http.Request) {

			defer func() {
				err := recover()
				if err == nil {
					return
				}
				if sentryClient != nil {
					// Send the error to sentry
					const size = 1 << 12
					buf := make([]byte, size)
					n := runtime.Stack(buf, false)
					sentryClient.CaptureMessage(fmt.Sprintf("%v\nStacktrace:\n%s", err, buf[:n]))
				}

				switch err := err.(type) {
				case HttpError:
					log.Printf("Return response for error %s", err.Message)
					err.WriteResponse(w)
					return
				default:
					http.Error(w, http.StatusText(500), 500)
					log.Printf("Panic: %v\n", err)
					debug.PrintStack()
					return
				}
			}()

			h.ServeHTTP(w, r)
		}
		return http.HandlerFunc(handler)
	}
}

type HttpError struct {
	StatusCode int
	Message    string
{'fraction_non_alphanumeric': 0.14177978883861236, 'fraction_numerical': 0.006787330316742082, 'mean_word_length': 2.901470588235294, 'pattern_counts': {'":': 0, '<': 7, '<?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': '456033', 'n_tokens_mistral': 1188, 'n_tokens_neox': 977, 'n_words': 221}