Skip to content

Commit

Permalink
Add some error handling & what not
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrogen18 committed Mar 30, 2021
1 parent d6223e4 commit 4fdf805
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine:3.13
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY ./goosebin .
CMD ["/app/goosebin"]
22 changes: 18 additions & 4 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (rh *requestHandler) createPaste(rw http.ResponseWriter, req *http.Request)
val, err := rh.client.SetNX(req.Context(), k.redisKey(), paste, rh.pasteTtl).Result()
if err != nil {
fmt.Printf("%s %s error on redis setnx: %v\n", req.Method, req.URL.Path, err)
rh.serveError(rw, req, http.StatusInternalServerError, err)
return
}

Expand All @@ -83,7 +84,6 @@ func (rh *requestHandler) createPaste(rw http.ResponseWriter, req *http.Request)
}

vars["title"] = "Paste created"
rw.WriteHeader(http.StatusOK)
vars["pastePath"] = k.path()
rh.serveTemplate(rw, req, "createPaste", vars, http.StatusOK)
}
Expand Down Expand Up @@ -117,8 +117,8 @@ func (rh *requestHandler) rawPaste(rw http.ResponseWriter, req *http.Request) {

func loadPaste(ctx context.Context, client *redis.Client, pk pasteKey) (string, time.Duration, error) {
val, err := client.Get(ctx, pk.redisKey()).Result()
if err == redis.Nil {
return "", time.Duration(0), redis.Nil
if err != nil {
return "", time.Duration(0), err
}

ttl, err := client.TTL(ctx, pk.redisKey()).Result()
Expand Down Expand Up @@ -149,8 +149,8 @@ func (rh *requestHandler) showPaste(rw http.ResponseWriter, req *http.Request) {
}

if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%s %s error getting key from redis: %v\n", req.Method, req.URL.Path, err)
rh.serveError(rw, req, http.StatusInternalServerError, err)
return
}

Expand Down Expand Up @@ -267,6 +267,20 @@ func newRequestHandler() (*requestHandler, error) {
return result, nil
}

func (rh *requestHandler) serveError(rw http.ResponseWriter, req *http.Request, code int, err error){
fmt.Printf("%s %s failed: %v\n", req.Method, req.URL.Path, err)
rw.Header().Add("Cache-Control", "no-store, max-age=0")
rw.WriteHeader(code)
vars := make(map[string]interface{})
vars["title"] = "Server Error"
vars["code"] = code
vars["path"] = req.URL.Path
err = rh.t.ExecuteTemplate(rw, "error", vars)
if err != nil {
fmt.Printf("Could not render error template: %v\n", err)
}
}

func (rh *requestHandler) serveTemplate(rw http.ResponseWriter, req *http.Request, name string, vars map[string]interface{}, status int) {
rw.Header().Add("Content-Type", "text/html; charset=UTF-8")
rw.WriteHeader(status)
Expand Down
4 changes: 4 additions & 0 deletions internal/server/templates/error.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{define "error"}}<!DOCTYPE html><html>{{template "head" .}}<body>
<h3>{{.code}}&nbsp;{{.path}}</h3>
<p>A server error has occurred</p>
</body></html>{{end}}
2 changes: 1 addition & 1 deletion internal/server/templates/showPaste.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<li><a href="{{.rawPath}}">View as raw</a></li>
</ul>

<p><pre>{{.paste}}</pre></p>
<p><pre style="border-width:1px; border-color: black; border-style:solid; padding: 0.66em;">{{.paste}}</pre></p>

</body></html>{{end}}

0 comments on commit 4fdf805

Please sign in to comment.