Skip to content

Commit

Permalink
Add HTTP Status Code to Errors (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
cynicaljoy authored Jul 28, 2023
1 parent 0a542a4 commit 926039c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const httpStatusQueryTimeout = 440
// `message`, and any [fauna.QueryInfo].
type ErrFauna struct {
*QueryInfo
StatusCode int `json:"-"`
Code string `json:"code"`
Message string `json:"message"`
Abort any `json:"abort"`
Expand Down Expand Up @@ -62,7 +63,7 @@ type ErrInvalidRequest struct {
*ErrFauna
}

// An ErrNetwork is returned when an unknown error is encounted when attempting
// An ErrNetwork is returned when an unknown error is encountered when attempting
// to send a request to Fauna.
type ErrNetwork error

Expand Down Expand Up @@ -104,12 +105,18 @@ type ErrThrottling struct {
func getErrFauna(httpStatus int, res *queryResponse) error {
if res.Error != nil {
res.Error.QueryInfo = newQueryInfo(res)
res.Error.StatusCode = httpStatus
}

switch httpStatus {
case http.StatusBadRequest:
if res.Error == nil {
err := &ErrQueryRuntime{&ErrFauna{QueryInfo: newQueryInfo(res), Code: "", Message: ""}}
err := &ErrQueryRuntime{&ErrFauna{
QueryInfo: newQueryInfo(res),
Code: "",
Message: "",
StatusCode: httpStatus,
}}
err.Message += "\n" + res.Summary
return err
}
Expand Down
1 change: 1 addition & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestGetErrFauna(t *testing.T) {
err := getErrFauna(tt.args.httpStatus, res)
if tt.wantErr {
assert.ErrorAs(t, err, &tt.args.errType)
assert.NotZero(t, res.Error.StatusCode)
} else {
assert.NoError(t, err)
}
Expand Down

0 comments on commit 926039c

Please sign in to comment.