feat(server): add error page and status code (#1099)

This commit is contained in:
MadDogOwner
2025-08-19 15:18:12 +08:00
committed by GitHub
parent e4c902dd93
commit b04677b806
4 changed files with 53 additions and 15 deletions

View File

@ -2,6 +2,9 @@ package common
import (
"context"
"fmt"
"html"
"net/http"
"strings"
"github.com/OpenListTeam/OpenList/v4/cmd/flags"
@ -38,6 +41,41 @@ func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
//c.Abort()
}
// ErrorPage is used to return error page HTML.
// It also returns standard HTTP status code.
// @param l: if true, log error
func ErrorPage(c *gin.Context, err error, code int, l ...bool) {
if len(l) > 0 && l[0] {
if flags.Debug || flags.Dev {
log.Errorf("%+v", err)
} else {
log.Errorf("%v", err)
}
}
codes := fmt.Sprintf("%d %s", code, http.StatusText(code))
html := fmt.Sprintf(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="color-scheme" content="dark light" />
<meta name="robots" content="noindex" />
<title>%s</title>
</head>
<body>
<h1>%s</h1>
<hr>
<p>%s</p>
</body>
</html>`,
codes, codes, html.EscapeString(hidePrivacy(err.Error())))
c.Data(code, "text/html; charset=utf-8", []byte(html))
c.Abort()
}
func ErrorWithDataResp(c *gin.Context, err error, code int, data interface{}, l ...bool) {
if len(l) > 0 && l[0] {
if flags.Debug || flags.Dev {

View File

@ -320,7 +320,7 @@ func ArchiveDown(c *gin.Context) {
filename := stdpath.Base(innerPath)
storage, err := fs.GetStorage(archiveRawPath, &fs.GetStoragesArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
if common.ShouldProxy(storage, filename) {
@ -340,7 +340,7 @@ func ArchiveDown(c *gin.Context) {
InnerPath: innerPath,
})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
redirect(c, link)
@ -354,7 +354,7 @@ func ArchiveProxy(c *gin.Context) {
filename := stdpath.Base(innerPath)
storage, err := fs.GetStorage(archiveRawPath, &fs.GetStoragesArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
if canProxy(storage, filename) {
@ -370,7 +370,7 @@ func ArchiveProxy(c *gin.Context) {
InnerPath: innerPath,
})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
proxy(c, link, file, storage.GetStorage().ProxyRange)
@ -413,7 +413,7 @@ func ArchiveInternalExtract(c *gin.Context) {
InnerPath: innerPath,
})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
fileName := stdpath.Base(innerPath)

View File

@ -26,7 +26,7 @@ func Down(c *gin.Context) {
filename := stdpath.Base(rawPath)
storage, err := fs.GetStorage(rawPath, &fs.GetStoragesArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
if common.ShouldProxy(storage, filename) {
@ -40,7 +40,7 @@ func Down(c *gin.Context) {
Redirect: true,
})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
redirect(c, link)
@ -52,7 +52,7 @@ func Proxy(c *gin.Context) {
filename := stdpath.Base(rawPath)
storage, err := fs.GetStorage(rawPath, &fs.GetStoragesArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
if canProxy(storage, filename) {
@ -67,7 +67,7 @@ func Proxy(c *gin.Context) {
Type: c.Query("type"),
})
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
proxy(c, link, file, storage.GetStorage().ProxyRange)
@ -89,7 +89,7 @@ func redirect(c *gin.Context, link *model.Link) {
}
link.URL, err = utils.InjectQuery(link.URL, query)
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
}
@ -106,7 +106,7 @@ func proxy(c *gin.Context, link *model.Link, file model.Obj, proxyRange bool) {
}
link.URL, err = utils.InjectQuery(link.URL, query)
if err != nil {
common.ErrorResp(c, err, 500)
common.ErrorPage(c, err, 500)
return
}
}
@ -149,9 +149,9 @@ func proxy(c *gin.Context, link *model.Link, file model.Obj, proxyRange bool) {
log.Errorf("%s %s local proxy error: %+v", c.Request.Method, c.Request.URL.Path, err)
} else {
if statusCode, ok := errors.Unwrap(err).(net.ErrorHttpStatusCode); ok {
common.ErrorResp(c, err, int(statusCode), true)
common.ErrorPage(c, err, int(statusCode), true)
} else {
common.ErrorResp(c, err, 500, true)
common.ErrorPage(c, err, 500, true)
}
}
}

View File

@ -27,7 +27,7 @@ func Down(verifyFunc func(string, string) error) func(c *gin.Context) {
meta, err := op.GetNearestMeta(rawPath)
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true)
common.ErrorPage(c, err, 500, true)
return
}
}
@ -37,7 +37,7 @@ func Down(verifyFunc func(string, string) error) func(c *gin.Context) {
s := c.Query("sign")
err = verifyFunc(rawPath, strings.TrimSuffix(s, "/"))
if err != nil {
common.ErrorResp(c, err, 401)
common.ErrorPage(c, err, 401)
c.Abort()
return
}