Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 404 instead of 500 if a route was not found #224

Merged
merged 4 commits into from
May 12, 2021
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
14 changes: 7 additions & 7 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
},
{
testRequest{http.MethodGet, "http://anyserver:8080/app"},
expectation{http.StatusInternalServerError, []byte("<html>configuration error</html>\n"), http.Header{"Couper-Error": {"configuration error"}}, ""},
expectation{http.StatusNotFound, []byte("<html>route not found error</html>\n"), http.Header{"Couper-Error": {"route not found error"}}, ""},
},
}},
{"files/01_couper.hcl", []requestCase{
Expand All @@ -212,15 +212,15 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
},
{
testRequest{http.MethodGet, "http://couper.io:9898/"},
expectation{http.StatusInternalServerError, []byte("<html>configuration error</html>\n"), nil, ""},
expectation{http.StatusNotFound, []byte("<html>route not found error</html>\n"), http.Header{"Couper-Error": {"route not found error"}}, ""},
},
{
testRequest{http.MethodGet, "http://example.com:9898/b"},
expectation{http.StatusOK, []byte(`<html lang="en">index B</html>`), nil, "file"},
},
{
testRequest{http.MethodGet, "http://example.com:9898/"},
expectation{http.StatusInternalServerError, []byte("<html>configuration error</html>\n"), nil, ""},
expectation{http.StatusNotFound, []byte("<html>route not found error</html>\n"), http.Header{"Couper-Error": {"route not found error"}}, ""},
},
}},
{"files_spa_api/01_couper.hcl", []requestCase{
Expand All @@ -236,7 +236,7 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
{"api/01_couper.hcl", []requestCase{
{
testRequest{http.MethodGet, "http://anyserver:8080/"},
expectation{http.StatusInternalServerError, []byte("<html>configuration error</html>\n"), http.Header{"Couper-Error": {"configuration error"}}, ""},
expectation{http.StatusNotFound, []byte("<html>route not found error</html>\n"), http.Header{"Couper-Error": {"route not found error"}}, ""},
},
{
testRequest{http.MethodGet, "http://anyserver:8080/v1"},
Expand All @@ -256,13 +256,13 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
},
{
testRequest{http.MethodGet, "http://anyserver:8080/v1x"},
expectation{http.StatusInternalServerError, []byte("<html>configuration error</html>\n"), http.Header{"Content-Type": {"text/html"}}, ""},
expectation{http.StatusNotFound, []byte("<html>route not found error</html>\n"), http.Header{"Couper-Error": {"route not found error"}}, ""},
},
}},
{"api/02_couper.hcl", []requestCase{
{
testRequest{http.MethodGet, "http://anyserver:8080/"},
expectation{http.StatusInternalServerError, []byte("<html>configuration error</html>\n"), http.Header{"Couper-Error": {"configuration error"}}, ""},
expectation{http.StatusNotFound, []byte("<html>route not found error</html>\n"), http.Header{"Couper-Error": {"route not found error"}}, ""},
},
{
testRequest{http.MethodGet, "http://anyserver:8080/v2/"},
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestHTTPServer_HostHeader2(t *testing.T) {

_ = res.Body.Close()

if string(resBytes) != "<html>configuration error</html>\n" {
if string(resBytes) != "<html>route not found error</html>\n" {
t.Errorf("%s", resBytes)
}

Expand Down
4 changes: 2 additions & 2 deletions server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func TestHTTPServer_ServeHTTP_Files(t *testing.T) {
expectedBody []byte
expectedStatus int
}{
{"/", []byte("<html><body><h1>configuration error: My custom error template</h1></body></html>"), http.StatusInternalServerError},
{"/apps/", []byte("<html><body><h1>configuration error: My custom error template</h1></body></html>"), http.StatusInternalServerError},
{"/", []byte("<html><body><h1>route not found error: My custom error template</h1></body></html>"), http.StatusNotFound},
{"/apps/", []byte("<html><body><h1>route not found error: My custom error template</h1></body></html>"), http.StatusNotFound},
{"/apps/shiny-product/", []byte("<html><body><h1>route not found error: My custom error template</h1></body></html>"), http.StatusNotFound},
{"/apps/shiny-product/assets/", []byte("<html><body><h1>route not found error: My custom error template</h1></body></html>"), http.StatusNotFound},
{"/apps/shiny-product/app/", spaContent, http.StatusOK},
Expand Down
6 changes: 1 addition & 5 deletions server/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,8 @@ func (m *Mux) FindHandler(req *http.Request) http.Handler {
return fileHandler
}

if isConfigured(m.opts.ServerOptions.FilesBasePath) && matchesPath(m.opts.ServerOptions.FilesBasePath, req.URL.Path) {
return m.opts.ServerOptions.FilesErrTpl.ServeError(errors.RouteNotFound)
}

// Fallback
return m.opts.ServerOptions.ServerErrTpl.ServeError(errors.Configuration)
return m.opts.ServerOptions.ServerErrTpl.ServeError(errors.RouteNotFound)
}
}

Expand Down