Skip to content

Commit de0cf3e

Browse files
authored
Add error decoding support for content type parameters such as charset (#1202)
1 parent c84b33c commit de0cf3e

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

cmd/cmd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"net/http"
78
"regexp"
@@ -26,7 +27,7 @@ var (
2627
// Err is used to write errors.
2728
Err io.Writer
2829
// jsonContentTypeRe is used to match Content-Type which contains JSON.
29-
jsonContentTypeRe = regexp.MustCompile(`^application/([[:alpha:]]+\+)?json$`)
30+
jsonContentTypeRe = regexp.MustCompile(`^application/([[:alpha:]]+\+)?json($|;)`)
3031
)
3132

3233
const msgWelcomePleaseConfigure = `
@@ -122,7 +123,7 @@ func decodedAPIError(resp *http.Response) error {
122123
strings.Join(apiError.Error.PossibleTrackIDs, ", "),
123124
)
124125
}
125-
return fmt.Errorf(apiError.Error.Message)
126+
return errors.New(apiError.Error.Message)
126127
}
127128
return fmt.Errorf("unexpected API response: %d", resp.StatusCode)
128129
}

cmd/cmd_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ func TestDecodeErrorResponse(t *testing.T) {
162162
response: errorResponse418("application/json", `{"error": {"type": "json", "valid": no}}`),
163163
wantMessage: "failed to parse API error response: invalid character 'o' in literal null (expecting 'u')",
164164
},
165+
{
166+
response: errorResponse418("application/json; charset=utf-8", `{"error": {"type": "track_ambiguous", "message": "message", "possible_track_ids": ["a", "b"]}}`),
167+
wantMessage: "message: a, b",
168+
},
165169
{
166170
response: errorResponse418("application/json", `{"error": {"type": "track_ambiguous", "message": "message", "possible_track_ids": ["a", "b"]}}`),
167171
wantMessage: "message: a, b",

0 commit comments

Comments
 (0)