]> Untitled Git - lemmy-ui.git/commitdiff
wrap login call in try/catch for error handling
authorayan4m1 <andrew@bulletlogic.com>
Thu, 15 Jun 2023 12:48:54 +0000 (08:48 -0400)
committerAndrew DeLisa <andrew@bulletlogic.com>
Fri, 16 Jun 2023 22:18:36 +0000 (18:18 -0400)
src/shared/components/home/login.tsx

index 381c13bb0ce05dac89f5b3068009f5c59940c0d7..3ddb4bc1d2c126126f142818757d0368c9379c5f 100644 (file)
@@ -160,38 +160,42 @@ export class Login extends Component<any, State> {
     if (username_or_email && password) {
       i.setState({ loginRes: { state: "loading" } });
 
-      const loginRes = await HttpService.client.login({
-        username_or_email,
-        password,
-        totp_2fa_token,
-      });
-      switch (loginRes.state) {
-        case "failed": {
-          if (loginRes.msg === "missing_totp_token") {
-            i.setState({ showTotp: true });
-            toast(i18n.t("enter_two_factor_code"), "info");
+      try {
+        const loginRes = await HttpService.client.login({
+          username_or_email,
+          password,
+          totp_2fa_token,
+        });
+        switch (loginRes.state) {
+          case "failed": {
+            if (loginRes.msg === "missing_totp_token") {
+              i.setState({ showTotp: true });
+              toast(i18n.t("enter_two_factor_code"), "info");
+            }
+
+            i.setState({ loginRes: { state: "empty" } });
+            break;
           }
 
-          i.setState({ loginRes: { state: "empty" } });
-          break;
-        }
-
-        case "success": {
-          UserService.Instance.login(loginRes.data);
-          const site = await HttpService.client.getSite({
-            auth: myAuth(),
-          });
+          case "success": {
+            UserService.Instance.login(loginRes.data);
+            const site = await HttpService.client.getSite({
+              auth: myAuth(),
+            });
 
-          if (site.state === "success") {
-            UserService.Instance.myUserInfo = site.data.my_user;
-          }
+            if (site.state === "success") {
+              UserService.Instance.myUserInfo = site.data.my_user;
+            }
 
-          i.props.history.action === "PUSH"
-            ? i.props.history.back()
-            : i.props.history.replace("/");
+            i.props.history.action === "PUSH"
+              ? i.props.history.back()
+              : i.props.history.replace("/");
 
-          break;
+            break;
+          }
         }
+      } catch (error) {
+        i.setState({ loginRes: { state: "failed", msg: error.message } });
       }
     }
   }