From be9bc8fbf6331255678a283aedea380c2fa9f523 Mon Sep 17 00:00:00 2001 From: Kevin Marques Date: Sat, 20 Jul 2024 11:35:53 -0300 Subject: [PATCH] :sparkles: feature (models): Created a simple helper function to write HTTP error data in JSON format --- internal/models/errors.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 internal/models/errors.go diff --git a/internal/models/errors.go b/internal/models/errors.go new file mode 100644 index 0000000..1e6c52d --- /dev/null +++ b/internal/models/errors.go @@ -0,0 +1,26 @@ +package models + +import ( + "encoding/json" + "net/http" +) + +type JsonError struct { + Status int `json:"status"` + StatusText string `json:"statusText"` + Message string `json:"message"` + Location string `json:"location"` + Error string `json:"error"` +} + +func WriteHttpJsonError(w http.ResponseWriter, status int, err error, location, message string) { + w.WriteHeader(status) + + json.NewEncoder(w).Encode(JsonError{ + Status: status, + StatusText: http.StatusText(status), + Message: message, + Location: location, + Error: err.Error(), + }) +}