forked from focks/apibuildr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path404.go
35 lines (32 loc) · 857 Bytes
/
404.go
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
package apibuildr
import (
"encoding/json"
"fmt"
"github.com/pborman/uuid"
"net/http"
"strconv"
"strings"
)
func FourZeroFour() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.RequestURI
responseId := uuid.NewUUID().String()
headers := make([]string, 0)
for k, v := range r.Header {
header := fmt.Sprintf("%s:%s", k, strings.Join(v, ","))
headers = append(headers, header)
}
nf := ErrorResponse{
Message: fmt.Sprintf("requested path %s not present", path),
Code: "not found",
Api: r.RequestURI,
ResponseId: responseId,
Headers: headers,
}
response, _ := json.Marshal(&nf)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.WriteHeader(404)
_, _ = w.Write(response)
})
}