Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shridarpatil committed Aug 15, 2024
1 parent 4ef4f45 commit 3d1be6a
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from frappe.utils.safe_exec import get_safe_globals, safe_exec
from frappe.integrations.utils import make_post_request
from frappe.desk.form.utils import get_pdf_link
from frappe.utils import add_to_date, nowdate
from frappe.utils import add_to_date, nowdate, datetime


class WhatsAppNotification(Document):
Expand Down Expand Up @@ -90,9 +90,12 @@ def send_template_message(self, doc: Document):
if self.fields:
parameters = []
for field in self.fields:
value = doc_data[field.field_name]
if isinstance(doc_data[field.field_name], (datetime.date, datetime.datetime)):
value = str(type(doc_data[field.field_name]))
parameters.append({
"type": "text",
"text": doc_data[field.field_name]
"text": value
})

data['template']["components"] = [{
Expand Down Expand Up @@ -182,6 +185,7 @@ def notify(self, data):
"content-type": "application/json"
}
try:
success = False
response = make_post_request(
f"{settings.url}/{settings.version}/{settings.phone_id}/messages",
headers=headers, data=json.dumps(data)
Expand All @@ -201,20 +205,28 @@ def notify(self, data):
}).save(ignore_permissions=True)

frappe.msgprint("WhatsApp Message Triggered", indicator="green", alert=True)
success = True

except Exception as e:
response = frappe.flags.integration_request.json()['error']
error_message = response.get('Error', response.get("message"))
error_message = str(e)
if frappe.flags.integration_request:
response = frappe.flags.integration_request.json()['error']
error_message = response.get('Error', response.get("message"))

frappe.msgprint(
f"Failed to trigger whatsapp message: {error_message}",
indicator="red",
alert=True
)
finally:
if not success:
meta = {"error": error_message}
else:
meta = frappe.flags.integration_request.json()
frappe.get_doc({
"doctype": "WhatsApp Notification Log",
"template": self.template,
"meta_data": frappe.flags.integration_request.json()
"meta_data": meta
}).insert(ignore_permissions=True)

def on_trash(self):
Expand Down

0 comments on commit 3d1be6a

Please sign in to comment.