15
15
import logging
16
16
from typing import List , Tuple , Dict
17
17
18
- from ..exceptions import PathError , FormatError
18
+ from ..errors import ASABIrisError , ErrorCode
19
19
20
20
#
21
21
@@ -62,52 +62,44 @@ async def send_email(
62
62
Send an email using specified parameters.
63
63
...
64
64
"""
65
- try :
66
- body_params = body_params or {}
67
- attachments = attachments or []
68
- email_cc = email_cc or []
69
- email_bcc = email_bcc or []
70
-
71
- # Rendering the template
72
- body_html , email_subject_body = await self ._render_template (body_template , body_params )
73
-
74
- # If email_subject is not provided or is empty use email_subject_body
75
- if email_subject is None or email_subject == '' :
76
- email_subject = email_subject_body
77
-
78
- # Processing attachments
79
- atts_gen = self .AttachmentRenderingService .render_attachment (attachments )
80
-
81
- # Sending the email
82
- await self .SmtpService .send (
83
- email_from = email_from ,
84
- email_to = email_to ,
85
- email_cc = email_cc ,
86
- email_bcc = email_bcc ,
87
- email_subject = email_subject ,
88
- body = body_html ,
89
- attachments = atts_gen ,
90
- )
91
- L .info ("Email sent successfully to: {}" .format (', ' .join (email_to )))
92
-
93
- # TODO: Capture common exceptions and print useful error messages
94
- except Exception as e :
95
- L .exception ("Error occurred when preparing the email" )
96
-
97
- error_message , error_subject = self ._generate_error_message (str (e ))
98
- await self .SmtpService .send (
99
- email_from = email_from ,
100
- email_to = email_to ,
101
- email_cc = email_cc ,
102
- email_bcc = email_bcc ,
103
- email_subject = error_subject ,
104
- body = error_message
105
- )
65
+ body_params = body_params or {}
66
+ attachments = attachments or []
67
+ email_cc = email_cc or []
68
+ email_bcc = email_bcc or []
69
+
70
+ # Rendering the template
71
+ body_html , email_subject_body = await self ._render_template (body_template , body_params )
72
+
73
+ # If email_subject is not provided or is empty use email_subject_body
74
+ if email_subject is None or email_subject == '' :
75
+ email_subject = email_subject_body
76
+
77
+ # Processing attachments
78
+ atts_gen = self .AttachmentRenderingService .render_attachment (attachments )
79
+
80
+ # Sending the email
81
+ await self .SmtpService .send (
82
+ email_from = email_from ,
83
+ email_to = email_to ,
84
+ email_cc = email_cc ,
85
+ email_bcc = email_bcc ,
86
+ email_subject = email_subject ,
87
+ body = body_html ,
88
+ attachments = atts_gen ,
89
+ )
90
+ L .info ("Email sent successfully to: {}" .format (', ' .join (email_to )))
106
91
107
92
108
93
async def _render_template (self , template : str , params : Dict ) -> Tuple [str , str ]:
109
94
if not template .startswith ('/Templates/Email/' ):
110
- raise PathError (use_case = 'Email' , invalid_path = template )
95
+ raise ASABIrisError (
96
+ ErrorCode .INVALID_PATH ,
97
+ tech_message = "Incorrect template path '{}'. Move templates to '/Templates/Email/" .format (template ),
98
+ error_i18n_key = "Incorrect template path '{{incorrect_path}}'. Please move your templates to '/Templates/Email/" ,
99
+ error_dict = {
100
+ "incorrect_path" : template ,
101
+ }
102
+ )
111
103
112
104
jinja_output = await self .JinjaService .format (template , params )
113
105
@@ -121,8 +113,14 @@ async def _render_template(self, template: str, params: Dict) -> Tuple[str, str]
121
113
return body , subject
122
114
123
115
else :
124
- raise FormatError (format = ext )
125
-
116
+ raise ASABIrisError (
117
+ ErrorCode .INVALID_FORMAT ,
118
+ tech_message = "Unsupported conversion format '{}' for template '{}'" .format (ext , template ),
119
+ error_i18n_key = "The format '{{invalid_format}}' is not supported" ,
120
+ error_dict = {
121
+ "invalid_format" : ext ,
122
+ }
123
+ )
126
124
127
125
def _generate_error_message (self , specific_error : str ) -> Tuple [str , str ]:
128
126
timestamp = datetime .datetime .now (tz = datetime .timezone .utc ).strftime ("%Y-%m-%d %H:%M:%S" )
0 commit comments