Skip to content

Commit

Permalink
[FRE-5394] Extra RawQuery and pass to caller in custom error struct. (#…
Browse files Browse the repository at this point in the history
…12)

* Added RawQuery to ErrorResponse
  • Loading branch information
funwithbots authored Jun 10, 2024
1 parent 41c0fd8 commit 06e1beb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func handleRequest[T any](client *http.Client, req *http.Request) (*T, error) {
}

resp.Error.Time = resp.Time
resp.Error.RawQuery = req.URL.RawQuery
return nil, resp.Error
}

Expand Down
17 changes: 9 additions & 8 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,13 @@ type Offer struct {
}

type ErrorResponse struct {
ID string `json:"id"`
Status int `json:"status"`
Code string `json:"code"`
Detail string `json:"detail"`
Meta ErrorMeta `json:"meta"`
Time *Timestamp `json:"-"`
ID string `json:"id"`
Status int `json:"status"`
Code string `json:"code"`
Detail string `json:"detail"`
Meta ErrorMeta `json:"meta"`
RawQuery string `json:"raw_query"`
Time *Timestamp `json:"-"`
}

func (er ErrorResponse) Error() string {
Expand All @@ -583,8 +584,8 @@ func (er ErrorResponse) Format(f fmt.State, c rune) {
e.Type, strings.Join(e.Loc, "."), e.Input, e.Message))
}

fmt.Fprintf(f, "error: %s (ID: %s; Status: %d; Code: %s); details: %s",
er.Detail, er.ID, er.Status, er.Code, strings.Join(meta, ", "))
fmt.Fprintf(f, "error: %s (ID: %s; Status: %d; Code: %s RawQuery: %s); details: %s",
er.Detail, er.ID, er.Status, er.Code, strings.Join(meta, ", "), er.RawQuery)
} else {
fmt.Fprint(f, er.Detail)
}
Expand Down
2 changes: 1 addition & 1 deletion types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestErrorResponse(t *testing.T) {

p := fmt.Sprintf("%+v", resp)
assert.Equal(t,
"error: Unable to validate request parameter(s) (ID: f49c8ffa-5ddc-4fbf-9841-6b3093c21eb2; Status: 422; Code: VALIDATION); details: (type [int_parsing]; loc [query.offset]; input [foo]; msg [Input should be a valid integer, unable to parse string as an integer])",
"error: Unable to validate request parameter(s) (ID: f49c8ffa-5ddc-4fbf-9841-6b3093c21eb2; Status: 422; Code: VALIDATION RawQuery: (type [int_parsing]; loc [query.offset]; input [foo]; msg [Input should be a valid integer, unable to parse string as an integer])); details: ",
p,
)
}
Expand Down

0 comments on commit 06e1beb

Please sign in to comment.