-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrorhandling.go
54 lines (46 loc) · 1.6 KB
/
errorhandling.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
53
54
package main
import (
"fmt"
"log"
"strings"
"github.com/ferluci/fast-realip"
"github.com/valyala/fasthttp"
)
func HandleDebug(ctx *fasthttp.RequestCtx, method, path string) {
if *debug == true {
log.Printf(
"- Debug: %s %s (%s) on %s\n",
realip.FromRequest(ctx),
method,
ctx.Request.Header.Peek("Auth"),
ctx.RequestURI(),
)
}
}
func HandleModifyFsFolder(ctx *fasthttp.RequestCtx) {
HandleGeneric(ctx, fasthttp.StatusMethodNotAllowed,
"Cannot "+string(ctx.Request.Header.Method())+" on path \""+fsFolder+"\"")
}
func HandleGeneric(ctx *fasthttp.RequestCtx, status int, message string) {
ctx.Response.SetStatusCode(status)
ctx.Response.Header.Set("X-Server-Message", message)
fmt.Fprintf(ctx, "%v %s\n", status, message)
}
func HandleForbidden(ctx *fasthttp.RequestCtx) {
ctx.Response.SetStatusCode(fasthttp.StatusForbidden)
ctx.Response.Header.Set("X-Server-Message", "403 Forbidden")
fmt.Fprint(ctx, "403 Forbidden\n")
log.Printf("- Returned 403 to %s - tried to connect with '%s' to '%s'",
realip.FromRequest(ctx), ctx.Request.Header.Peek("Auth"), ctx.Path())
}
func HandleInternalServerError(ctx *fasthttp.RequestCtx, err error) {
if strings.HasSuffix(err.Error(), "no such file or directory") {
HandleGeneric(ctx, fasthttp.StatusNotFound, "Not Found")
log.Printf("- Returned 404 to %s with error %v", realip.FromRequest(ctx), err)
return
}
ctx.Response.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.Response.Header.Set("X-Server-Message", "500 "+err.Error())
fmt.Fprintf(ctx, "500 %v\n", err)
log.Printf("- Returned 500 to %s with error %v", realip.FromRequest(ctx), err)
}