File tree Expand file tree Collapse file tree
main/java/com/auth0/jwt/impl
test/java/com/auth0/jwt/impl Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 77import com .fasterxml .jackson .databind .ObjectMapper ;
88import com .fasterxml .jackson .databind .ObjectReader ;
99import com .fasterxml .jackson .databind .SerializationFeature ;
10+ import java .io .IOException ;
1011import org .junit .Before ;
1112import org .junit .Rule ;
1213import org .junit .Test ;
1516import static com .auth0 .jwt .impl .JWTParser .getDefaultObjectMapper ;
1617import static org .hamcrest .MatcherAssert .assertThat ;
1718import static org .hamcrest .Matchers .*;
19+ import static org .junit .Assert .fail ;
1820import static org .mockito .ArgumentMatchers .any ;
1921import static org .mockito .Mockito .mock ;
2022import 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}
You can’t perform that action at this time.
0 commit comments