Azure / azure-notificationhubs-android

Android SDK and Samples for Azure Notification Hubs
Apache License 2.0
33 stars 63 forks source link

Updating Connection to throw NotificationHubExceptions with error responses #306

Closed zhangeugenia closed 5 months ago

zhangeugenia commented 5 months ago

Updating Connection.java to pass response bodies from requests which errored out (response codes greater than and including 400) into the NotificationHubException that is thrown: getInputStream() only contains the response body for successful requests, which resulted in NotificationHubExceptions being thrown with no message when the result code was 4xx+

Tested via sample app with a malformed request to the backend (template registration with invalid template). Before:

ex.toString():com.microsoft.windowsazure.messaging.NotificationHubException
ex.getStatusCode():400
ex.getMessage():null
e.printStackTrace():
com.microsoft.windowsazure.messaging.NotificationHubException
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:257)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:171)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:131)
    at com.microsoft.windowsazure.messaging.NotificationHub.upsertRegistrationInternal(NotificationHub.java:465)
    at com.microsoft.windowsazure.messaging.NotificationHub.registerInternal(NotificationHub.java:429)
    at com.microsoft.windowsazure.messaging.NotificationHub.registerTemplate(NotificationHub.java:201)
    ...

After:

ex.toString():com.microsoft.windowsazure.messaging.NotificationHubException: <Error xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Code>400</Code><Detail>Failed to parse BodyTemplate: Invalid template payload. TrackingId:d04313e8-c1e2-4def-881e-b55020b78cdc,TimeStamp:4/11/2024 5:41:42 AM +00:00</Detail></Error>
ex.getStatusCode():400
ex.getMessage():<Error xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Code>400</Code><Detail>Failed to parse BodyTemplate: Invalid template payload. TrackingId:d04313e8-c1e2-4def-881e-b55020b78cdc,TimeStamp:4/11/2024 5:41:42 AM +00:00</Detail></Error>
e.printStackTrace():
com.microsoft.windowsazure.messaging.NotificationHubException: <Error xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Code>400</Code><Detail>Failed to parse BodyTemplate: Invalid template payload. TrackingId:d04313e8-c1e2-4def-881e-b55020b78cdc,TimeStamp:4/11/2024 5:41:42 AM +00:00</Detail></Error>
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:257)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:171)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:131)
    at com.microsoft.windowsazure.messaging.NotificationHub.upsertRegistrationInternal(NotificationHub.java:465)
    at com.microsoft.windowsazure.messaging.NotificationHub.registerInternal(NotificationHub.java:429)
    at com.microsoft.windowsazure.messaging.NotificationHub.registerTemplate(NotificationHub.java:201)
    ...