package main

import (
	"fmt"
	"log"
	"net/http"
)

// writeInternalServerError logs err and returns a generic 500 to the client.
// It never exposes err.Error() to the HTTP response.
func writeInternalServerError(w http.ResponseWriter, err error, context string) {
	writeServerError(w, err, "Internal Server Error", context)
}

// writeServerError logs err and returns publicMessage with HTTP 500 to the client.
// It never exposes err.Error() to the HTTP response.
func writeServerError(w http.ResponseWriter, err error, publicMessage string, context string) {
	if err != nil && context != "" {
		err = fmt.Errorf("%s: %w", context, err)
	}
	if err != nil {
		log.Printf("server error: %v", err)
	}
	http.Error(w, publicMessage, http.StatusInternalServerError)
}
