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 4b5e516
Show file tree
Hide file tree
Showing 2 changed files with 67 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
60 changes: 60 additions & 0 deletions routingv8/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package routingv8_test

import (
"testing"

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

func TestRouteError(t *testing.T) {
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",
},
{
name: "response with 0 status code",
responseError: routingv8.ResponseError{
Response: &routingv8.HereErrorResponse{
Status: 0,
},
HTTPBody: "{}",
},
expectedErrorString: "Response: {} StatusCode: 0",
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.responseError.Error(), tt.expectedErrorString)
})
}
}

0 comments on commit 4b5e516

Please sign in to comment.