Make authentification error report to work, and

warn user if trying to use API but not filling corresponding fields
This commit is contained in:
Godzil 2018-08-01 19:55:59 +01:00
parent ab35bb4439
commit 9c3aaf220a
3 changed files with 37 additions and 8 deletions

View File

@ -111,7 +111,13 @@ export default function(args: string[], done: (err?: Error) => void)
tasksArr[i].retry = 0; 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)); log.error(JSON.stringify(errin));
if (config.debug) if (config.debug)

3
src/interface/AuthError.d.ts vendored Normal file
View File

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

View File

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