File size: 1,602 Bytes
ea775bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package api
import (
"errors"
"net/http"
"strconv"
"github.com/Resinat/Resin/internal/service"
)
func invalidArgumentError(message string) *service.ServiceError {
return &service.ServiceError{
Code: "INVALID_ARGUMENT",
Message: message,
}
}
func writeInvalidArgument(w http.ResponseWriter, message string) {
writeServiceError(w, invalidArgumentError(message))
}
func writePayloadTooLarge(w http.ResponseWriter, limit int64) {
msg := "request body too large"
if limit > 0 {
msg = "request body too large (max " + strconv.FormatInt(limit, 10) + " bytes)"
}
WriteError(w, http.StatusRequestEntityTooLarge, "PAYLOAD_TOO_LARGE", msg)
}
func writeDecodeBodyError(w http.ResponseWriter, err error) {
var tooLarge *requestBodyTooLargeError
if errors.As(err, &tooLarge) {
writePayloadTooLarge(w, tooLarge.Limit)
return
}
writeInvalidArgument(w, err.Error())
}
// writeServiceError maps service errors to HTTP response codes.
func writeServiceError(w http.ResponseWriter, err error) {
if err == nil {
WriteError(w, http.StatusInternalServerError, "INTERNAL", "internal server error")
return
}
var svcErr *service.ServiceError
if errors.As(err, &svcErr) {
var status int
switch svcErr.Code {
case "INVALID_ARGUMENT":
status = http.StatusBadRequest
case "NOT_FOUND":
status = http.StatusNotFound
case "CONFLICT":
status = http.StatusConflict
default:
status = http.StatusInternalServerError
}
WriteError(w, status, svcErr.Code, svcErr.Message)
return
}
WriteError(w, http.StatusInternalServerError, "INTERNAL", "internal server error")
}
|