braintree / braintree_java

Braintree Java library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
158 stars 98 forks source link

Exception message is always null when Http status code is checked in Http#httpRequest() #66

Closed eanmclaughlin closed 5 years ago

eanmclaughlin commented 6 years ago

General information

Issue description

When Http#throwExceptionIfErrorStatusCode(int code, String message) is called in Http#httpRequest(), the message parameter is always null as seen here: https://github.com/braintree/braintree_java/blob/master/src/main/java/com/braintreegateway/util/Http.java#L150

This leads to difficult debugging due to a lack of information when attempting to access the thrown exception's message via Exception#getMessage(), particularly when the exception is an AuthorizationException, whose message is set to the variable decodedMessage, which is initialised to null and its value is only set if message is not null. See https://github.com/braintree/braintree_java/blob/master/src/main/java/com/braintreegateway/util/Http.java#L384 and https://github.com/braintree/braintree_java/blob/master/src/main/java/com/braintreegateway/util/Http.java#L399

We encountered this today in our system when changes to our merchant account and public/private keys resulted in HTTP 403 errors in requests to generate client tokens.

We log results with a log statement similar to log.error("Failed to get BraintreeToken. Error message: {}", e.getMessage()); which obviously was not very helpful in debugging.

One solution would be to pass the request's responseMessage from HttpURLConnection#getResponseMessage() to the call to throwExceptionIfErrorStatusCode(int, String), however that may not work well with the URLDecoder#decode() performed on message.

crookedneighbor commented 6 years ago

Thanks for the report. We'll look into this.

crookedneighbor commented 5 years ago

This is fixed in 2.84.0