diff --git a/message_streams_test.go b/message_streams_test.go index 9d05e31..a81f817 100644 --- a/message_streams_test.go +++ b/message_streams_test.go @@ -101,6 +101,28 @@ func TestListMessageStreams(t *testing.T) { } } +func TestGetUnknownMessageStream(t *testing.T) { + responseJSON := `{"ErrorCode":1226,"Message":"The message stream for the provided 'ID' was not found."}` + + tMux.HandleFunc(pat.Get("/message-streams/unknown"), func(w http.ResponseWriter, req *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(responseJSON)) + }) + + res, err := client.GetMessageStream(context.Background(), "unknown") + if err == nil { + t.Fatalf("MessageStream: expected error") + } + if err.Error() != "The message stream for the provided 'ID' was not found." { + t.Fatalf("MessageStream: wrong error message (%s)", err.Error()) + } + + var zero MessageStream + if res != zero { + t.Fatalf("MessageStream: expected empty response") + } +} + func TestGetMessageStream(t *testing.T) { responseJSON := `{ "ID": "broadcasts", diff --git a/postmark.go b/postmark.go index 104df5c..77998dd 100644 --- a/postmark.go +++ b/postmark.go @@ -96,6 +96,17 @@ func (client *Client) doRequest(ctx context.Context, opts parameters, dst interf if err != nil { return } + + if res.StatusCode >= 400 { + // If the status code is not a success, attempt to unmarshall the body into the APIError struct. + var apiErr APIError + err = json.Unmarshal(body, &apiErr) + if err != nil { + return + } + return apiErr + } + err = json.Unmarshal(body, dst) return }