Skip to content

Commit

Permalink
fix: ensure error response body stream is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy committed Apr 21, 2022
1 parent a4321fe commit f949d85
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
Expand Up @@ -14,6 +14,7 @@
import org.apache.commons.lang3.exception.ContextedRuntimeException;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@RequiredArgsConstructor
Expand All @@ -39,8 +40,8 @@ public class TwitchExtensionsErrorDecoder implements ErrorDecoder {
public Exception decode(String methodKey, Response response) {
Exception ex;

try {
String responseBody = response.body() == null ? "" : IOUtils.toString(response.body().asInputStream(), StandardCharsets.UTF_8.name());
try (InputStream is = response.body() == null ? null : response.body().asInputStream()) {
String responseBody = is == null ? "" : IOUtils.toString(is, StandardCharsets.UTF_8);

if (response.status() == 401) {
ex = new UnauthorizedException()
Expand All @@ -53,7 +54,7 @@ public Exception decode(String methodKey, Response response) {
.addContextValue("requestMethod", response.request().httpMethod())
.addContextValue("responseBody", responseBody);
} else if (response.status() == 429) {
ex = new ContextedRuntimeException("To many requests!")
ex = new ContextedRuntimeException("Too many requests!")
.addContextValue("requestUrl", response.request().url())
.addContextValue("requestMethod", response.request().httpMethod())
.addContextValue("responseBody", responseBody);
Expand All @@ -70,7 +71,8 @@ public Exception decode(String methodKey, Response response) {
.addContextValue("responseBody", responseBody)
.addContextValue("errorType", error.getError())
.addContextValue("errorStatus", error.getStatus())
.addContextValue("errorType", error.getMessage());
.addContextValue("errorType", error.getMessage())
.addContextValue("errorMessage", error.getMessage());
}
} catch (IOException fallbackToDefault) {
ex = defaultDecoder.decode(methodKey, response);
Expand Down
Expand Up @@ -17,6 +17,7 @@
import org.apache.commons.lang3.exception.ContextedRuntimeException;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
Expand Down Expand Up @@ -56,8 +57,8 @@ public TwitchHelixErrorDecoder(Decoder decoder, TwitchHelixRateLimitTracker rate
public Exception decode(String methodKey, Response response) {
Exception ex;

try {
String responseBody = response.body() == null ? "" : IOUtils.toString(response.body().asInputStream(), StandardCharsets.UTF_8.name());
try (InputStream is = response.body() == null ? null : response.body().asInputStream()) {
String responseBody = is == null ? "" : IOUtils.toString(is, StandardCharsets.UTF_8);

if (response.status() == 401) {
ex = new UnauthorizedException()
Expand Down
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.lang3.exception.ContextedRuntimeException;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
Expand Down Expand Up @@ -42,15 +43,15 @@ public TwitchKrakenErrorDecoder(Decoder decoder) {
* Overwrite the Decode Method to handle custom error cases
*
* @param methodKey Method Key
* @param response Response
* @param response Response
* @return Exception
*/
@Override
public Exception decode(String methodKey, Response response) {
Exception ex = null;
Exception ex;

try {
String responseBody = response.body() == null ? "" : IOUtils.toString(response.body().asInputStream(), StandardCharsets.UTF_8.name());
try (InputStream is = response.body() == null ? null : response.body().asInputStream()) {
String responseBody = is == null ? "" : IOUtils.toString(is, StandardCharsets.UTF_8);

if (response.status() == 401) {
ex = new UnauthorizedException()
Expand All @@ -63,10 +64,10 @@ public Exception decode(String methodKey, Response response) {
.addContextValue("requestMethod", response.request().httpMethod())
.addContextValue("responseBody", responseBody);
} else if (response.status() == 429) {
ex = new ContextedRuntimeException("To many requests!")
ex = new ContextedRuntimeException("Too many requests!")
.addContextValue("requestUrl", response.request().url())
.addContextValue("requestMethod", response.request().httpMethod())
.addContextValue("responseBody", responseBody);;
.addContextValue("responseBody", responseBody);
} else if (response.status() == 503) {
// If you get an HTTP 503 (Service Unavailable) error, retry once.
// If that retry also results in an HTTP 503, there probably is something wrong with the downstream service.
Expand All @@ -80,7 +81,8 @@ public Exception decode(String methodKey, Response response) {
.addContextValue("responseBody", responseBody)
.addContextValue("errorType", error.getError())
.addContextValue("errorStatus", error.getStatus())
.addContextValue("errorType", error.getMessage());
.addContextValue("errorType", error.getMessage())
.addContextValue("errorMessage", error.getMessage());
}
} catch (IOException fallbackToDefault) {
ex = defaultDecoder.decode(methodKey, response);
Expand Down
Expand Up @@ -14,6 +14,7 @@
import org.apache.commons.lang3.exception.ContextedRuntimeException;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class TwitchMessagingInterfaceErrorDecoder implements ErrorDecoder {
Expand All @@ -40,13 +41,13 @@ public TwitchMessagingInterfaceErrorDecoder(Decoder decoder) {
* Overwrite the Decode Method to handle custom error cases
*
* @param methodKey Method Key
* @param response Response
* @param response Response
* @return Exception
*/
@Override
public Exception decode(String methodKey, Response response) {
try {
String responseBody = IOUtils.toString(response.body().asInputStream(), StandardCharsets.UTF_8.name());
try (InputStream is = response.body() == null ? null : response.body().asInputStream()) {
String responseBody = is == null ? "" : IOUtils.toString(is, StandardCharsets.UTF_8);

if (response.status() == 401) {
throw new UnauthorizedException()
Expand All @@ -72,7 +73,8 @@ public Exception decode(String methodKey, Response response) {
.addContextValue("responseBody", responseBody)
.addContextValue("errorType", error.getError())
.addContextValue("errorStatus", error.getStatus())
.addContextValue("errorType", error.getMessage());
.addContextValue("errorType", error.getMessage())
.addContextValue("errorMessage", error.getMessage());
} catch (IOException fallbackToDefault) {
return defaultDecoder.decode(methodKey, response);
}
Expand Down

0 comments on commit f949d85

Please sign in to comment.