Add error.templ
This commit is contained in:
parent
822bcb36ed
commit
47183d3cf9
|
@ -1,37 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>delphi.market</title>
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
|
||||
<link rel="manifest" href="/site.webmanifest" />
|
||||
<link rel="stylesheet" href="/css/index.css" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="theme-color" content="#091833" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header class="flex flex-row text-center justify-center pt-1">
|
||||
<nav>
|
||||
<a href="/">home</a>
|
||||
</nav>
|
||||
</header>
|
||||
<div class="container flex flex-column text-center">
|
||||
<code>
|
||||
<strong>
|
||||
<pre>
|
||||
_ _ ___ ___
|
||||
| || | / _ \ / _ \
|
||||
| || |_| | | | | | |
|
||||
|__ _| |_| | |_| |
|
||||
|_| \___/ \___/ </pre>
|
||||
</strong>
|
||||
</code>
|
||||
<div class="font-mono mb-1">Bad Request</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -1,37 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>delphi.market</title>
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
|
||||
<link rel="manifest" href="/site.webmanifest" />
|
||||
<link rel="stylesheet" href="/css/index.css" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="theme-color" content="#091833" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header class="flex flex-row text-center justify-center pt-1">
|
||||
<nav>
|
||||
<a href="/">home</a>
|
||||
</nav>
|
||||
</header>
|
||||
<div class="container flex flex-column text-center">
|
||||
<code>
|
||||
<strong>
|
||||
<pre>
|
||||
_ _ ___ _ _
|
||||
| || | / _ \| || |
|
||||
| || |_| | | | || |_
|
||||
|__ _| |_| |__ _|
|
||||
|_| \___/ |_| </pre>
|
||||
</strong>
|
||||
</code>
|
||||
<div class="font-mono mb-1">Not Found</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -1,37 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>delphi.market</title>
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
|
||||
<link rel="manifest" href="/site.webmanifest" />
|
||||
<link rel="stylesheet" href="/css/index.css" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="theme-color" content="#091833" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header class="flex flex-row text-center justify-center pt-1">
|
||||
<nav>
|
||||
<a href="/">home</a>
|
||||
</nav>
|
||||
</header>
|
||||
<div class="container flex flex-column text-center">
|
||||
<code>
|
||||
<strong>
|
||||
<pre>
|
||||
_ _ ___ ____
|
||||
| || | / _ \| ___|
|
||||
| || |_| | | |___ \
|
||||
|__ _| |_| |___) |
|
||||
|_| \___/|____/ </pre>
|
||||
</strong>
|
||||
</code>
|
||||
<div class="font-mono mb-1">Method Not Allowed</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -1,37 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>delphi.market</title>
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
|
||||
<link rel="manifest" href="/site.webmanifest" />
|
||||
<link rel="stylesheet" href="/css/index.css" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="theme-color" content="#091833" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header class="flex flex-row text-center justify-center pt-1">
|
||||
<nav>
|
||||
<a href="/">home</a>
|
||||
</nav>
|
||||
</header>
|
||||
<div class="container flex flex-column text-center">
|
||||
<code>
|
||||
<strong>
|
||||
<pre>
|
||||
____ ___ ___
|
||||
| ___| / _ \ / _ \
|
||||
|___ \| | | | | | |
|
||||
___) | |_| | |_| |
|
||||
|____/ \___/ \___/ </pre>
|
||||
</strong>
|
||||
</code>
|
||||
<div class="font-mono mb-1">Internal Server Error</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -1,20 +1,45 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/pages"
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func httpErrorHandler(err error, c echo.Context) {
|
||||
c.Logger().Error(err)
|
||||
code := http.StatusInternalServerError
|
||||
if httpError, ok := err.(*echo.HTTPError); ok {
|
||||
code = httpError.Code
|
||||
func httpErrorHandler(sc context.Context) echo.HTTPErrorHandler {
|
||||
return func(err error, c echo.Context) {
|
||||
var (
|
||||
code = http.StatusInternalServerError
|
||||
page = pages.Error
|
||||
buf *bytes.Buffer
|
||||
)
|
||||
|
||||
c.Logger().Error(err)
|
||||
|
||||
if httpError, ok := err.(*echo.HTTPError); ok {
|
||||
code = httpError.Code
|
||||
}
|
||||
|
||||
if strings.Contains(err.Error(), "violates check constraint") ||
|
||||
strings.Contains(err.Error(), "violates unique constraint") {
|
||||
code = 400
|
||||
}
|
||||
|
||||
buf = templ.GetBuffer()
|
||||
defer templ.ReleaseBuffer(buf)
|
||||
|
||||
if err = page(code).Render(context.RenderContext(sc, c), buf); err != nil {
|
||||
c.Logger().Error(err)
|
||||
code = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
if err = c.HTML(code, buf.String()); err != nil {
|
||||
c.Logger().Error(err)
|
||||
}
|
||||
}
|
||||
if strings.Contains(err.Error(), "violates check constraint") || strings.Contains(err.Error(), "violates unique constraint") {
|
||||
code = 400
|
||||
}
|
||||
c.JSON(code, map[string]any{"status": code})
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ templ Header() {
|
|||
<a href="/">home</a>
|
||||
</div>
|
||||
<div class="ms-auto">
|
||||
<a href="/about">about</a>
|
||||
if ctx.Value(c.SessionContextKey) != nil {
|
||||
<a href="/user">user</a>
|
||||
} else {
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package pages
|
||||
|
||||
import (
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lib"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/pages/components"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
templ Error(code int) {
|
||||
<html>
|
||||
@components.Head()
|
||||
<body class="container">
|
||||
@components.Header()
|
||||
<div class="flex flex-col text-center">
|
||||
<code class="mt-3">
|
||||
<strong>
|
||||
<pre>
|
||||
{ lib.Figlet("random", strconv.Itoa(code)) }
|
||||
</pre>
|
||||
</strong>
|
||||
</code>
|
||||
<div class="font-mono mb-3">{ http.StatusText(code) }</div>
|
||||
</div>
|
||||
@components.Footer()
|
||||
</body>
|
||||
</html>
|
||||
}
|
|
@ -32,7 +32,7 @@ func New(ctx Context) *Server {
|
|||
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
|
||||
}))
|
||||
|
||||
e.HTTPErrorHandler = httpErrorHandler
|
||||
e.HTTPErrorHandler = httpErrorHandler(ctx)
|
||||
|
||||
s = &Server{e}
|
||||
|
||||
|
|
Loading…
Reference in New Issue