1
0
mirror of https://github.com/strongdm/comply synced 2024-07-05 08:11:48 +00:00
comply/vendor/github.com/kevinburke/rest/ctx.go
Justin McCarthy 7468711b3b
go.mod: update gitlab dep to v0.30.1
Releases after github.com/xanzy/go-gitlab@v0.31.0 introduce breaking
changes to the NewClient call.

Addresses #94
2020-09-14 11:31:56 -07:00

44 lines
1011 B
Go

// +build go1.7
package rest
import (
"context"
"net/http"
)
type ctxVar int
var errCtx ctxVar = 0
var domainCtx ctxVar = 1
// CtxErr returns an error that's been stored in the Request context.
func CtxErr(r *http.Request) error {
val := r.Context().Value(errCtx)
if val == nil {
return nil
}
return val.(error)
}
// ctxSetErr sets err in the request context, and returns the new request.
func ctxSetErr(r *http.Request, err error) *http.Request {
return r.WithContext(context.WithValue(r.Context(), errCtx, err))
}
// CtxDomain returns a domain that's been set on the request. Use it to get the
// domain set on a 401 error handler.
func CtxDomain(r *http.Request) string {
val := r.Context().Value(domainCtx)
if val == nil {
return ""
}
return val.(string)
}
// ctxSetDomain sets a domain in the request context, and returns the new
// request.
func ctxSetDomain(r *http.Request, domain string) *http.Request {
return r.WithContext(context.WithValue(r.Context(), domainCtx, domain))
}