From 1ba71b8d8248a4d872c2572d45ea11062a530d68 Mon Sep 17 00:00:00 2001 From: Lz Date: Sun, 9 Feb 2025 09:43:05 +0800 Subject: [PATCH 1/3] fix(context): throw ErrViewNotFound when no view is avaiable --- app.go | 6 ++++++ context.go | 3 +++ errs.go | 3 ++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 5ddece5..adcb9f0 100644 --- a/app.go +++ b/app.go @@ -233,6 +233,12 @@ func (app *App) HandleFile(name string, v *FileViewer) { return } + if errors.Is(err, ErrViewNotFound) { + ctx.WriteStatus(http.StatusNotFound) + ctx.Response.Write([]byte("View Not Found")) // nolint: errcheck + return + } + logID := nextLogID() ctx.WriteHeader("X-Log-Id", logID) ctx.WriteStatus(http.StatusInternalServerError) diff --git a/context.go b/context.go index 9fe0c5d..cf45a1a 100644 --- a/context.go +++ b/context.go @@ -74,6 +74,9 @@ func (c *Context) View(data any, options ...string) error { // no any viewer is matched if !ok { if v == nil { + if len(c.Routing.Viewers) == 0 { + return ErrViewNotFound + } v = c.Routing.Viewers[0] // use the first viewer as a fallback when no viewer is matched or specified by name } } diff --git a/errs.go b/errs.go index 051b388..87f41a8 100644 --- a/errs.go +++ b/errs.go @@ -3,5 +3,6 @@ package xun import "errors" var ( - ErrCancelled = errors.New("xun: request_cancelled") + ErrCancelled = errors.New("xun: request_cancelled") + ErrViewNotFound = errors.New("xun: view_not_found") ) From 9661d4c0c26cb2b267fabfe593515ec8a7ed367c Mon Sep 17 00:00:00 2001 From: Lz Date: Sun, 9 Feb 2025 09:55:23 +0800 Subject: [PATCH 2/3] fix(context): added tests for ErrViewNotFound --- app.go | 12 ++++++------ context_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app.go b/app.go index adcb9f0..4ed1151 100644 --- a/app.go +++ b/app.go @@ -233,12 +233,6 @@ func (app *App) HandleFile(name string, v *FileViewer) { return } - if errors.Is(err, ErrViewNotFound) { - ctx.WriteStatus(http.StatusNotFound) - ctx.Response.Write([]byte("View Not Found")) // nolint: errcheck - return - } - logID := nextLogID() ctx.WriteHeader("X-Log-Id", logID) ctx.WriteStatus(http.StatusInternalServerError) @@ -395,6 +389,12 @@ func (app *App) createHandler(pattern string, hf HandleFunc, opts []RoutingOptio return } + if errors.Is(err, ErrViewNotFound) { + ctx.WriteStatus(http.StatusNotFound) + ctx.Response.Write([]byte("View Not Found")) // nolint: errcheck + return + } + logID := nextLogID() ctx.WriteHeader("X-Log-Id", logID) ctx.WriteStatus(http.StatusInternalServerError) diff --git a/context_test.go b/context_test.go index 1fb5d83..3c97ea7 100644 --- a/context_test.go +++ b/context_test.go @@ -112,6 +112,10 @@ func TestMixedViewers(t *testing.T) { }) }) + app.Get("/view404", func(c *Context) error { + return c.View(nil) + }, WithViewer()) //deleted default viewer + app.Start() defer app.Close() @@ -140,6 +144,16 @@ func TestMixedViewers(t *testing.T) { resp.Body.Close() }) + t.Run("view_not_found", func(t *testing.T) { + req, err := http.NewRequest("GET", srv.URL+"/view404", nil) + req.Header.Set("Accept", "text/html") + require.NoError(t, err) + resp, err := client.Do(req) + require.NoError(t, err) + require.Equal(t, http.StatusNotFound, resp.StatusCode) + resp.Body.Close() + }) + t.Run("data_bind_with_html_and_json_should_both_work", func(t *testing.T) { req, err := http.NewRequest("GET", srv.URL+"/list", nil) req.Header.Set("Accept", "text/html, */*") From fadd7997f52e70ba1f7ed959c3600f5eefe3af40 Mon Sep 17 00:00:00 2001 From: Lz Date: Sun, 9 Feb 2025 09:59:52 +0800 Subject: [PATCH 3/3] fix(context): fixed deepsource warning --- context_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 3c97ea7..49b206d 100644 --- a/context_test.go +++ b/context_test.go @@ -114,7 +114,7 @@ func TestMixedViewers(t *testing.T) { app.Get("/view404", func(c *Context) error { return c.View(nil) - }, WithViewer()) //deleted default viewer + }, WithViewer()) // delete default viewer app.Start() defer app.Close()