From 022a08f5a1212381c8afda4feadae1cb513d169b Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 13 Nov 2014 21:56:07 -0800 Subject: [PATCH] Throw proper exceptions when an HTTP request fails --- .../src/com/limelight/nvstream/http/NvHTTP.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/moonlight-common/src/com/limelight/nvstream/http/NvHTTP.java b/moonlight-common/src/com/limelight/nvstream/http/NvHTTP.java index 0a60cb45..8f3bfd89 100644 --- a/moonlight-common/src/com/limelight/nvstream/http/NvHTTP.java +++ b/moonlight-common/src/com/limelight/nvstream/http/NvHTTP.java @@ -1,5 +1,6 @@ package com.limelight.nvstream.http; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -36,6 +37,7 @@ import com.limelight.nvstream.StreamConfiguration; import com.limelight.nvstream.http.PairingManager.PairState; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; +import com.squareup.okhttp.Response; import com.squareup.okhttp.ResponseBody; @@ -230,14 +232,25 @@ public class NvHTTP { // queries do not. private ResponseBody openHttpConnection(String url, boolean enableReadTimeout) throws IOException { Request request = new Request.Builder().url(url).build(); + Response response; if (enableReadTimeout) { performAndroidTlsHack(httpClientWithReadTimeout); - return httpClientWithReadTimeout.newCall(request).execute().body(); + response = httpClientWithReadTimeout.newCall(request).execute(); } else { performAndroidTlsHack(httpClient); - return httpClient.newCall(request).execute().body(); + response = httpClient.newCall(request).execute(); + } + + if (response.isSuccessful()) { + return response.body(); + } + else if (response.code() == 404) { + throw new FileNotFoundException(url); + } + else { + throw new IOException("HTTP request failed: "+response.code()); } }