From 44b1a1983a700560bd5b2625835fd5c1fb1ef173 Mon Sep 17 00:00:00 2001 From: Doug Keen Date: Wed, 2 Nov 2016 18:15:55 -0700 Subject: [PATCH] Better handling of BART API errors --- .../bart/networktasks/BartApiException.java | 7 +++++++ .../bart/networktasks/EtdContentHandler.java | 9 ++++++++- .../GetRealTimeDeparturesTask.java | 6 ++++++ .../dougkeen/bart/services/EtdService.java | 19 ++++++++++++++++++- app/src/main/res/layout/departures.xml | 11 +++++------ 5 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/dougkeen/bart/networktasks/BartApiException.java diff --git a/app/src/main/java/com/dougkeen/bart/networktasks/BartApiException.java b/app/src/main/java/com/dougkeen/bart/networktasks/BartApiException.java new file mode 100644 index 0000000..dd8c87c --- /dev/null +++ b/app/src/main/java/com/dougkeen/bart/networktasks/BartApiException.java @@ -0,0 +1,7 @@ +package com.dougkeen.bart.networktasks; + +public class BartApiException extends Exception { + public BartApiException(String message) { + super(message); + } +} diff --git a/app/src/main/java/com/dougkeen/bart/networktasks/EtdContentHandler.java b/app/src/main/java/com/dougkeen/bart/networktasks/EtdContentHandler.java index b661d50..7d82efa 100644 --- a/app/src/main/java/com/dougkeen/bart/networktasks/EtdContentHandler.java +++ b/app/src/main/java/com/dougkeen/bart/networktasks/EtdContentHandler.java @@ -27,7 +27,7 @@ public class EtdContentHandler extends DefaultHandler { private final static List TAGS = Arrays.asList("date", "time", "abbreviation", "minutes", "platform", "direction", "length", - "color", "hexcolor", "bikeflag", "destination"); + "color", "hexcolor", "bikeflag", "destination", "error"); private RealTimeDepartures realTimeDepartures; @@ -41,6 +41,7 @@ public class EtdContentHandler extends DefaultHandler { private String currentValue; private Departure currentDeparture; private boolean isParsingTag; + private String error; @Override public void characters(char[] ch, int start, int length) @@ -125,8 +126,14 @@ public class EtdContentHandler extends DefaultHandler { currentDestinationAbbreviation = null; } else if (localName.equals("station")) { realTimeDepartures.finalizeDeparturesList(); + } else if (localName.equals("error")) { + error = currentValue; } isParsingTag = false; currentValue = null; } + + public String getError() { + return error; + } } diff --git a/app/src/main/java/com/dougkeen/bart/networktasks/GetRealTimeDeparturesTask.java b/app/src/main/java/com/dougkeen/bart/networktasks/GetRealTimeDeparturesTask.java index ed63478..fa54808 100644 --- a/app/src/main/java/com/dougkeen/bart/networktasks/GetRealTimeDeparturesTask.java +++ b/app/src/main/java/com/dougkeen/bart/networktasks/GetRealTimeDeparturesTask.java @@ -111,6 +111,12 @@ public abstract class GetRealTimeDeparturesTask extends } final RealTimeDepartures realTimeDepartures = handler .getRealTimeDepartures(); + + if (handler.getError() != null) { + mException = new BartApiException("BART's systems are reporting a problem:\n\"" + handler.getError() + "\""); + return null; + } + return realTimeDepartures; } catch (MalformedURLException e) { throw new RuntimeException(e); diff --git a/app/src/main/java/com/dougkeen/bart/services/EtdService.java b/app/src/main/java/com/dougkeen/bart/services/EtdService.java index d0e0d06..6d49160 100644 --- a/app/src/main/java/com/dougkeen/bart/services/EtdService.java +++ b/app/src/main/java/com/dougkeen/bart/services/EtdService.java @@ -25,6 +25,7 @@ import com.dougkeen.bart.model.ScheduleInformation; import com.dougkeen.bart.model.ScheduleItem; import com.dougkeen.bart.model.Station; import com.dougkeen.bart.model.StationPair; +import com.dougkeen.bart.networktasks.BartApiException; import com.dougkeen.bart.networktasks.GetRealTimeDeparturesTask; import com.dougkeen.bart.networktasks.GetScheduleInformationTask; import com.googlecode.androidannotations.annotations.EService; @@ -115,6 +116,8 @@ public class EtdService extends Service { private List mLatestDepartures; private ScheduleInformation mLatestScheduleInfo; + private String mLatestDepartureError; + private AsyncTask mGetDeparturesTask; private AsyncTask mGetScheduleInformationTask; @@ -139,6 +142,12 @@ public class EtdService extends Service { mStarted = true; fetchLatestDepartures(); } + // Replay ETD or error event + if (!mLatestDepartures.isEmpty()) { + listener.onETDChanged(mLatestDepartures); + } else if (mLatestDepartureError != null) { + listener.onError(mLatestDepartureError); + } } protected void unregisterListener(EtdServiceListener listener) { @@ -195,6 +204,7 @@ public class EtdService extends Service { mIgnoreDepartureDirection) { @Override public void onResult(RealTimeDepartures result) { + mLatestDepartureError = null; Log.v(Constants.TAG, "Processing data from server"); processLatestDepartures(result); Log.v(Constants.TAG, "Done processing data from server"); @@ -205,7 +215,14 @@ public class EtdService extends Service { @Override public void onError(Exception e) { Log.w(Constants.TAG, e.getMessage(), e); - notifyListenersOfError(getString(R.string.could_not_connect)); + + if (e instanceof BartApiException) { + mLatestDepartureError = e.getMessage(); + } else { + mLatestDepartureError = getString(R.string.could_not_connect); + } + notifyListenersOfError(mLatestDepartureError); + // Try again in 60s scheduleDepartureFetch(60000); notifyListenersOfRequestEnd(); diff --git a/app/src/main/res/layout/departures.xml b/app/src/main/res/layout/departures.xml index e26ddc1..34cf7ee 100644 --- a/app/src/main/res/layout/departures.xml +++ b/app/src/main/res/layout/departures.xml @@ -1,6 +1,5 @@ - @@ -20,14 +19,14 @@ android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="10dp" - android:visibility="gone" - /> + android:visibility="gone" /> + android:layout_weight="1" + android:padding="10dp" /> -F + F