Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,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)
Expand Down
3 changes: 3 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
14 changes: 14 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func TestMixedViewers(t *testing.T) {
})
})

app.Get("/view404", func(c *Context) error {
return c.View(nil)
}, WithViewer()) // delete default viewer

app.Start()
defer app.Close()

Expand Down Expand Up @@ -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, */*")
Expand Down
3 changes: 2 additions & 1 deletion errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)