Skip to content

Commit

Permalink
Merge pull request #164 from fvaleye/feature/handle-exception-when-fe…
Browse files Browse the repository at this point in the history
…tching-latest-carbon-factor

Handle the exception when Tracarbon is failing to get the latest carbon factor of the location
  • Loading branch information
fvaleye authored Apr 4, 2023
2 parents 0fb8d11 + 7e2ded4 commit 813cb05
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ aiofiles = "^23.1.0"
psutil = "^5.9.4"
ujson = "^5.7.0"
msgpack = "^1.0.4"
pydantic = "^1.9.1"
pydantic = "^1.10.7"
typer = "^0.7.0"
ec2-metadata = "^2.11.0"
python-dotenv = ">=0.21,<1.1"
Expand Down
15 changes: 9 additions & 6 deletions tracarbon/locations/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,17 @@ async def get_latest_co2g_kwh(self) -> float:
return self.co2g_kwh

logger.info(
f"Request the latest carbon intensity in CO2g/kwh for your country {self.name}."
f"Request the latest carbon intensity in Co2g/kwh for your country {self.name}."
)
if not self.co2signal_api_key:
raise CO2SignalAPIKeyIsMissing()
url = f"{self.co2signal_url}{self.name}"
response = await self.request(
url=url,
headers={"auth-token": self.co2signal_api_key},
)
response = {}
try:
response = await self.request(
url=url,
headers={"auth-token": self.co2signal_api_key},
)
logger.debug(f"Response from the {url}: {response}.")
if "data" in response:
response = response["data"]
Expand All @@ -125,7 +126,9 @@ async def get_latest_co2g_kwh(self) -> float:
)
except Exception:
logger.error(
f"Failed to get the latest carbon intensity of your country {self.name}, response: {response}"
f'Failed to get the latest carbon intensity of your country {self.name} {response if response else ""}.'
f"Please check your API configuration."
f"Fallback to use the last known CO2g/kWh of your location {self.co2g_kwh}"
)
return self.co2g_kwh

Expand Down

0 comments on commit 813cb05

Please sign in to comment.