Handle errors

This commit is contained in:
Kristoffer Dalby 2021-09-20 07:54:18 +01:00
parent 2d39d6602c
commit b3efd1e47b
No known key found for this signature in database
GPG key ID: 09F62DC067465735

View file

@ -5,6 +5,8 @@ import (
"net/http"
"text/template"
"github.com/rs/zerolog/log"
"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
)
@ -46,7 +48,11 @@ func (h *Headscale) AppleMobileConfig(c *gin.Context) {
var payload bytes.Buffer
if err := t.Execute(&payload, config); err != nil {
c.Error(err)
log.Error().
Str("handler", "AppleMobileConfig").
Err(err).
Msg("Could not render Apple index template")
c.Data(http.StatusInternalServerError, "text/html; charset=utf-8", []byte("Could not render Apple index template"))
return
}
@ -58,13 +64,21 @@ func (h *Headscale) ApplePlatformConfig(c *gin.Context) {
id, err := uuid.NewV4()
if err != nil {
c.Error(err)
log.Error().
Str("handler", "ApplePlatformConfig").
Err(err).
Msg("Failed not create UUID")
c.Data(http.StatusInternalServerError, "text/html; charset=utf-8", []byte("Failed to create UUID"))
return
}
contentId, err := uuid.NewV4()
if err != nil {
c.Error(err)
log.Error().
Str("handler", "ApplePlatformConfig").
Err(err).
Msg("Failed not create UUID")
c.Data(http.StatusInternalServerError, "text/html; charset=utf-8", []byte("Failed to create UUID"))
return
}
@ -78,12 +92,20 @@ func (h *Headscale) ApplePlatformConfig(c *gin.Context) {
switch platform {
case "macos":
if err := macosTemplate.Execute(&payload, platformConfig); err != nil {
c.Error(err)
log.Error().
Str("handler", "ApplePlatformConfig").
Err(err).
Msg("Could not render Apple macOS template")
c.Data(http.StatusInternalServerError, "text/html; charset=utf-8", []byte("Could not render Apple macOS template"))
return
}
case "ios":
if err := iosTemplate.Execute(&payload, platformConfig); err != nil {
c.Error(err)
log.Error().
Str("handler", "ApplePlatformConfig").
Err(err).
Msg("Could not render Apple iOS template")
c.Data(http.StatusInternalServerError, "text/html; charset=utf-8", []byte("Could not render Apple iOS template"))
return
}
default:
@ -99,7 +121,11 @@ func (h *Headscale) ApplePlatformConfig(c *gin.Context) {
var content bytes.Buffer
if err := commonTemplate.Execute(&content, config); err != nil {
c.Error(err)
log.Error().
Str("handler", "ApplePlatformConfig").
Err(err).
Msg("Could not render Apple platform template")
c.Data(http.StatusInternalServerError, "text/html; charset=utf-8", []byte("Could not render Apple platform template"))
return
}