From 20d2615081e79a40c711476a4464c78dddc59b51 Mon Sep 17 00:00:00 2001
From: Juan Font Alonso <juanfontalonso@gmail.com>
Date: Sun, 14 Aug 2022 12:47:04 +0200
Subject: [PATCH] Check json encoder errors

---
 noise_api.go | 34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/noise_api.go b/noise_api.go
index 29ab98df..6bf555e6 100644
--- a/noise_api.go
+++ b/noise_api.go
@@ -313,7 +313,15 @@ func (h *Headscale) handleNoiseAuthKey(
 
 	writer.Header().Set("Content-Type", "application/json")
 	writer.WriteHeader(http.StatusOK)
-	json.NewEncoder(writer).Encode(resp)
+	err = json.NewEncoder(writer).Encode(resp)
+	if err != nil {
+		log.Error().
+			Caller().
+			Err(err).
+			Msg("Failed to encode response")
+
+		return
+	}
 
 	log.Info().
 		Caller().
@@ -343,7 +351,13 @@ func (h *Headscale) handleNoiseNodeValidRegistration(
 		Inc()
 	writer.Header().Set("Content-Type", "application/json")
 	writer.WriteHeader(http.StatusOK)
-	json.NewEncoder(writer).Encode(resp)
+	err := json.NewEncoder(writer).Encode(resp)
+	if err != nil {
+		log.Error().
+			Caller().
+			Err(err).
+			Msg("Failed to encode response")
+	}
 }
 
 func (h *Headscale) handleNoiseMachineRegistrationNew(
@@ -370,7 +384,13 @@ func (h *Headscale) handleNoiseMachineRegistrationNew(
 
 	writer.Header().Set("Content-Type", "application/json")
 	writer.WriteHeader(http.StatusOK)
-	json.NewEncoder(writer).Encode(resp)
+	err := json.NewEncoder(writer).Encode(resp)
+	if err != nil {
+		log.Error().
+			Caller().
+			Err(err).
+			Msg("Failed to encode response")
+	}
 }
 
 func (h *Headscale) handleNoiseNodeLogOut(
@@ -430,7 +450,13 @@ func (h *Headscale) handleNoiseNodeRefreshKey(
 
 	writer.Header().Set("Content-Type", "application/json")
 	writer.WriteHeader(http.StatusOK)
-	json.NewEncoder(writer).Encode(resp)
+	err := json.NewEncoder(writer).Encode(resp)
+	if err != nil {
+		log.Error().
+			Caller().
+			Err(err).
+			Msg("Failed to encode response")
+	}
 }
 
 func (h *Headscale) handleNoiseNodeExpired(