From 694cff8e8e20fc83c025a396b1969f1d7bb99f15 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Tue, 3 May 2022 09:13:15 -0500 Subject: [PATCH] chore: add api specific 404 Prevents weird errors when routes are moved, like https://github.com/coder/coder/issues/1205 --- coderd/coderd.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/coderd/coderd.go b/coderd/coderd.go index e15f41ac6f19b..a3ff71788e633 100644 --- a/coderd/coderd.go +++ b/coderd/coderd.go @@ -69,6 +69,7 @@ func New(options *Options) (http.Handler, func()) { }) r := chi.NewRouter() + r.Use( func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -80,6 +81,12 @@ func New(options *Options) (http.Handler, func()) { ) r.Route("/api/v2", func(r chi.Router) { + r.NotFound(func(rw http.ResponseWriter, r *http.Request) { + httpapi.Write(rw, http.StatusNotFound, httpapi.Response{ + Message: "Route not found.", + }) + }) + r.Use( // Specific routes can specify smaller limits. httpmw.RateLimitPerMinute(options.APIRateLimit),