zero: merge pomerium/zero-sdk (#4848)

This commit is contained in:
Denis Mishin 2023-12-11 17:31:39 -05:00 committed by GitHub
parent c4dd965f2d
commit ea64902a73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 4800 additions and 170 deletions

View file

@ -0,0 +1,42 @@
package apierror
import (
"fmt"
"net/http"
)
// RequestIDError is an error that wraps another error and includes the response ID
type RequestIDError struct {
Err error
ResponseID *string
}
// Error implements error for RequestIDError
func (e *RequestIDError) Error() string {
if e.ResponseID == nil {
return e.Err.Error()
}
return fmt.Sprintf("[x-response-id:%s]: %v", *e.ResponseID, e.Err)
}
// Unwrap implements errors.Unwrap for RequestIDError
func (e *RequestIDError) Unwrap() error {
return e.Err
}
// Is implements errors.Is for RequestIDError
func (e *RequestIDError) Is(err error) bool {
//nolint:errorlint
_, ok := err.(*RequestIDError)
return ok
}
// WithRequestID creates a new RequestIDError
func WithRequestID(err error, headers http.Header) *RequestIDError {
r := &RequestIDError{Err: err}
id := headers.Get("X-Response-Id")
if id != "" {
r.ResponseID = &id
}
return r
}