-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a77b50e
commit d3a0cfc
Showing
3 changed files
with
106 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package graphql | ||
|
||
import "fmt" | ||
|
||
// RequestError represents an error building a request. | ||
type RequestError struct{ Err error } | ||
|
||
func (e *RequestError) Error() string { return fmt.Sprintf("request error: %v", e.Err) } | ||
func (e *RequestError) Unwrap() error { return e.Err } | ||
|
||
// OptionError represents an error modifiying a request. | ||
type OptionError struct{ Err error } | ||
|
||
func (e *OptionError) Error() string { return fmt.Sprintf("request option error: %v", e.Err) } | ||
func (e *OptionError) Unwrap() error { return e.Err } | ||
|
||
// ResponseError represents a response error, either with getting a response | ||
// from the server (eg. network error), or reading its body. | ||
type ResponseError struct{ Err error } | ||
|
||
func (e *ResponseError) Error() string { return fmt.Sprintf("request option error: %v", e.Err) } | ||
func (e *ResponseError) Unwrap() error { return e.Err } | ||
|
||
// ServerError indicates that the server returned a response but it was not what | ||
// we consider a successful one. | ||
type ServerError struct { | ||
Body []byte | ||
Status string | ||
} | ||
|
||
func (e *ServerError) Error() string { | ||
return fmt.Sprintf("non-200 OK status code: %v body: %q", e.Status, e.Body) | ||
} | ||
|
||
// BodyError indicates that the server responded with the right status code but | ||
// the body was unexpected and it did not parse as a valid GraphQL response. | ||
type BodyError struct { | ||
Body []byte | ||
Err error | ||
} | ||
|
||
func (e *BodyError) Error() string { | ||
return fmt.Sprintf("could not parse the body: %v, body: %q", e.Err, e.Body) | ||
} | ||
|
||
func (e *BodyError) Unwrap() error { return e.Err } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package graphql | ||
|
||
import "net/http" | ||
|
||
// RequestOption allows you to modify the request before sending it to the | ||
// server. | ||
type RequestOption func(*http.Request) error | ||
|
||
// WithHeader sets a header on the request. | ||
func WithHeader(key, value string) RequestOption { | ||
return func(r *http.Request) error { | ||
r.Header.Set(key, value) | ||
return nil | ||
} | ||
} |