From 3442a92f359b35a76244dfc4f2e55a34d4ec1b2b Mon Sep 17 00:00:00 2001
From: Alexander Neonxp Kiryukhin
Date: Sun, 6 Oct 2024 17:47:06 +0300
Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D1=81=D1=81=D1=8B?=
=?UTF-8?q?=D0=BB=D0=BE=D0=BA=20+=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE?=
=?UTF-8?q?=D1=82=D0=BA=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
cmd/app/serve/serve.go | 4 ++-
pkg/handler/error.go | 14 ++++++++
pkg/handler/quote.go | 2 +-
pkg/tpl/error.templ | 8 +++++
pkg/tpl/error_templ.go | 71 +++++++++++++++++++++++++++++++++++++++++
pkg/tpl/layout.templ | 3 +-
pkg/tpl/layout_templ.go | 2 +-
pkg/tpl/quote.templ | 6 ++--
pkg/tpl/quote_templ.go | 6 ++--
9 files changed, 106 insertions(+), 10 deletions(-)
create mode 100644 pkg/handler/error.go
create mode 100644 pkg/tpl/error.templ
create mode 100644 pkg/tpl/error_templ.go
diff --git a/cmd/app/serve/serve.go b/cmd/app/serve/serve.go
index dcc296b..0609083 100644
--- a/cmd/app/serve/serve.go
+++ b/cmd/app/serve/serve.go
@@ -25,12 +25,14 @@ func Run(c *cli.Context) error {
e := echo.New()
+ e.HTTPErrorHandler = handler.ErrorHandler
+
e.Use(middleware.Recover())
e.Use(middleware.Logger())
e.GET("/", h.Index)
- e.GET("/random", h.Random)
e.GET("/quote/:id", h.Quote)
+ e.GET("/random", h.Random)
e.GET("/add", h.AddQuote)
e.POST("/add", h.AddQuotePost)
e.GET("/add/success", h.AddQuoteSuccess)
diff --git a/pkg/handler/error.go b/pkg/handler/error.go
new file mode 100644
index 0000000..f74be59
--- /dev/null
+++ b/pkg/handler/error.go
@@ -0,0 +1,14 @@
+package handler
+
+import (
+ "log"
+
+ "github.com/labstack/echo/v4"
+ "sh.org.ru/pkg/tpl"
+)
+
+func ErrorHandler(err error, c echo.Context) {
+ if err := tpl.ErrorPage(err.Error()).Render(c.Request().Context(), c.Response()); err != nil {
+ log.Println(err)
+ }
+}
diff --git a/pkg/handler/quote.go b/pkg/handler/quote.go
index af9fd82..2a5f7e6 100644
--- a/pkg/handler/quote.go
+++ b/pkg/handler/quote.go
@@ -12,7 +12,7 @@ func (h *Handler) Quote(c echo.Context) error {
sid := c.Param("id")
id, err := strconv.Atoi(sid)
if err != nil {
- return err
+ return echo.ErrNotFound
}
quote := new(model.Quote)
diff --git a/pkg/tpl/error.templ b/pkg/tpl/error.templ
new file mode 100644
index 0000000..8045c1a
--- /dev/null
+++ b/pkg/tpl/error.templ
@@ -0,0 +1,8 @@
+package tpl
+
+templ ErrorPage(err string) {
+ @Layout(HeaderParams{}) {
+ Ошибка!
+
{ err }
+ } +} diff --git a/pkg/tpl/error_templ.go b/pkg/tpl/error_templ.go new file mode 100644 index 0000000..cc13ddd --- /dev/null +++ b/pkg/tpl/error_templ.go @@ -0,0 +1,71 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.2.778 +package tpl + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import templruntime "github.com/a-h/templ/runtime" + +func ErrorPage(err string) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(err) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `pkg/tpl/error.templ`, Line: 6, Col: 10} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = Layout(HeaderParams{}).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} + +var _ = templruntime.GeneratedTemplate diff --git a/pkg/tpl/layout.templ b/pkg/tpl/layout.templ index fc9314e..2c41a0d 100644 --- a/pkg/tpl/layout.templ +++ b/pkg/tpl/layout.templ @@ -22,8 +22,9 @@ templ Layout(params HeaderParams) {