Skip to content

Commit b04677b

Browse files
authored
feat(server): add error page and status code (#1099)
1 parent e4c902d commit b04677b

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

server/common/common.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package common
22

33
import (
44
"context"
5+
"fmt"
6+
"html"
7+
"net/http"
58
"strings"
69

710
"github.com/OpenListTeam/OpenList/v4/cmd/flags"
@@ -38,6 +41,41 @@ func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
3841
//c.Abort()
3942
}
4043

44+
// ErrorPage is used to return error page HTML.
45+
// It also returns standard HTTP status code.
46+
// @param l: if true, log error
47+
func ErrorPage(c *gin.Context, err error, code int, l ...bool) {
48+
49+
if len(l) > 0 && l[0] {
50+
if flags.Debug || flags.Dev {
51+
log.Errorf("%+v", err)
52+
} else {
53+
log.Errorf("%v", err)
54+
}
55+
}
56+
57+
codes := fmt.Sprintf("%d %s", code, http.StatusText(code))
58+
59+
html := fmt.Sprintf(`<!DOCTYPE html>
60+
<html lang="en">
61+
<head>
62+
<meta charset="utf-8" />
63+
<meta name="viewport" content="width=device-width, initial-scale=1" />
64+
<meta name="color-scheme" content="dark light" />
65+
<meta name="robots" content="noindex" />
66+
<title>%s</title>
67+
</head>
68+
<body>
69+
<h1>%s</h1>
70+
<hr>
71+
<p>%s</p>
72+
</body>
73+
</html>`,
74+
codes, codes, html.EscapeString(hidePrivacy(err.Error())))
75+
c.Data(code, "text/html; charset=utf-8", []byte(html))
76+
c.Abort()
77+
}
78+
4179
func ErrorWithDataResp(c *gin.Context, err error, code int, data interface{}, l ...bool) {
4280
if len(l) > 0 && l[0] {
4381
if flags.Debug || flags.Dev {

server/handles/archive.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func ArchiveDown(c *gin.Context) {
320320
filename := stdpath.Base(innerPath)
321321
storage, err := fs.GetStorage(archiveRawPath, &fs.GetStoragesArgs{})
322322
if err != nil {
323-
common.ErrorResp(c, err, 500)
323+
common.ErrorPage(c, err, 500)
324324
return
325325
}
326326
if common.ShouldProxy(storage, filename) {
@@ -340,7 +340,7 @@ func ArchiveDown(c *gin.Context) {
340340
InnerPath: innerPath,
341341
})
342342
if err != nil {
343-
common.ErrorResp(c, err, 500)
343+
common.ErrorPage(c, err, 500)
344344
return
345345
}
346346
redirect(c, link)
@@ -354,7 +354,7 @@ func ArchiveProxy(c *gin.Context) {
354354
filename := stdpath.Base(innerPath)
355355
storage, err := fs.GetStorage(archiveRawPath, &fs.GetStoragesArgs{})
356356
if err != nil {
357-
common.ErrorResp(c, err, 500)
357+
common.ErrorPage(c, err, 500)
358358
return
359359
}
360360
if canProxy(storage, filename) {
@@ -370,7 +370,7 @@ func ArchiveProxy(c *gin.Context) {
370370
InnerPath: innerPath,
371371
})
372372
if err != nil {
373-
common.ErrorResp(c, err, 500)
373+
common.ErrorPage(c, err, 500)
374374
return
375375
}
376376
proxy(c, link, file, storage.GetStorage().ProxyRange)
@@ -413,7 +413,7 @@ func ArchiveInternalExtract(c *gin.Context) {
413413
InnerPath: innerPath,
414414
})
415415
if err != nil {
416-
common.ErrorResp(c, err, 500)
416+
common.ErrorPage(c, err, 500)
417417
return
418418
}
419419
fileName := stdpath.Base(innerPath)

server/handles/down.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Down(c *gin.Context) {
2626
filename := stdpath.Base(rawPath)
2727
storage, err := fs.GetStorage(rawPath, &fs.GetStoragesArgs{})
2828
if err != nil {
29-
common.ErrorResp(c, err, 500)
29+
common.ErrorPage(c, err, 500)
3030
return
3131
}
3232
if common.ShouldProxy(storage, filename) {
@@ -40,7 +40,7 @@ func Down(c *gin.Context) {
4040
Redirect: true,
4141
})
4242
if err != nil {
43-
common.ErrorResp(c, err, 500)
43+
common.ErrorPage(c, err, 500)
4444
return
4545
}
4646
redirect(c, link)
@@ -52,7 +52,7 @@ func Proxy(c *gin.Context) {
5252
filename := stdpath.Base(rawPath)
5353
storage, err := fs.GetStorage(rawPath, &fs.GetStoragesArgs{})
5454
if err != nil {
55-
common.ErrorResp(c, err, 500)
55+
common.ErrorPage(c, err, 500)
5656
return
5757
}
5858
if canProxy(storage, filename) {
@@ -67,7 +67,7 @@ func Proxy(c *gin.Context) {
6767
Type: c.Query("type"),
6868
})
6969
if err != nil {
70-
common.ErrorResp(c, err, 500)
70+
common.ErrorPage(c, err, 500)
7171
return
7272
}
7373
proxy(c, link, file, storage.GetStorage().ProxyRange)
@@ -89,7 +89,7 @@ func redirect(c *gin.Context, link *model.Link) {
8989
}
9090
link.URL, err = utils.InjectQuery(link.URL, query)
9191
if err != nil {
92-
common.ErrorResp(c, err, 500)
92+
common.ErrorPage(c, err, 500)
9393
return
9494
}
9595
}
@@ -106,7 +106,7 @@ func proxy(c *gin.Context, link *model.Link, file model.Obj, proxyRange bool) {
106106
}
107107
link.URL, err = utils.InjectQuery(link.URL, query)
108108
if err != nil {
109-
common.ErrorResp(c, err, 500)
109+
common.ErrorPage(c, err, 500)
110110
return
111111
}
112112
}
@@ -149,9 +149,9 @@ func proxy(c *gin.Context, link *model.Link, file model.Obj, proxyRange bool) {
149149
log.Errorf("%s %s local proxy error: %+v", c.Request.Method, c.Request.URL.Path, err)
150150
} else {
151151
if statusCode, ok := errors.Unwrap(err).(net.ErrorHttpStatusCode); ok {
152-
common.ErrorResp(c, err, int(statusCode), true)
152+
common.ErrorPage(c, err, int(statusCode), true)
153153
} else {
154-
common.ErrorResp(c, err, 500, true)
154+
common.ErrorPage(c, err, 500, true)
155155
}
156156
}
157157
}

server/middlewares/down.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Down(verifyFunc func(string, string) error) func(c *gin.Context) {
2727
meta, err := op.GetNearestMeta(rawPath)
2828
if err != nil {
2929
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
30-
common.ErrorResp(c, err, 500, true)
30+
common.ErrorPage(c, err, 500, true)
3131
return
3232
}
3333
}
@@ -37,7 +37,7 @@ func Down(verifyFunc func(string, string) error) func(c *gin.Context) {
3737
s := c.Query("sign")
3838
err = verifyFunc(rawPath, strings.TrimSuffix(s, "/"))
3939
if err != nil {
40-
common.ErrorResp(c, err, 401)
40+
common.ErrorPage(c, err, 401)
4141
c.Abort()
4242
return
4343
}

0 commit comments

Comments
 (0)