Wrap sending error file in try/catch for unexpected behaviors [skip ci]

OmarHatem committed Jan 12, 2023 at 17:08 UTC d79b481d3e883a0718e5ec5c188f9d40c993c785
1 file changed +21 -18
lib/main.dart
+21 -18
@@ -172,8 +172,9 @@ void _saveException(String? error, StackTrace? stackTrace) async {
172 }
173 };
174
175 - String separator = "\n\n==========================================================" +
176 - "\n==========================================================\n\n";
175 + const String separator =
176 + '''\n\n==========================================================
177 + ==========================================================\n\n''';
178
179 await file.writeAsString(
180 jsonEncode(exception) + separator,
@@ -182,26 +183,28 @@ void _saveException(String? error, StackTrace? stackTrace) async {
183 }
184
185 void _sendExceptionFile() async {
185 - final appDocDir = await getApplicationDocumentsDirectory();
186 + try {
187 + final appDocDir = await getApplicationDocumentsDirectory();
188
187 - final file = File('${appDocDir.path}/error.txt');
189 + final file = File('${appDocDir.path}/error.txt');
190
189 - print(file.readAsStringSync());
191 + final MailOptions mailOptions = MailOptions(
192 + subject: 'Mobile App Issue',
193 + recipients: ['support@cakewallet.com'],
194 + attachments: [file.path],
195 + );
196
191 - final MailOptions mailOptions = MailOptions(
192 - subject: 'Mobile App Issue',
193 - recipients: ['support@cakewallet.com'],
194 - attachments: [file.path],
195 - );
197 + final result = await FlutterMailer.send(mailOptions);
198
197 - final result = await FlutterMailer.send(mailOptions);
198 -
199 - // Clear file content if the error was sent or saved.
200 - // On android we can't know if it was sent or saved
201 - if (result.name == MailerResponse.sent.name ||
202 - result.name == MailerResponse.saved.name ||
203 - result.name == MailerResponse.android.name) {
204 - file.writeAsString("", mode: FileMode.write);
199 + // Clear file content if the error was sent or saved.
200 + // On android we can't know if it was sent or saved
201 + if (result.name == MailerResponse.sent.name ||
202 + result.name == MailerResponse.saved.name ||
203 + result.name == MailerResponse.android.name) {
204 + file.writeAsString("", mode: FileMode.write);
205 + }
206 + } catch (e, s) {
207 + _saveException(e.toString(), s);
208 }
209 }
210