Skip to content

Commit d3548ea

Browse files
committed
Preserve original JSON parse error in JWTParser
JWTParser was wrapping malformed JSON errors in a JWTDecodeException without keeping the original IOException as the cause. This change passes the original IOException through so callers can inspect the root cause. It also adds a regression test for malformed JSON parsing.
1 parent f720a21 commit d3548ea

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

lib/src/main/java/com/auth0/jwt/impl/JWTParser.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public Payload parsePayload(String json) throws JWTDecodeException {
4444
try {
4545
return payloadReader.readValue(json);
4646
} catch (IOException e) {
47-
throw decodeException(json);
47+
throw decodeException(json, e);
4848
}
4949
}
5050

@@ -57,7 +57,7 @@ public Header parseHeader(String json) throws JWTDecodeException {
5757
try {
5858
return headerReader.readValue(json);
5959
} catch (IOException e) {
60-
throw decodeException(json);
60+
throw decodeException(json, e);
6161
}
6262
}
6363

@@ -89,4 +89,9 @@ private static JWTDecodeException decodeException() {
8989
private static JWTDecodeException decodeException(String json) {
9090
return new JWTDecodeException(String.format("The string '%s' doesn't have a valid JSON format.", json));
9191
}
92+
93+
private static JWTDecodeException decodeException(String json, Throwable cause) {
94+
return new JWTDecodeException(
95+
String.format("The string '%s' doesn't have a valid JSON format.", json), cause);
96+
}
9297
}

lib/src/test/java/com/auth0/jwt/impl/JWTParserTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.ObjectReader;
99
import com.fasterxml.jackson.databind.SerializationFeature;
10+
import java.io.IOException;
1011
import org.junit.Before;
1112
import org.junit.Rule;
1213
import org.junit.Test;
@@ -15,6 +16,7 @@
1516
import static com.auth0.jwt.impl.JWTParser.getDefaultObjectMapper;
1617
import static org.hamcrest.MatcherAssert.assertThat;
1718
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.fail;
1820
import static org.mockito.ArgumentMatchers.any;
1921
import static org.mockito.Mockito.mock;
2022
import static org.mockito.Mockito.verify;
@@ -113,4 +115,15 @@ public void shouldThrowWhenConvertingPayloadFromInvalidJson() {
113115
exception.expectMessage("The string '}{' doesn't have a valid JSON format.");
114116
parser.parsePayload("}{");
115117
}
118+
119+
@Test
120+
public void shouldPreserveCauseWhenParsingInvalidJson() {
121+
try {
122+
parser.parsePayload("}{");
123+
fail("Expected JWTDecodeException to be thrown");
124+
} catch (JWTDecodeException e) {
125+
assertThat(e.getCause(), is(notNullValue()));
126+
assertThat(e.getCause(), is(instanceOf(IOException.class)));
127+
}
128+
}
116129
}

0 commit comments

Comments
 (0)