From b9acff6c2f737dfd00a90df857d772f159df23d4 Mon Sep 17 00:00:00 2001 From: chiften Date: Sun, 3 May 2020 18:55:37 +0430 Subject: [PATCH] add a network response error checker to networkBoundResource.java --- .../data/NetworkBoundResource.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/iammert/com/androidarchitecture/data/NetworkBoundResource.java b/app/src/main/java/iammert/com/androidarchitecture/data/NetworkBoundResource.java index a37044b..c6c6e86 100644 --- a/app/src/main/java/iammert/com/androidarchitecture/data/NetworkBoundResource.java +++ b/app/src/main/java/iammert/com/androidarchitecture/data/NetworkBoundResource.java @@ -50,7 +50,19 @@ private void fetchFromNetwork(final LiveData dbSource) { @Override public void onResponse(Call call, Response response) { result.removeSource(dbSource); - saveResultAndReInit(response.body()); + if(response.isSuccessful()) { + saveResultAndReInit(response.body()); + return; + } + result.addSource(dbSource, newData->{ + try { + result.setValue(Resource.error(response.errorBody().string(), newData)); + } catch (IOException e) { + + // reload from disk whatever we had + result.setValue(Resource.success(newData)); + } + }); } @Override