Skip to content

Commit 83f6662

Browse files
authored
Merge pull request #2350 from bugsnag/PLAT-15132/notify-http-errors
Report HTTP errors
2 parents 7ddd33d + 241a014 commit 83f6662

13 files changed

Lines changed: 356 additions & 77 deletions

File tree

bugsnag-android-core/api/bugsnag-android-core.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,8 @@ public abstract interface class com/bugsnag/android/Plugin {
702702
}
703703

704704
public final class com/bugsnag/android/Request : com/bugsnag/android/AbstractHttpEntity, com/bugsnag/android/JsonStream$Streamable {
705+
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
706+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
705707
public fun addQueryParameter (Ljava/lang/String;Ljava/lang/String;)V
706708
public fun getHttpMethod ()Ljava/lang/String;
707709
public fun getHttpVersion ()Ljava/lang/String;
@@ -716,6 +718,7 @@ public final class com/bugsnag/android/Request : com/bugsnag/android/AbstractHtt
716718
}
717719

718720
public final class com/bugsnag/android/Response : com/bugsnag/android/AbstractHttpEntity, com/bugsnag/android/JsonStream$Streamable {
721+
public fun <init> (I)V
719722
public fun getStatusCode ()I
720723
public fun setStatusCode (I)V
721724
public fun toStream (Lcom/bugsnag/android/JsonStream;)V

bugsnag-android-core/src/androidTest/java/com/bugsnag/android/RequestTest.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ class RequestTest {
88
private val testBodyString = "this is a body string with some content"
99
private val customBodyLength = 5000L
1010

11-
private val logger = NoopLogger
12-
1311
@Test
1412
fun urlQueryIsExtracted() {
1513
val request = Request(
16-
logger,
17-
"GET",
1814
"1.1",
15+
"GET",
1916
"http://localhost/test?t1=arg1&test2=argument+2"
2017
)
2118

@@ -27,14 +24,14 @@ class RequestTest {
2724

2825
@Test
2926
fun setBody() {
30-
val request = Request(logger, "GET", "1.1", "http://localhost/")
27+
val request = Request("1.1", "GET", "http://localhost/")
3128
request.body = testBodyString
3229
assertEquals(testBodyString, request.body)
3330
}
3431

3532
@Test
3633
fun setBodyWithUserLength() {
37-
val request = Request(logger, "GET", "1.1", "http://localhost/")
34+
val request = Request("1.1", "GET", "http://localhost/")
3835
request.bodyLength = customBodyLength
3936
request.body = testBodyString
4037

@@ -44,15 +41,15 @@ class RequestTest {
4441

4542
@Test
4643
fun setNullBody() {
47-
val request = Request(logger, "GET", "1.1", "http://localhost/")
44+
val request = Request("1.1", "GET", "http://localhost/")
4845
request.body = testBodyString
4946
request.body = null
5047
assertNull(request.body)
5148
}
5249

5350
@Test
5451
fun setHttpMethod() {
55-
val request = Request(logger, "GET", "1.1", "http://localhost/")
52+
val request = Request("1.1", "GET", "http://localhost/")
5653
assertEquals("GET", request.httpMethod)
5754

5855
request.httpMethod = "POST"
@@ -61,7 +58,7 @@ class RequestTest {
6158

6259
@Test
6360
fun setHttpVersion() {
64-
val request = Request(logger, "1.1", "1.1", "http://localhost/")
61+
val request = Request("1.1", "1.1", "http://localhost/")
6562
assertEquals("1.1", request.httpVersion)
6663

6764
request.httpVersion = "1.0"
@@ -70,7 +67,7 @@ class RequestTest {
7067

7168
@Test
7269
fun setUrl() {
73-
val request = Request(logger, "GET", "1.1", "http://localhost/")
70+
val request = Request("1.1", "GET", "http://localhost/")
7471
assertEquals("http://localhost/", request.url)
7572

7673
request.url = "https://google.com"
@@ -79,7 +76,7 @@ class RequestTest {
7976

8077
@Test
8178
fun setUrlWithQuery() {
82-
val request = Request(logger, "GET", "1.1", "http://localhost/")
79+
val request = Request("1.1", "GET", "http://localhost/")
8380
request.url = "http://foo.com?a=1&b=2"
8481
assertEquals("http://foo.com", request.url)
8582
assertEquals("1", request.getQueryParameter("a"))
@@ -89,7 +86,7 @@ class RequestTest {
8986

9087
@Test
9188
fun queryParameters() {
92-
val request = Request(logger, "GET", "1.1", "http://localhost/")
89+
val request = Request("1.1", "GET", "http://localhost/")
9390
request.addQueryParameter("foo", "bar")
9491
request.addQueryParameter("another", "param")
9592

@@ -104,7 +101,7 @@ class RequestTest {
104101

105102
@Test
106103
fun headers() {
107-
val request = Request(logger, "GET", "1.1", "http://localhost/")
104+
val request = Request("1.1", "GET", "http://localhost/")
108105
request.addHeader("X-Test", "value")
109106
request.addHeader("Another-Header", "another-value")
110107

@@ -119,7 +116,7 @@ class RequestTest {
119116

120117
@Test
121118
fun setBodyLength() {
122-
val request = Request(logger, "GET", "1.1", "http://localhost/")
119+
val request = Request("1.1", "GET", "http://localhost/")
123120
request.bodyLength = 1234
124121
assertEquals(1234, request.bodyLength)
125122

bugsnag-android-core/src/main/java/com/bugsnag/android/Request.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,56 @@
1818
*/
1919
@SuppressWarnings("ConstantValue")
2020
public final class Request extends AbstractHttpEntity implements JsonStream.Streamable {
21-
private final Logger logger;
2221
private final Map<String, String> params = new LinkedHashMap<>();
2322

24-
@NonNull
23+
@Nullable
2524
private String httpMethod;
2625

27-
@NonNull
26+
@Nullable
2827
private String httpVersion;
2928

30-
@NonNull
31-
@SuppressWarnings("NotNullFieldNotInitialized")
29+
@Nullable
3230
private String url;
3331

34-
Request(Logger logger,
35-
@NonNull String httpMethod,
36-
@NonNull String httpVersion,
37-
@NonNull String url) {
32+
/**
33+
* Constructs a new Request with the specified HTTP version, method, and URL.
34+
* If the URL contains query parameters, they will be extracted and stored separately.
35+
*
36+
* @param httpVersion the HTTP version (e.g. "HTTP/1.1"), or null
37+
* @param httpMethod the HTTP method (e.g. "GET", "POST"), or null
38+
* @param url the request URL, optionally including query parameters, or null
39+
*/
40+
public Request(
41+
@Nullable String httpVersion,
42+
@Nullable String httpMethod,
43+
@Nullable String url) {
3844

39-
this.logger = logger;
4045
this.httpMethod = httpMethod;
4146
this.httpVersion = httpVersion;
4247
setUrl(url);
4348
}
4449

45-
private void logNull(String property) {
46-
logger.e("Invalid null value supplied to request." + property + ", ignoring");
50+
/**
51+
* Constructs a new Request with the specified HTTP method and URL.
52+
* The HTTP version will be set to null.
53+
* If the URL contains query parameters, they will be extracted and stored separately.
54+
*
55+
* @param httpMethod the HTTP method (e.g. "GET", "POST"), or null
56+
* @param url the request URL, optionally including query parameters, or null
57+
*/
58+
public Request(
59+
@Nullable String httpMethod,
60+
@Nullable String url) {
61+
62+
this(null, httpMethod, url);
4763
}
4864

4965
/**
5066
* Return the HTTP method for this request (e.g. "GET").
5167
*
5268
* @return the HTTP method
5369
*/
54-
@NonNull
70+
@Nullable
5571
public String getHttpMethod() {
5672
return httpMethod;
5773
}
@@ -61,20 +77,16 @@ public String getHttpMethod() {
6177
*
6278
* @param httpMethod the HTTP method name
6379
*/
64-
public void setHttpMethod(@NonNull String httpMethod) {
65-
if (httpMethod != null) {
66-
this.httpMethod = httpMethod;
67-
} else {
68-
logNull("httpMethod");
69-
}
80+
public void setHttpMethod(@Nullable String httpMethod) {
81+
this.httpMethod = httpMethod;
7082
}
7183

7284
/**
7385
* Return the HTTP version for this request (e.g. "HTTP/1.1").
7486
*
7587
* @return the HTTP version
7688
*/
77-
@NonNull
89+
@Nullable
7890
public String getHttpVersion() {
7991
return httpVersion;
8092
}
@@ -84,20 +96,16 @@ public String getHttpVersion() {
8496
*
8597
* @param httpVersion the HTTP version
8698
*/
87-
public void setHttpVersion(@NonNull String httpVersion) {
88-
if (httpVersion != null) {
89-
this.httpVersion = httpVersion;
90-
} else {
91-
logNull("httpVersion");
92-
}
99+
public void setHttpVersion(@Nullable String httpVersion) {
100+
this.httpVersion = httpVersion;
93101
}
94102

95103
/**
96104
* Return the URL for this request, excluding query parameters.
97105
*
98106
* @return the request URL
99107
*/
100-
@NonNull
108+
@Nullable
101109
public String getUrl() {
102110
return url;
103111
}
@@ -108,7 +116,7 @@ public String getUrl() {
108116
*
109117
* @param url the request URL, optionally including query parameters
110118
*/
111-
public void setUrl(@NonNull String url) {
119+
public void setUrl(@Nullable String url) {
112120
if (url != null) {
113121
int querySeparatorIndex = url.indexOf('?');
114122

bugsnag-android-core/src/main/java/com/bugsnag/android/Response.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
public final class Response extends AbstractHttpEntity implements JsonStream.Streamable {
1313
private int statusCode;
1414

15-
Response(@IntRange(from = 0) int statusCode) {
15+
/**
16+
* Constructs a new Response with the specified HTTP status code.
17+
*
18+
* @param statusCode the HTTP status code
19+
*/
20+
public Response(@IntRange(from = 0) int statusCode) {
1621
this.statusCode = statusCode;
1722
}
1823

bugsnag-plugin-android-okhttp/detekt-baseline.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<ID>MagicNumber:BugsnagOkHttpPlugin.kt$399</ID>
1313
<ID>MagicNumber:BugsnagOkHttpPlugin.kt$400</ID>
1414
<ID>MagicNumber:BugsnagOkHttpPlugin.kt$599</ID>
15+
<ID>NestedBlockDepth:BugsnagOkHttpInterceptor.kt$OkHttpInstrumentedResponse$private fun extractResponseBody(): String?</ID>
16+
<ID>ReturnCount:BugsnagOkHttpInterceptor.kt$OkHttpInstrumentedResponse$private fun extractResponseBody(): String?</ID>
1517
<ID>TooManyFunctions:DelegateEventListener.kt$DelegateEventListener : EventListener</ID>
1618
</CurrentIssues>
1719
</SmellBaseline>

bugsnag-plugin-android-okhttp/src/main/java/com/bugsnag/android/okhttp/BugsnagOkHttp.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import kotlin.math.max
1919
* [addInterceptor](okhttp3.OkHttpClient.Builder.addInterceptor) to configure it with your
2020
* `OkHttpClient`.
2121
*/
22+
@Suppress("SENSELESS_COMPARISON")
2223
class BugsnagOkHttp : HttpInstrumentationBuilder<Request, Response> {
2324
private val errorCodes = BitSet()
2425
private var maxRequestBodyCapture = DEFAULT_BODY_CAPTURE_SIZE
@@ -85,7 +86,6 @@ class BugsnagOkHttp : HttpInstrumentationBuilder<Request, Response> {
8586
}
8687

8788
override fun addRequestCallback(callback: HttpRequestCallback<Request>): BugsnagOkHttp {
88-
@Suppress("SENSELESS_COMPARISON")
8989
if (callback != null) {
9090
requestCallbacks.add(callback)
9191
}
@@ -94,7 +94,6 @@ class BugsnagOkHttp : HttpInstrumentationBuilder<Request, Response> {
9494
}
9595

9696
override fun addResponseCallback(callback: HttpResponseCallback<Request, Response>): BugsnagOkHttp {
97-
@Suppress("SENSELESS_COMPARISON")
9897
if (callback != null) {
9998
responseCallbacks.add(callback)
10099
}
@@ -148,6 +147,6 @@ class BugsnagOkHttp : HttpInstrumentationBuilder<Request, Response> {
148147
}
149148

150149
internal companion object {
151-
private const val DEFAULT_BODY_CAPTURE_SIZE = 4096L
150+
private const val DEFAULT_BODY_CAPTURE_SIZE: Long = 0L
152151
}
153152
}

0 commit comments

Comments
 (0)