Browse Source

Make authentification error report to work, and
warn user if trying to use API but not filling corresponding fields

Godzil 5 years ago
parent
commit
9c3aaf220a
3 changed files with 37 additions and 8 deletions
  1. 7 1
      src/batch.ts
  2. 3 0
      src/interface/AuthError.d.ts
  3. 27 7
      src/my_request.ts

+ 7 - 1
src/batch.ts

@@ -111,7 +111,13 @@ export default function(args: string[], done: (err?: Error) => void)
             tasksArr[i].retry = 0;
           }
 
-          if (tasksArr[i].retry <= 0)
+          if (errin.authError)
+          {
+            /* Force a graceful exit */
+            log.error(errin.message);
+            i = tasksArr.length;
+          }
+          else if (tasksArr[i].retry <= 0)
           {
             log.error(JSON.stringify(errin));
             if (config.debug)

+ 3 - 0
src/interface/AuthError.d.ts

@@ -0,0 +1,3 @@
+interface IAuthError extends Error {
+  authError: boolean;
+}

+ 27 - 7
src/my_request.ts

@@ -30,13 +30,13 @@ const defaultHeaders: request.Headers =
   'Referer': 'https://www.crunchyroll.com/login',
 };
 
-function startSession(config: IConfig): Promise<string>
+function AuthError(msg: string): IAuthError
 {
-  if (config.crDeviceId === undefined)
-  { 
-    config.crDeviceId = uuid.v4();
-  }
+  return { name: 'AuthError', message: msg, authError: true };
+}
 
+function startSession(config: IConfig): Promise<any>
+{
   return rp(
   {
     method: 'GET',
@@ -53,7 +53,11 @@ function startSession(config: IConfig): Promise<string>
   })
   .then((response: any) =>
   {
-    if ((response.data === undefined) || (response.data.session_id === undefined)) throw new Error('Getting session failed: ' + JSON.stringify(response));
+    if ((response.data === undefined) || (response.data.session_id === undefined))
+    {
+      throw new Error('Getting session failed: ' + JSON.stringify(response));
+    }
+
     return response.data.session_id;
   });
 }
@@ -127,7 +131,7 @@ function checkIfUserIsAuth(config: IConfig, done: (err: Error) => void): void
     if (isAuthenticated === false)
     {
         const error = $('ul.message, li.error').text();
-        return done(new Error('Authentication failed: ' + error));
+        return done(AuthError('Authentication failed: ' + error));
     }
     else
     {
@@ -246,6 +250,17 @@ function authenticate(config: IConfig, done: (err: Error) => void)
 
     if (config.logUsingApi)
     {
+      if (config.crDeviceId === undefined)
+      {
+        config.crDeviceId = uuid.v4();
+      }
+
+      if (!config.crSessionUrl || !config.crDeviceType || !config.crAPIVersion ||
+          !config.crLocale || !config.crLoginUrl)
+      {
+        return done(AuthError('Invalid API configuration, please check your config file.'));
+      }
+
       startSession(config)
       .then((sessionId: string) =>
       {
@@ -265,6 +280,10 @@ function authenticate(config: IConfig, done: (err: Error) => void)
             return done(errCheckAuth2);
           }
         });
+      })
+      .catch((errInChk) =>
+      {
+        return done(AuthError(errInChk.message));
       });
     }
     else if (config.logUsingCookie)
@@ -289,6 +308,7 @@ function authenticate(config: IConfig, done: (err: Error) => void)
     else
     {
       log.error('This method of login is currently unsupported...\n');
+      return done(AuthError('Unsupported login method'));
     }
 
   });