Skip to content

Commit

Permalink
✨ feature (models): Created a simple helper function to write HTTP er…
Browse files Browse the repository at this point in the history
…ror data in JSON format
  • Loading branch information
kevinmarquesp committed Jul 20, 2024
1 parent 195bfbf commit be9bc8f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/models/errors.go
Original file line number Diff line number Diff line change
@@ -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(),
})
}

0 comments on commit be9bc8f

Please sign in to comment.