mirror of
https://github.com/pomerium/pomerium.git
synced 2025-06-05 12:23:03 +02:00
internal/httputil: add request id to error page (#144)
This commit is contained in:
parent
3d6471c4b3
commit
f68338c888
7 changed files with 31 additions and 29 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue