You've already forked nyx
mirror of
https://github.com/rls-moe/nyx
synced 2025-08-19 06:18:38 +00:00
MVP, no mod tools or anything but it works
This commit is contained in:
77
http/errw/handler.go
Normal file
77
http/errw/handler.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package errw
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/GeertJohan/go.rice"
|
||||
"github.com/pressly/chi/middleware"
|
||||
"go.rls.moe/nyx/http/middle"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var riceConf = rice.Config{
|
||||
LocateOrder: []rice.LocateMethod{
|
||||
rice.LocateWorkingDirectory,
|
||||
rice.LocateEmbedded,
|
||||
rice.LocateAppended,
|
||||
},
|
||||
}
|
||||
|
||||
var box = riceConf.MustFindBox("http/errw/res/")
|
||||
|
||||
var (
|
||||
errorTmpl = template.New("errw/error")
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
errorTmpl, err = errorTmpl.Parse(box.MustString("error.html"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type ErrorWithTitle interface {
|
||||
error
|
||||
ErrorTitle() string
|
||||
}
|
||||
|
||||
type errorWTInt struct {
|
||||
message, title string
|
||||
}
|
||||
|
||||
func (e errorWTInt) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (e errorWTInt) ErrorTitle() string {
|
||||
return e.title
|
||||
}
|
||||
|
||||
func MakeErrorWithTitle(title, message string) ErrorWithTitle {
|
||||
return errorWTInt{message, title}
|
||||
}
|
||||
|
||||
func ErrorWriter(err error, w http.ResponseWriter, r *http.Request) {
|
||||
ctx := middle.GetBaseCtx(r)
|
||||
|
||||
if err == nil {
|
||||
ErrorWriter(errors.New("Unknonw Error"), w, r)
|
||||
}
|
||||
|
||||
if errWT, ok := err.(ErrorWithTitle); ok {
|
||||
ctx["Error"] = map[string]string{
|
||||
"Code": middleware.GetReqID(r.Context()),
|
||||
"Description": errWT.Error(),
|
||||
"Title": errWT.ErrorTitle(),
|
||||
}
|
||||
} else {
|
||||
ctx["Error"] = map[string]string{
|
||||
"Code": middleware.GetReqID(r.Context()),
|
||||
"Description": err.Error(),
|
||||
"Title": "Error",
|
||||
}
|
||||
}
|
||||
errorTmpl.Execute(w, ctx)
|
||||
return
|
||||
}
|
35
http/errw/res/error.html
Normal file
35
http/errw/res/error.html
Normal file
@@ -0,0 +1,35 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Config.Site.Title}} Admin Login</title>
|
||||
<style>
|
||||
div.error {
|
||||
border: 1px solid black;
|
||||
width: 500px;
|
||||
margin: auto;
|
||||
margin-top: 100px;
|
||||
}
|
||||
div.error h1 {
|
||||
margin-bottom: 0px;
|
||||
text-align: center;
|
||||
}
|
||||
div.error h2 {
|
||||
text-align: center;
|
||||
}
|
||||
div.error h3 {
|
||||
margin-top: 0px;
|
||||
text-align: center;
|
||||
color: #888;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="error">
|
||||
<h1>{{.Error.Title}}</h1><br/>
|
||||
<h3>{{.Error.Code}}</h3><br/>
|
||||
<h2>{{.Error.Description}}</h2>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user