From 926039cd7a0736edcff84ffa9e87f6172b6364b5 Mon Sep 17 00:00:00 2001 From: Darren Cunningham Date: Fri, 28 Jul 2023 15:01:20 -0400 Subject: [PATCH] Add HTTP Status Code to Errors (#117) --- error.go | 11 +++++++++-- error_test.go | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/error.go b/error.go index 01f066c..5e92fb3 100644 --- a/error.go +++ b/error.go @@ -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"` @@ -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 @@ -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 } diff --git a/error_test.go b/error_test.go index a5db1e3..dd49fef 100644 --- a/error_test.go +++ b/error_test.go @@ -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) }