Fix the crash from okhttp interceptor (#168)

Summary:
Add a null check before creating a new response
Fixes #166
Pull Request resolved: https://github.com/facebook/Sonar/pull/168

Reviewed By: jknoxville

Differential Revision: D8915871

Pulled By: passy

fbshipit-source-id: aa93273f6fe90a8160133331e0844f67ff7e620a
This commit is contained in:
Thomson Thomas
2018-07-24 08:21:07 -07:00
committed by Facebook Github Bot
parent 02cd3ba560
commit 65239838f2

View File

@@ -1,4 +1,10 @@
// Copyright 2004-present Facebook. All Rights Reserved. /*
* Copyright (c) 2004-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.plugins.network; package com.facebook.sonar.plugins.network;
import android.util.Log; import android.util.Log;
@@ -39,10 +45,14 @@ public class SonarOkhttpInterceptor implements Interceptor {
ResponseInfo responseInfo = convertResponse(response, body, randInt); ResponseInfo responseInfo = convertResponse(response, body, randInt);
plugin.reportResponse(responseInfo); plugin.reportResponse(responseInfo);
// Creating new response as can't used response.body() more than once // Creating new response as can't used response.body() more than once
return response if (responseInfo.body != null) {
.newBuilder() return response
.body(ResponseBody.create(body.contentType(), responseInfo.body)) .newBuilder()
.build(); .body(ResponseBody.create(body.contentType(), responseInfo.body))
.build();
} else {
return response;
}
} }
private static byte[] bodyToByteArray(final Request request) { private static byte[] bodyToByteArray(final Request request) {