internal/httputil: add request id to error page (#144)

This commit is contained in:
Bobby DeSimone 2019-05-28 18:12:49 -07:00 committed by GitHub
parent 3d6471c4b3
commit f68338c888
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 29 deletions

View file

@ -6,6 +6,7 @@ import (
"io"
"net/http"
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/internal/templates"
)
@ -31,11 +32,16 @@ func CodeForError(err error) int {
// ErrorResponse renders an error page for errors given a message and a status code.
// If no message is passed, defaults to the text of the status code.
func ErrorResponse(rw http.ResponseWriter, req *http.Request, message string, code int) {
func ErrorResponse(rw http.ResponseWriter, r *http.Request, message string, code int) {
if message == "" {
message = http.StatusText(code)
}
if req.Header.Get("Accept") == "application/json" {
reqID := ""
id, ok := log.IDFromRequest(r)
if ok {
reqID = id
}
if r.Header.Get("Accept") == "application/json" {
var response struct {
Error string `json:"error"`
}
@ -45,13 +51,15 @@ func ErrorResponse(rw http.ResponseWriter, req *http.Request, message string, co
title := http.StatusText(code)
rw.WriteHeader(code)
t := struct {
Code int
Title string
Message string
Code int
Title string
Message string
RequestID string
}{
Code: code,
Title: title,
Message: message,
Code: code,
Title: title,
Message: message,
RequestID: reqID,
}
templates.New().ExecuteTemplate(rw, "error.html", t)
}

View file

@ -1,4 +1,4 @@
package middleware // import "github.com/pomerium/pomerium/internal/middleware"
package log // import "github.com/pomerium/pomerium/internal/log"
import (
"context"
@ -9,16 +9,9 @@ import (
"strings"
"time"
"github.com/pomerium/pomerium/internal/log"
"github.com/rs/zerolog"
)
// FromRequest gets the logger in the request's context.
// This is a shortcut for log.Ctx(r.Context())
func FromRequest(r *http.Request) *zerolog.Logger {
return log.Ctx(r.Context())
}
// NewHandler injects log into requests context.
func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {

View file

@ -1,4 +1,4 @@
package middleware // import "github.com/pomerium/pomerium/internal/middleware"
package log // import "github.com/pomerium/pomerium/internal/log"
import (
"bytes"

View file

@ -1,4 +1,4 @@
package middleware // import "github.com/pomerium/pomerium/internal/middleware"
package log // import "github.com/pomerium/pomerium/internal/log"
// The original work was derived from Goji's middleware, source:
// https://github.com/zenazn/goji/tree/master/web/middleware
@ -175,7 +175,6 @@ type http2FancyWriter struct {
func (f *http2FancyWriter) Flush() {
f.wroteHeader = true
fl := f.basicWriter.ResponseWriter.(http.Flusher)
fl.Flush()
}

View file

@ -1,4 +1,4 @@
package middleware
package log // import "github.com/pomerium/pomerium/internal/log"
import (
"net/http/httptest"

View file

@ -266,7 +266,9 @@ func New() *template.Template {
<h1 class="title">{{.Title}}</h1>
<section>
<p class="message">{{.Message}}.</p>
<p class="message">Troubleshoot your <a href="/.pomerium">session</a>.</p>
<p class="message">Troubleshoot your <a href="/.pomerium">session</a>.</br>
{{if .RequestID}} Request {{.RequestID}} {{end}}
</p>
</section>
</form>
</div>