🤬
  • Catch all json parsing exceptions in HttpResponse.bodyJson() method.

    PiperOrigin-RevId: 466470161
    Change-Id: I335c661897222affda07d49ca6285e1f34147755
  • Loading...
  • Annie Mao committed with Copybara-Service 2 years ago
    afda7792
    1 parent 6772e481
  • ■ ■ ■ ■ ■
    common/src/main/java/com/google/tsunami/common/net/http/HttpResponse.java
    skipped 50 lines
    51 51   
    52 52   /**
    53 53   * Tries to parse the response body as json and returns the parsing result as {@link JsonElement}.
    54  - * If parsing failed, {@link com.google.gson.JsonSyntaxException} will be thrown.
     54 + * If parsing failed, an empty optional is returned.
    55 55   *
    56 56   * @return HTTP response body as a Gson {@link JsonElement} object.
    57 57   */
    58 58   @Memoized
    59 59   public Optional<JsonElement> bodyJson() {
    60  - return bodyString().map(JsonParser::parseString);
     60 + try {
     61 + return bodyString().map(JsonParser::parseString);
     62 + } catch (RuntimeException e) {
     63 + // Do best-effort parsing and ignore Json parsing errors
     64 + return Optional.empty();
     65 + }
    61 66   }
    62 67   
    63 68   /**
    skipped 32 lines
  • ■ ■ ■ ■ ■ ■
    common/src/test/java/com/google/tsunami/common/net/http/HttpResponseTest.java
    skipped 20 lines
    21 21  import static org.junit.Assert.assertThrows;
    22 22  import static org.junit.Assert.assertTrue;
    23 23   
    24  -import com.google.gson.JsonSyntaxException;
    25 24  import com.google.protobuf.ByteString;
    26 25  import okhttp3.HttpUrl;
    27 26  import org.junit.Test;
    skipped 41 lines
    69 68   }
    70 69   
    71 70   @Test
    72  - public void bodyJson_whenNonJsonResponseBody_throwsJsonSyntaxException() {
     71 + public void bodyJson_whenNonJsonResponseBody_returnsEmptyOptional() {
    73 72   HttpResponse httpResponse =
    74 73   HttpResponse.builder()
    75 74   .setStatus(HttpStatus.OK)
    skipped 2 lines
    78 77   .setResponseUrl(TEST_URL)
    79 78   .build();
    80 79   
    81  - assertThrows(JsonSyntaxException.class, httpResponse::bodyJson);
     80 + assertThat(httpResponse.bodyJson()).isEmpty();
    82 81   }
    83 82   
    84 83   @Test
    skipped 24 lines
    109 108   }
    110 109   
    111 110   @Test
    112  - public void jsonFieldEqualsToValue_whenNonJsonResponseBody_throwsJsonSyntaxException() {
     111 + public void jsonFieldEqualsToValue_whenNonJsonResponseBody_returnsEmptyOptional() {
    113 112   HttpResponse httpResponse =
    114 113   HttpResponse.builder()
    115 114   .setStatus(HttpStatus.OK)
    skipped 2 lines
    118 117   .setResponseUrl(TEST_URL)
    119 118   .build();
    120 119   
    121  - assertThrows(
    122  - JsonSyntaxException.class, () -> httpResponse.jsonFieldEqualsToValue("field", "value"));
     120 + assertThat(httpResponse.bodyJson()).isEmpty();
    123 121   }
    124 122   
    125 123   @Test
    skipped 25 lines
Please wait...
Page is in error, reload to recover