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) {