46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/oslstats/internal/notify"
|
|
"git.haelnorr.com/h/oslstats/internal/throw"
|
|
"github.com/a-h/templ"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// parseErrorDetails extracts code and stacktrace from JSON Details field
|
|
// Returns (code, stacktrace). If parsing fails, returns (500, original details string)
|
|
func parseErrorDetails(details string) (int, string) {
|
|
if details == "" {
|
|
return 500, ""
|
|
}
|
|
|
|
var errDetails notify.ErrorDetails
|
|
err := json.Unmarshal([]byte(details), &errDetails)
|
|
if err != nil {
|
|
// Not JSON or malformed - treat as plain stacktrace with default code
|
|
return 500, details
|
|
}
|
|
|
|
return errDetails.Code, errDetails.Stacktrace
|
|
}
|
|
|
|
func renderSafely(page templ.Component, s *hws.Server, r *http.Request, w http.ResponseWriter) {
|
|
err := page.Render(r.Context(), w)
|
|
if err != nil {
|
|
throw.InternalServiceError(s, w, r, "Failed to render page", errors.Wrap(err, "page."))
|
|
}
|
|
}
|
|
|
|
func logError(s *hws.Server, msg string, err error) {
|
|
s.LogError(hws.HWSError{
|
|
Message: msg,
|
|
Error: err,
|
|
Level: hws.ErrorERROR,
|
|
StatusCode: http.StatusInternalServerError,
|
|
})
|
|
}
|