<commit_before>package handlers

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strings"
	"time"

	"github.com/jipiboily/forwardlytics/integrations"
)

// Track is taking an event to send it to the enabled integrations
func Track(w http.ResponseWriter, r *http.Request) {
	// This is the soonest we can do that, pretty much at least.
	receivedAt := time.Now().Unix()

	// This endpoint is a POST, everything else be a 404
	if r.Method != "POST" {
		http.NotFound(w, r)
		return
	}

	// Unmarshal input JSON
	decoder := json.NewDecoder(r.Body)
	var event integrations.Event
	err := decoder.Decode(&event)
	if err != nil {
		log.Println("Bad request:", r.Body)
		writeResponse(w, "Invalid request.", http.StatusBadRequest)
		return
	}
	event.ReceivedAt = receivedAt

	// Input validation
	missingParameters := event.Validate()
	if len(missingParameters) != 0 {
		msg := "Missing parameters: "
		msg = msg + strings.Join(missingParameters, ", ") + "."
		writeResponse(w, msg, http.StatusBadRequest)
		return
	}

	// Yay, it worked so far, let's send all the things to integrations!
	for _, integrationName := range integrations.IntegrationList() {
		integration := integrations.GetIntegration(integrationName)
		if integration.Enabled() {
			log.Println("Forwarding event to", integrationName)
			err := integration.Track(event)
			if err != nil {
				errMsg := fmt.Sprintf("Fatal error during event with an integration (%s): %s", integrationName, err)
				log.Println(errMsg)
				writeResponse(w, errMsg, 500)
				return
			}
		}

	}

	writeResponse(w, "Forwarding event to integrations.", http.StatusOK)
}
<commit_msg>add error printing<commit_after>package handlers

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strings"
	"time"

	"github.com/jipiboily/forwardlytics/integrations"
)

// Track is taking an event to send it to the enabled integrations
func Track(w http.ResponseWriter, r *http.Request) {
	// This is the soonest we can do that, pretty much at least.
	receivedAt := time.Now().Unix()

	// This endpoint is a POST, everything else be a 404
	if r.Method != "POST" {
		http.NotFound(w, r)
		return
	}

	// Unmarshal input JSON
	decoder := json.NewDecoder(r.Body)
	var event integrations.Event
	err := decoder.Decode(&event)
	if err != nil {
		log.Println("Bad request:", r.Body)
		log.Println("err", err)
		writeResponse(w, "Invalid request.", http.StatusBadRequest)
		return
	}
	event.ReceivedAt = receivedAt

	// Input validation
	missingParameters := event.Validate()
	if len(missingParameters) != 0 {
		msg := "Missing parameters: "
		msg = msg + strings.Join(missingParameters, ", ") + "."
		writeResponse(w, msg, http.StatusBadRequest)
		return
	}

	// Yay, it worked so far, let's send all the things to integrations!
	for _, integrationName := range integrations.IntegrationList() {
		integration := integrations.GetIntegration(integrationName)
		if integration.Enabled() {
			log.Println("Forwarding event to", integrationName)
			err := integration.Track(event)
			if err != nil {
				errMsg := fmt.Sprintf("Fatal error during event with an integration (%s): %s", integrationName, err)
				log.Println(errMsg)
				writeResponse(w, errMsg, 500)
				return
			}
		}

	}

	writeResponse(w, "Forwarding event to integrations.", http.StatusOK)
}

{'fraction_non_alphanumeric': 0.12503812137846904, 'fraction_numerical': 0.004269594388533089, 'mean_word_length': 3.9397590361445785, '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': '63711', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1037, 'n_words': 302}