Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Return on realm lookup error (#829)
Browse files Browse the repository at this point in the history
* Return on realm lookup error

* Return not-found instead
  • Loading branch information
sethvargo authored Oct 14, 2020
1 parent c47a42c commit a91a57c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/server/assets/400.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{define "400"}}
<!doctype html>
<html lang="en">
<head>
{{template "head" .}}
</head>

<body>
<main role="main" class="container mt-5">
<h1>Not found</h1>
<p>
The page you requested does not exist.
</p>
</main>
</body>
</html>
{{end}}
15 changes: 15 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ func InternalError(w http.ResponseWriter, r *http.Request, h *render.Renderer, e
}
}

// NotFound returns an error indicating the URL was not found.
func NotFound(w http.ResponseWriter, r *http.Request, h *render.Renderer) {
accept := strings.Split(r.Header.Get("Accept"), ",")
accept = append(accept, strings.Split(r.Header.Get("Content-Type"), ",")...)

switch {
case prefixInList(accept, ContentTypeHTML):
h.RenderHTMLStatus(w, http.StatusNotFound, "400", nil)
case prefixInList(accept, ContentTypeJSON):
h.RenderJSON(w, http.StatusNotFound, http.StatusText(http.StatusNotFound))
default:
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
}

// Unauthorized returns an error indicating the request was unauthorized.
func Unauthorized(w http.ResponseWriter, r *http.Request, h *render.Renderer) {
accept := strings.Split(r.Header.Get("Accept"), ",")
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/redirect/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/google/exposure-notifications-verification-server/pkg/database"
)

func (c *Controller) HandleIndex() http.Handler {
Expand All @@ -42,7 +43,13 @@ func (c *Controller) HandleIndex() http.Handler {
}
realm, err := c.db.FindRealmByRegion(hostRegion)
if err != nil {
if database.IsNotFound(err) {
controller.NotFound(w, r, c.h)
return
}

controller.InternalError(w, r, c.h, err)
return
}

// Get App Store Data.
Expand Down

0 comments on commit a91a57c

Please sign in to comment.