<commit_before>package router

import (
	"log"
	"net/http"
	"strings"

	"github.com/julienschmidt/httprouter"
)

type Handler func(http.ResponseWriter, *http.Request) *HandlerError

type HandlerWithParams func(http.ResponseWriter, *http.Request, httprouter.Params) *HandlerError

type HandlerError struct {
	Err     error
	Message string
	Code    int
}

func (r Router) GET(path string, handler Handler) {
	r.Handler("GET", r.prefix+path, handleErrors(handler))
}

func (r Router) GETWithParams(path string, handler HandlerWithParams) {
	r.Router.GET(r.prefix+path, handleErrorsWithParams(handler))
}

func (r Router) POST(path string, handler Handler) {
	r.Handler("POST", r.prefix+path, handleErrors(handler))
}

func (r Router) PUTWithParams(path string, handler HandlerWithParams) {
	r.PUT(r.prefix+path, handleErrorsWithParams(handler))
}

// Router is a wrapper around julienschmidt/httprouter that implements error
// handling specific to this application.
type Router struct {
	*httprouter.Router
	prefix string
}

// NewWithPrefix creates a Router that adds an option to use a common prefix for
// all the paths.
func NewWithPrefix(prefix string) *Router {
	return &Router{
		Router: httprouter.New(),
		prefix: strings.TrimSuffix(prefix, "/"),
	}
}

func handleErrors(h Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if e := h(w, r); e != nil {
			handleError(w, e)
		}
	})
}

func handleErrorsWithParams(h HandlerWithParams) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		if e := h(w, r, ps); e != nil {
			handleError(w, e)
		}
	}
}

func handleError(w http.ResponseWriter, e *HandlerError) {
	log.Print(e.Err)
	http.Error(w, e.Message, e.Code)
}

func (e *HandlerError) Error() string {
	return e.Err.Error()
}<commit_msg>update router to include message and code sent to user in error logs<commit_after>package router

import (
	"log"
	"net/http"
	"strings"

	"github.com/julienschmidt/httprouter"
)

type Handler func(http.ResponseWriter, *http.Request) *HandlerError

type HandlerWithParams func(http.ResponseWriter, *http.Request, httprouter.Params) *HandlerError

type HandlerError struct {
	Err     error
	Message string
	Code    int
}

func (r Router) GET(path string, handler Handler) {
	r.Handler("GET", r.prefix+path, handleErrors(handler))
}

func (r Router) GETWithParams(path string, handler HandlerWithParams) {
	r.Router.GET(r.prefix+path, handleErrorsWithParams(handler))
}

func (r Router) POST(path string, handler Handler) {
	r.Handler("POST", r.prefix+path, handleErrors(handler))
}

func (r Router) PUTWithParams(path string, handler HandlerWithParams) {
	r.PUT(r.prefix+path, handleErrorsWithParams(handler))
}

// Router is a wrapper around julienschmidt/httprouter that implements error
// handling specific to this application.
type Router struct {
	*httprouter.Router
	prefix string
}

// NewWithPrefix creates a Router that adds an option to use a common prefix for
// all the paths.
func NewWithPrefix(prefix string) *Router {
	return &Router{
		Router: httprouter.New(),
		prefix: strings.TrimSuffix(prefix, "/"),
	}
}

func handleErrors(h Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if e := h(w, r); e != nil {
			handleError(w, e)
		}
	})
}

func handleErrorsWithParams(h HandlerWithParams) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		if e := h(w, r, ps); e != nil {
			handleError(w, e)
		}
	}
}

func handleError(w http.ResponseWriter, e *HandlerError) {
	log.Println(e.Err)
	log.Printf("Responded to the user with code %d and message: %s\n", e.Code, e.Message)
	http.Error(w, e.Message, e.Code)
}

func (e *HandlerError) Error() string {
	return e.Err.Error()
}
{'fraction_non_alphanumeric': 0.12172774869109948, 'fraction_numerical': 0.0, 'mean_word_length': 4.754518072289157, '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': '6028746', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1280, 'n_words': 354}