Skip to content

Commit

Permalink
feat: on malformed error response, ensure more context
Browse files Browse the repository at this point in the history
  • Loading branch information
quoral committed Mar 1, 2024
1 parent 0e47b39 commit 880eaa8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions routingv8/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ type ResponseError struct {
}

func (r *ResponseError) Error() string {
if r.Response == nil || r.Response.Status == 0 {
return fmt.Sprintf(
"Response: %s StatusCode: %d",
r.HTTPBody,
r.HTTPStatusCode,
)
}
return fmt.Sprintf(
"Title: %v, Status: %d, Code: %v, Cause: %v, Action: %v",
r.Response.Title,
Expand Down
50 changes: 50 additions & 0 deletions routingv8/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package routingv8_test

import (
"testing"

"go.einride.tech/here/routingv8"
"gotest.tools/v3/assert"
)

func TestRouteError(t *testing.T) {

Check failure on line 10 in routingv8/client_test.go

View workflow job for this annotation

GitHub Actions / make

TestRouteError's subtests should call t.Parallel (tparallel)
t.Parallel()
for _, tt := range []*struct {
name string
expectedErrorString string
responseError routingv8.ResponseError
}{
{
name: "normally serialized",
responseError: routingv8.ResponseError{
Response: &routingv8.HereErrorResponse{
Title: "Unknown",
Status: 500,
Code: "ErrorCode",
Cause: "Random Cause",
Action: "Do it right",
},
},
expectedErrorString: "Title: Unknown, Status: 500, Code: ErrorCode, Cause: Random Cause, Action: Do it right",
},
{
name: "response not present",
responseError: routingv8.ResponseError{
HTTPBody: "{}",
},
expectedErrorString: "Response: {} StatusCode: 0",
},
{
name: "response malformed",
responseError: routingv8.ResponseError{
Response: &routingv8.HereErrorResponse{},
HTTPBody: "{}",
},
expectedErrorString: "Response: {} StatusCode: 0",
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.responseError.Error(), tt.expectedErrorString)
})
}
}

0 comments on commit 880eaa8

Please sign in to comment.