Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/src/main/java/com/auth0/jwt/impl/JWTParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Payload parsePayload(String json) throws JWTDecodeException {
try {
return payloadReader.readValue(json);
} catch (IOException e) {
throw decodeException(json);
throw decodeException(json, e);
}
}

Expand All @@ -57,7 +57,7 @@ public Header parseHeader(String json) throws JWTDecodeException {
try {
return headerReader.readValue(json);
} catch (IOException e) {
throw decodeException(json);
throw decodeException(json, e);
}
}

Expand Down Expand Up @@ -89,4 +89,9 @@ private static JWTDecodeException decodeException() {
private static JWTDecodeException decodeException(String json) {
return new JWTDecodeException(String.format("The string '%s' doesn't have a valid JSON format.", json));
}

private static JWTDecodeException decodeException(String json, Throwable cause) {
return new JWTDecodeException(
String.format("The string '%s' doesn't have a valid JSON format.", json), cause);
}
}
13 changes: 13 additions & 0 deletions lib/src/test/java/com/auth0/jwt/impl/JWTParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -15,6 +16,7 @@
import static com.auth0.jwt.impl.JWTParser.getDefaultObjectMapper;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -113,4 +115,15 @@ public void shouldThrowWhenConvertingPayloadFromInvalidJson() {
exception.expectMessage("The string '}{' doesn't have a valid JSON format.");
parser.parsePayload("}{");
}

@Test
public void shouldPreserveCauseWhenParsingInvalidJson() {
try {
parser.parsePayload("}{");
fail("Expected JWTDecodeException to be thrown");
} catch (JWTDecodeException e) {
assertThat(e.getCause(), is(notNullValue()));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
}
}
}