From 4b5e5161dd72ece75104396816abd76afe8713bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Johan=20Ansari-=C3=96nnestam?= Date: Fri, 1 Mar 2024 13:53:28 +0100 Subject: [PATCH] feat: on malformed error response, ensure more context --- routingv8/client.go | 7 +++++ routingv8/client_test.go | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 routingv8/client_test.go diff --git a/routingv8/client.go b/routingv8/client.go index 0bfb1ae..fd823b3 100644 --- a/routingv8/client.go +++ b/routingv8/client.go @@ -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, diff --git a/routingv8/client_test.go b/routingv8/client_test.go new file mode 100644 index 0000000..4d1975a --- /dev/null +++ b/routingv8/client_test.go @@ -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) + }) + } +}