Skip to content

Commit c6cb97e

Browse files
authored
Add ability to suppress logging of known unsuccessful API calls (commons-app#5526)
1 parent ba11f0d commit c6cb97e

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MediaDataExtractor @Inject constructor(private val mediaClient: MediaClien
4343
return Single.ambArray(
4444
mediaClient.getMediaById(PAGE_ID_PREFIX + media.pageId)
4545
.onErrorResumeNext { Single.never() },
46-
mediaClient.getMedia(media.filename)
46+
mediaClient.getMediaSuppressingErrors(media.filename)
4747
.onErrorResumeNext { Single.never() }
4848
)
4949

app/src/main/java/fr/free/nrw/commons/OkHttpConnectionFactory.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public Response intercept(@NonNull final Chain chain) throws IOException {
6767
}
6868

6969
public static class UnsuccessfulResponseInterceptor implements Interceptor {
70+
private static final String SUPPRESS_ERROR_LOG = "x-commons-suppress-error-log";
71+
public static final String SUPPRESS_ERROR_LOG_HEADER = SUPPRESS_ERROR_LOG+": true";
7072
private static final List<String> DO_NOT_INTERCEPT = Collections.singletonList(
7173
"api.php?format=json&formatversion=2&errorformat=plaintext&action=upload&ignorewarnings=1");
7274

@@ -75,7 +77,16 @@ public static class UnsuccessfulResponseInterceptor implements Interceptor {
7577
@Override
7678
@NonNull
7779
public Response intercept(@NonNull final Chain chain) throws IOException {
78-
final Response rsp = chain.proceed(chain.request());
80+
final Request rq = chain.request();
81+
82+
// If the request contains our special "suppress errors" header, make note of it
83+
// but don't pass that on to the server.
84+
final boolean suppressErrors = rq.headers().names().contains(SUPPRESS_ERROR_LOG);
85+
final Request request = rq.newBuilder()
86+
.removeHeader(SUPPRESS_ERROR_LOG)
87+
.build();
88+
89+
final Response rsp = chain.proceed(request);
7990

8091
// Do not intercept certain requests and let the caller handle the errors
8192
if(isExcludedUrl(chain.request())) {
@@ -89,7 +100,12 @@ public Response intercept(@NonNull final Chain chain) throws IOException {
89100
}
90101
}
91102
} catch (final IOException e) {
92-
Timber.e(e);
103+
// Log the error as debug (and therefore, "expected") or at error level
104+
if (suppressErrors) {
105+
Timber.d(e, "Suppressed (known / expected) error");
106+
} else {
107+
Timber.e(e);
108+
}
93109
}
94110
return rsp;
95111
}

app/src/main/java/fr/free/nrw/commons/media/MediaClient.kt

+11
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ class MediaClient @Inject constructor(
130130
.map { it.first() }
131131
}
132132

133+
/**
134+
* Fetches Media object from the imageInfo API but suppress (known) errors
135+
*
136+
* @param titles the tiles to be searched for. Can be filename or template name
137+
* @return
138+
*/
139+
fun getMediaSuppressingErrors(titles: String?): Single<Media> {
140+
return responseMapper(mediaInterface.getMediaSuppressingErrors(titles))
141+
.map { it.first() }
142+
}
143+
133144
/**
134145
* The method returns the picture of the day
135146
*

app/src/main/java/fr/free/nrw/commons/media/MediaInterface.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package fr.free.nrw.commons.media;
22

3+
import static fr.free.nrw.commons.OkHttpConnectionFactory.UnsuccessfulResponseInterceptor.SUPPRESS_ERROR_LOG_HEADER;
4+
35
import io.reactivex.Single;
46
import java.util.Map;
57
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse;
68
import retrofit2.http.GET;
9+
import retrofit2.http.Headers;
710
import retrofit2.http.Query;
811
import retrofit2.http.QueryMap;
912

@@ -103,6 +106,17 @@ Single<MwQueryResponse> getMediaListFromSearch(@Query("gsrsearch") String keywor
103106
MEDIA_PARAMS_WITH_CATEGORY_DETAILS)
104107
Single<MwQueryResponse> getMedia(@Query("titles") String title);
105108

109+
/**
110+
* Fetches Media object from the imageInfo API but suppress (known) errors
111+
*
112+
* @param title the tiles to be searched for. Can be filename or template name
113+
* @return
114+
*/
115+
@GET("w/api.php?action=query&format=json&formatversion=2" +
116+
MEDIA_PARAMS_WITH_CATEGORY_DETAILS)
117+
@Headers(SUPPRESS_ERROR_LOG_HEADER)
118+
Single<MwQueryResponse> getMediaSuppressingErrors(@Query("titles") String title);
119+
106120
/**
107121
* Fetches Media object from the imageInfo API
108122
*
@@ -111,6 +125,7 @@ Single<MwQueryResponse> getMediaListFromSearch(@Query("gsrsearch") String keywor
111125
*/
112126
@GET("w/api.php?action=query&format=json&formatversion=2" +
113127
MEDIA_PARAMS)
128+
@Headers(SUPPRESS_ERROR_LOG_HEADER)
114129
Single<MwQueryResponse> getMediaById(@Query("pageids") String pageIds);
115130

116131
/**
@@ -125,6 +140,7 @@ Single<MwQueryResponse> getMediaListFromSearch(@Query("gsrsearch") String keywor
125140
Single<MwQueryResponse> getMediaWithGenerator(@Query("titles") String title);
126141

127142
@GET("w/api.php?format=json&action=parse&prop=text")
143+
@Headers(SUPPRESS_ERROR_LOG_HEADER)
128144
Single<MwParseResponse> getPageHtml(@Query("page") String title);
129145

130146
/**

0 commit comments

Comments
 (0)