refactor error handling logic

this is mostly a noop for now, but will make it easier to migrate to
using httputil.ReverseProxy.
This commit is contained in:
Will Norris 2017-06-17 11:22:02 -04:00
parent 1dbc725ae3
commit 5f3c970a20
2 changed files with 31 additions and 19 deletions

21
data.go
View file

@ -36,15 +36,18 @@ const (
optScaleUp = "scaleUp"
)
// urlError reports a malformed URL error.
type urlError struct {
type requestError struct {
message string
url *url.URL
status int
}
func (e urlError) Error() string {
return fmt.Sprintf("malformed URL %q: %s", e.url, e.message)
func (e requestError) Error() string { return e.message }
func (e requestError) StatusCode() int { return e.status }
func urlError(msg string, u *url.URL) error {
return requestError{fmt.Sprintf("malformed URL %q: %s", u, msg), http.StatusBadRequest}
}
func permissionError(msg string) error { return requestError{msg, http.StatusForbidden} }
// Options specifies transformations to be performed on the requested image.
type Options struct {
@ -270,13 +273,13 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
// first segment should be options
parts := strings.SplitN(path, "/", 2)
if len(parts) != 2 {
return nil, urlError{"too few path segments", r.URL}
return nil, urlError("too few path segments", r.URL)
}
var err error
req.URL, err = parseURL(parts[1])
if err != nil {
return nil, urlError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL}
return nil, urlError(fmt.Sprintf("unable to parse remote URL: %v", err), r.URL)
}
req.Options = ParseOptions(parts[0])
@ -287,11 +290,11 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
}
if !req.URL.IsAbs() {
return nil, urlError{"must provide absolute remote URL", r.URL}
return nil, urlError("must provide absolute remote URL", r.URL)
}
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
return nil, urlError{"remote URL must have http or https scheme", r.URL}
return nil, urlError("remote URL must have http or https scheme", r.URL)
}
// query string is always part of the remote URL