Skip to content

Commit

Permalink
Better exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
coddingtonbear committed Jul 4, 2020
1 parent 9fb09a4 commit bd3e8af
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions myfitnesspal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .base import MFPBase
from .day import Day
from .entry import Entry
from .exceptions import MyfitnesspalLoginError, MyfitnesspalRequestFailed
from .exercise import Exercise
from .fooditem import FoodItem
from .fooditemserving import FoodItemServing
Expand Down Expand Up @@ -109,7 +110,7 @@ def _login(self):
# bad assumption?) PORTING_CHECK
content = result.content.decode("utf8")
if "Incorrect username or password" in content:
raise ValueError("Incorrect username or password.")
raise MyfitnesspalLoginError()

self._auth_data = self._get_auth_data()
self._user_metadata = self._get_user_metadata()
Expand All @@ -122,7 +123,7 @@ def _get_auth_data(self) -> types.AuthData:
parse.urljoin(self.BASE_URL_SECURE, "/user/auth_token") + "?refresh=true"
)
if not result.ok:
raise RuntimeError(
raise MyfitnesspalRequestFailed(
"Unable to fetch authentication token from MyFitnessPal: "
"status code: {status}".format(status=result.status_code)
)
Expand Down Expand Up @@ -573,7 +574,7 @@ def set_measurements(

# throw an error if it failed.
if not result.ok:
raise RuntimeError(
raise MyfitnesspalRequestFailed(
"Unable to update measurement in MyFitnessPal: "
"status code: {status}".format(status=result.status_code)
)
Expand Down Expand Up @@ -672,7 +673,7 @@ def get_food_search_results(self, query: str) -> List[FoodItem]:
content = result.content.decode("utf8")
document = lxml.html.document_fromstring(content)
if "Matching Foods:" not in content:
raise ValueError("Unable to load search results.")
raise MyfitnesspalRequestFailed("Unable to load search results.")

return self._get_food_search_results(document)

Expand Down Expand Up @@ -719,12 +720,8 @@ def get_food_item_details(self, mfp_id) -> FoodItem:
)
result = self._get_request_for_url(metadata_url, send_token=True)
if not result.ok:
logger.warning(
"Unable to fetch item details; this may cause Myfitnesspal "
"to behave incorrectly if you have logged-in with your "
"e-mail address rather than your basic username; status %s.",
result.status_code,
)
raise MyfitnesspalRequestFailed()

resp = result.json()["item"]

# identifying serving info
Expand Down

0 comments on commit bd3e8af

Please sign in to comment.