pomerium/internal/httputil/errors.go
Bobby DeSimone 3eff6cce13
internal/sessions: make user state domain scoped
internal/sessions: session state is domain scoped
internal/sessions: infer csrf cookie, route scoped
proxy & authenticate: use shared cookie name
proxy & authenticate: prevent resaving unchanged session
proxy & authenticate: redirect instead of error for no session on login
internal/config: merge cookies
proxy: remove favicon specific route
proxy: use mock server for tests
proxy: add tests for failures
2019-05-20 20:44:05 -07:00

65 lines
1.7 KiB
Go

package httputil // import "github.com/pomerium/pomerium/internal/httputil"
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/pomerium/pomerium/internal/templates"
)
// HTTPError stores the status code and a message for a given HTTP error.
type HTTPError struct {
Code int
Message string
}
// Error fulfills the error interface, returning a string representation of the error.
func (h HTTPError) Error() string {
return fmt.Sprintf("%d %s: %s", h.Code, http.StatusText(h.Code), h.Message)
}
// CodeForError maps an error type and returns a corresponding http.Status
func CodeForError(err error) int {
switch err {
case ErrTokenRevoked:
return http.StatusUnauthorized
}
return http.StatusInternalServerError
}
// ErrorResponse renders an error page for errors given a message and a status code.
func ErrorResponse(rw http.ResponseWriter, req *http.Request, message string, code int) {
if req.Header.Get("Accept") == "application/json" {
var response struct {
Error string `json:"error"`
}
response.Error = message
writeJSONResponse(rw, code, response)
} else {
title := http.StatusText(code)
rw.WriteHeader(code)
t := struct {
Code int
Title string
Message string
}{
Code: code,
Title: title,
Message: message,
}
templates.New().ExecuteTemplate(rw, "error.html", t)
}
}
// writeJSONResponse is a helper that sets the application/json header and writes a response.
func writeJSONResponse(rw http.ResponseWriter, code int, response interface{}) {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(code)
err := json.NewEncoder(rw).Encode(response)
if err != nil {
io.WriteString(rw, err.Error())
}
}