<commit_before>package core

import (
    "fmt"
    "net/http"
    "encoding/json"
    "github.com/gorilla/mux"
    "github.com/codegangsta/negroni"
    "bsm/api/lesson"
    "bsm/api/slide"
)

type JSONHandlerFunc func(http.ResponseWriter, *http.Request) (interface{}, error)

func Init() {
    router := mux.NewRouter().StrictSlash(false)
    router.HandleFunc("/hello", handler)
    router.HandleFunc("/api/v1/lesson", JSONDecorator(lesson.List))
    router.HandleFunc("/api/v1/lesson/{id:[0-9]+}", JSONDecorator(lesson.Retrieve))
    router.HandleFunc("/api/v1/slide/{id:[0-9]+}", JSONDecorator(slide.Retrieve))
    // add controller routes
    router.PathPrefix("/").Handler(http.FileServer(http.Dir("./assets/")))

    stack := negroni.New()
    // add middleware
    stack.UseHandler(router)

    http.Handle("/", stack)
}

func JSONDecorator(handler JSONHandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        data, err := handler(w,r)
        payload := struct{
            Data    interface{}         `json:"data"`
            Err     error               `json:"err"`
        } {
            Data: data,
            Err: err,
        }
        
        w.Header().Set("Content-Type", "application/json")
        if bPayload, err := json.MarshalIndent(payload, "", "  "); err == nil {
            fmt.Fprint(w, string(bPayload))
        } else {
            fmt.Fprintf(w, "{\"data\":\"\",\"err\":\"Marshal error: %s\"}", err)
        }
    }
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, world!")
}

<commit_msg>return 500 on error<commit_after>package core

import (
    "fmt"
    "net/http"
    "encoding/json"
    "github.com/gorilla/mux"
    "github.com/codegangsta/negroni"
    "bsm/api/lesson"
    "bsm/api/slide"
)

type JSONHandlerFunc func(http.ResponseWriter, *http.Request) (interface{}, error)

func Init() {
    router := mux.NewRouter().StrictSlash(false)
    router.HandleFunc("/hello", handler)
    router.HandleFunc("/api/v1/lesson", JSONDecorator(lesson.List))
    router.HandleFunc("/api/v1/lesson/{id:[0-9]+}", JSONDecorator(lesson.Retrieve))
    router.HandleFunc("/api/v1/slide/{id:[0-9]+}", JSONDecorator(slide.Retrieve))
    // add controller routes
    router.PathPrefix("/").Handler(http.FileServer(http.Dir("./assets/")))

    stack := negroni.New()
    // add middleware
    stack.UseHandler(router)

    http.Handle("/", stack)
}

func JSONDecorator(handler JSONHandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        data, err := handler(w,r)

        payload := struct{
            Data    interface{}         `json:"data"`
            Err     interface{}         `json:"err"`
        } {
            Data: data,
            Err: err,
        }
        
        if err != nil {
            payload.Err = err.Error()
        }
        
        w.Header().Set("Content-Type", "application/json")
        if bPayload, merr := json.MarshalIndent(payload, "", "  "); merr == nil {
            if err != nil {
                w.WriteHeader(500)
            }
            fmt.Fprint(w, string(bPayload))
        } else {
            w.WriteHeader(500)
            fmt.Fprintf(w, "{\"data\":\"\",\"err\":\"Marshal error: %s\"}", err)
        }
    }
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, world!")
}


{'fraction_non_alphanumeric': 0.16911764705882354, 'fraction_numerical': 0.006764705882352941, 'mean_word_length': 2.3310479921645446, 'pattern_counts': {'":': 4, '<': 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': '5852792', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1109, 'n_words': 187}