I am designing a class whose object is instaniated with a user idn which creates a pdf with that user specific details. It should log something depending on success or failure which can be determined only by caller as the success or failure depends on if it was not just created but dispatched which is done by the caller.
However two problems I can see in this design pattern is that:
It does look a bit odd that caller has to log it from outside. What if the init itself fails?
The way I am thinking of logging as a method if this class is probably wrong? But it doesn't seem so since it shares the state, i.e the user. Any recommendations on this design?
Library Code
class create_pdf():
"""
Class
"""
def __init__(self, user):
"""
"""
user
db_calls_to_get_full_user_details(user)
def fetch_pdf_dms(self):
"""
Fetch from DMS
"""
logic
def fetch_pdf_engine(self):
"""
Fetch from a PDF Generator
"""
# logic that fetches the blob
def log(self, error = None):
"""
"""
# If there was no failure and it was a fetch_pdf_engine
# event then I store the blob into dms for the first time.
# If the event failed I capture the exception. Finally I
# log the event in the database.
def fetch_pdf(self):
pdf = fetch_pdf_dms
if pdf:
return pdf
return fetch_pdf_engine # which also stores to dms first time!
Consumer Code:
method = REQUEST['method']
for each user in users:
try:
object = create_pdf(user)
object.do_something()
dispatch(method)
object.log()
except:
object.log(error=exception)