-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherrors.go
52 lines (42 loc) · 1.54 KB
/
errors.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"log"
"github.com/5HT2B/heartbeat/templates"
"github.com/ferluci/fast-realip"
"github.com/valyala/fasthttp"
)
func errorPageHandler(ctx *fasthttp.RequestCtx, code int, message string, plaintext bool) {
ctx.SetStatusCode(code)
log.Printf("- Returned %v to %s - tried to connect with %s to %s",
code, realip.FromRequest(ctx), ctx.Method(), ctx.Path())
if plaintext {
ctx.Response.Header.Set(fasthttp.HeaderContentType, "text/plain; charset=utf-8")
_, _ = fmt.Fprintf(ctx, "%s\n", message)
} else {
p := &templates.ErrorPage{
Message: message,
Path: ctx.Path(),
Method: ctx.Method(),
}
templates.WritePageTemplate(ctx, p)
}
}
func ErrorBadRequest(ctx *fasthttp.RequestCtx, plaintext bool) {
errorPageHandler(ctx, fasthttp.StatusBadRequest, "400 Bad Request", plaintext)
}
func ErrorForbidden(ctx *fasthttp.RequestCtx, plaintext bool) {
errorPageHandler(ctx, fasthttp.StatusForbidden, "403 Forbidden", plaintext)
}
func ErrorNotFound(ctx *fasthttp.RequestCtx, plaintext bool) {
errorPageHandler(ctx, fasthttp.StatusNotFound, "404 Not Found", plaintext)
}
func HandleInternalErr(ctx *fasthttp.RequestCtx, message string, err error) {
errorPageHandler(ctx, fasthttp.StatusInternalServerError, "500 "+message+": "+err.Error(), true)
}
func HandleClientErr(ctx *fasthttp.RequestCtx, message string, err error) {
errorPageHandler(ctx, fasthttp.StatusBadRequest, "400 "+message+": "+err.Error(), true)
}
func HandleSuccess(ctx *fasthttp.RequestCtx) {
errorPageHandler(ctx, fasthttp.StatusOK, "200 OK", true)
}