From eb07c987302b477b6c2e453e9fe65c9dae8c36a6 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Mon, 26 Feb 2024 21:50:51 +0200 Subject: [PATCH] Make sure we handle connection error properly for WAC --- handlers/meta/handlers.go | 6 +++++- handlers/meta/whataspp_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/handlers/meta/handlers.go b/handlers/meta/handlers.go index bdd985e3d..27ce1cd4a 100644 --- a/handlers/meta/handlers.go +++ b/handlers/meta/handlers.go @@ -1071,7 +1071,11 @@ func (h *handler) requestWAC(payload whatsapp.SendRequest, accessToken string, r req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") - _, respBody, _ := h.RequestHTTP(req, clog) + resp, respBody, err := h.RequestHTTP(req, clog) + if err != nil || resp.StatusCode/100 == 5 { + return courier.ErrConnectionFailed + } + respPayload := &whatsapp.SendResponse{} err = json.Unmarshal(respBody, respPayload) if err != nil { diff --git a/handlers/meta/whataspp_test.go b/handlers/meta/whataspp_test.go index ddc85566e..a52b93cef 100644 --- a/handlers/meta/whataspp_test.go +++ b/handlers/meta/whataspp_test.go @@ -615,6 +615,17 @@ var whatsappOutgoingTests = []OutgoingTestCase{ }, ExpectedError: courier.ErrFailedWithReason("130429", "(#130429) Rate limit hit"), }, + { + Label: "Error Connection", + MsgText: "Error", + MsgURN: "whatsapp:250788123123", + MockResponses: map[string][]*httpx.MockResponse{ + "*/12345_ID/messages": { + httpx.NewMockResponse(500, nil, []byte(`Bad Gateway`)), + }, + }, + ExpectedError: courier.ErrConnectionFailed, + }, } func TestWhatsAppOutgoing(t *testing.T) {