summaryrefslogtreecommitdiff
path: root/internal/apierror/apierror.go
blob: e88781a4088d8a1b29a00cca1b73ae5dde6c21c2 (plain)
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
package apierror

import (
	"encoding/json"
	"net/http"
	"strconv"
)

type Error struct {
	Status  int
	Type    string
	Message string
}

func Write(w http.ResponseWriter, err Error, requestID string) {
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("X-AIGW-Request-ID", requestID)
	w.WriteHeader(err.Status)
	_ = json.NewEncoder(w).Encode(map[string]any{
		"error": map[string]string{
			"code":    strconv.Itoa(err.Status),
			"type":    err.Type,
			"message": err.Message,
		},
	})
}