diff --git a/src/batch.ts b/src/batch.ts index 759ab40..ad56148 100644 --- a/src/batch.ts +++ b/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) diff --git a/src/interface/AuthError.d.ts b/src/interface/AuthError.d.ts new file mode 100644 index 0000000..3266b62 --- /dev/null +++ b/src/interface/AuthError.d.ts @@ -0,0 +1,3 @@ +interface IAuthError extends Error { + authError: boolean; +} diff --git a/src/my_request.ts b/src/my_request.ts index 91ef1d7..9420c86 100644 --- a/src/my_request.ts +++ b/src/my_request.ts @@ -30,13 +30,13 @@ const defaultHeaders: request.Headers = 'Referer': 'https://www.crunchyroll.com/login', }; -function startSession(config: IConfig): Promise +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 +{ return rp( { method: 'GET', @@ -53,7 +53,11 @@ function startSession(config: IConfig): Promise }) .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')); } });