Jelajahi Sumber

Handle error in applying code

Neeraj Gupta 2 tahun lalu
induk
melakukan
ffcc309492
2 mengubah file dengan 34 tambahan dan 1 penghapusan
  1. 4 1
      lib/ui/growth/apply_code_screen.dart
  2. 30 0
      lib/utils/dialog_util.dart

+ 4 - 1
lib/ui/growth/apply_code_screen.dart

@@ -116,7 +116,10 @@ class _ApplyCodeScreenState extends State<ApplyCodeScreen> {
                         } catch (e) {
                           Logger('$runtimeType')
                               .severe("failed to apply referral", e);
-                          showGenericErrorDialog(context: context);
+                          showErrorDialogForException(
+                            context: context,
+                            exception: e as Exception,
+                          );
                         }
                       },
                     )

+ 30 - 0
lib/utils/dialog_util.dart

@@ -1,6 +1,7 @@
 import 'dart:math';
 
 import 'package:confetti/confetti.dart';
+import "package:dio/dio.dart";
 import 'package:flutter/material.dart';
 import 'package:photos/core/constants.dart';
 import "package:photos/models/search/button_result.dart";
@@ -38,6 +39,35 @@ Future<ButtonResult?> showErrorDialog(
   );
 }
 
+Future<ButtonResult?> showErrorDialogForException({
+  required BuildContext context,
+  required Exception exception,
+  bool isDismissible = true,
+}) async {
+  String errorMessage =
+      "It looks like something went wrong. Please retry after some time. If the error persists, please contact our support team.";
+  if (exception is DioError &&
+      exception.response != null &&
+      exception.response!.data["code"] != null) {
+    errorMessage = "It looks like something went wrong. \n\nReason: " +
+        exception.response!.data["code"];
+  }
+  return showDialogWidget(
+    context: context,
+    title: "Error",
+    icon: Icons.error_outline_outlined,
+    body: errorMessage,
+    isDismissible: isDismissible,
+    buttons: const [
+      ButtonWidget(
+        buttonType: ButtonType.secondary,
+        labelText: "OK",
+        isInAlert: true,
+      ),
+    ],
+  );
+}
+
 ///Will return null if dismissed by tapping outside
 Future<ButtonResult?> showGenericErrorDialog({
   required BuildContext context,