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" "net/http"
"text/template" "text/template"
"github.com/rs/zerolog/log"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
) )
@ -46,7 +48,11 @@ func (h *Headscale) AppleMobileConfig(c *gin.Context) {
var payload bytes.Buffer var payload bytes.Buffer
if err := t.Execute(&payload, config); err != nil { 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 return
} }
@ -58,13 +64,21 @@ func (h *Headscale) ApplePlatformConfig(c *gin.Context) {
id, err := uuid.NewV4() id, err := uuid.NewV4()
if err != nil { 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 return
} }
contentId, err := uuid.NewV4() contentId, err := uuid.NewV4()
if err != nil { 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 return
} }
@ -78,12 +92,20 @@ func (h *Headscale) ApplePlatformConfig(c *gin.Context) {
switch platform { switch platform {
case "macos": case "macos":
if err := macosTemplate.Execute(&payload, platformConfig); err != nil { 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 return
} }
case "ios": case "ios":
if err := iosTemplate.Execute(&payload, platformConfig); err != nil { 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 return
} }
default: default:
@ -99,7 +121,11 @@ func (h *Headscale) ApplePlatformConfig(c *gin.Context) {
var content bytes.Buffer var content bytes.Buffer
if err := commonTemplate.Execute(&content, config); err != nil { 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 return
} }