<commit_before>/*
Package response provides helpers and utils for working with HTTP response
*/
package response

import (
	"context"
	"encoding/json"
	"net/http"

	"github.com/vardius/go-api-boilerplate/internal/errors"
	"github.com/vardius/go-api-boilerplate/internal/http/middleware/metadata"
)

// RespondJSON returns data as json response
func RespondJSON(ctx context.Context, w http.ResponseWriter, payload interface{}, statusCode int) {

	// If there is nothing to marshal then set status code and return.
	if payload == nil || statusCode == http.StatusNoContent {
		w.WriteHeader(statusCode)
		if metadata, ok := ctx.Value(metadata.KeyMetadataValues).(*metadata.Metadata); ok {
			metadata.StatusCode = statusCode
		}
		return
	}

	encoder := json.NewEncoder(w)
	encoder.SetEscapeHTML(true)
	encoder.SetIndent("", "")

	w.WriteHeader(statusCode)
	w.Header().Set("Content-Type", "application/json")

	if err := encoder.Encode(payload); err != nil {
		panic(err)
	}

	if metadata, ok := ctx.Value(metadata.KeyMetadataValues).(*metadata.Metadata); ok {
		metadata.StatusCode = statusCode
	}

	// Check if it is stream response
	if f, ok := w.(http.Flusher); ok {
		f.Flush()
	}
}

// RespondJSONError returns error response
// uses WithPayloadAsJSON internally
func RespondJSONError(ctx context.Context, w http.ResponseWriter, err error) {
	RespondJSON(ctx, w, err, errors.HTTPStatusCode(err))
}<commit_msg>Log response error<commit_after>/*
Package response provides helpers and utils for working with HTTP response
*/
package response

import (
	"context"
	"encoding/json"
	"log"
	"net/http"

	"github.com/vardius/go-api-boilerplate/internal/errors"
	"github.com/vardius/go-api-boilerplate/internal/http/middleware/metadata"
)

// RespondJSON returns data as json response
func RespondJSON(ctx context.Context, w http.ResponseWriter, payload interface{}, statusCode int) {

	// If there is nothing to marshal then set status code and return.
	if payload == nil || statusCode == http.StatusNoContent {
		w.WriteHeader(statusCode)
		if metadata, ok := ctx.Value(metadata.KeyMetadataValues).(*metadata.Metadata); ok {
			metadata.StatusCode = statusCode
		}
		return
	}

	encoder := json.NewEncoder(w)
	encoder.SetEscapeHTML(true)
	encoder.SetIndent("", "")

	w.WriteHeader(statusCode)
	w.Header().Set("Content-Type", "application/json")

	if err := encoder.Encode(payload); err != nil {
		panic(err)
	}

	if metadata, ok := ctx.Value(metadata.KeyMetadataValues).(*metadata.Metadata); ok {
		metadata.StatusCode = statusCode
	}

	// Check if it is stream response
	if f, ok := w.(http.Flusher); ok {
		f.Flush()
	}
}

// RespondJSONError returns error response
// uses WithPayloadAsJSON internally
func RespondJSONError(ctx context.Context, w http.ResponseWriter, err error) {
	log.Printf("RespondJSONError: %v\n", err)

	RespondJSON(ctx, w, err, errors.HTTPStatusCode(err))
}
{'fraction_non_alphanumeric': 0.12743055555555555, 'fraction_numerical': 0.0, 'mean_word_length': 4.583333333333333, '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': '2737558', 'n_tokens_mistral': 941, 'n_tokens_neox': 898, 'n_words': 228}