BACKGROUND
I have a function where i cache file from aws s3. I provide it the path on s3 and also the local path of where i want the cached file to live. Sometimes if the file does not exist on s3 it will return a 404 error. That makes sense but i want to handle it gracefully. What i want to do is if the file i am trying to cache does not exist I want my get_overview_stream function to return None.
CODE
def get_overview_stream(*, environment, proxy_key):
s3_overview_file_path = f"s3://{FOO_TRACK_BUCKET}/{environment}"
overview_file = f"{proxy_key}.csv"
local_path = cache_directory(environment)
try:
cache.cache_file(s3_overview_file_path, local_path, overview_file)
overview_file_cache = local_path / f"{proxy_key}.csv"
return overview_file_cache.open("r")
except botocore.exceptions.ClientError as e:
if e.response["Error"]["Code"] == "404":
return None
else:
raise
Attempted Solution Issue
I am very new to python so i am not sure if this the best way to handle this exception and if there is a more cleaner way. If so i would love to hear feedback.
-
1\$\begingroup\$ Honestly, I've never used to libraries involved here, but I don't see anything wrong with this approach. The exception handling seems fine to me. \$\endgroup\$Carcigenicate– Carcigenicate2020年08月15日 17:57:24 +00:00Commented Aug 15, 2020 at 17:57
-
\$\begingroup\$ The only thing I would change here is adding a docstring and type information. You could move the last 2 lines in the try block outside. raising a custom exception instead of returning the sentinel value None might also be an improvement, but that last point id a matter of taste /habit \$\endgroup\$Maarten Fabré– Maarten Fabré2020年08月15日 20:05:55 +00:00Commented Aug 15, 2020 at 20:05
-
\$\begingroup\$ Also, always open a file with a context manager instead of a bare open() \$\endgroup\$Maarten Fabré– Maarten Fabré2020年08月15日 20:06:59 +00:00Commented Aug 15, 2020 at 20:06
-
\$\begingroup\$ @MaartenFabré can you show example of what you mean by custom exception? \$\endgroup\$Dinero– Dinero2020年08月18日 11:51:07 +00:00Commented Aug 18, 2020 at 11:51
1 Answer 1
I would suggest not opening the file in your function but instead returning the path or None
, such that the caller can open the file using a with
statement.