Skip to content

Commit

Permalink
Change the error capture logic & fix #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederisk committed Dec 28, 2020
1 parent 8c71197 commit 7308750
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions src/export_command/wikimedia_function/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,44 @@ export async function writePage(): Promise<void> {
let args: any;
let result: any;
let token: string | undefined;

args = {
'action': action.query,
'meta': 'tokens',
'type': 'csrf'
};
result = await bot.request(args);
const reNew: TokensResult = TokensConvert.toTokensResult(result);
token = reNew.query?.tokens?.csrftoken;
if (token) {
return token;
let errors: any[] = [{}, {}];

try {
args = {
'action': action.query,
'meta': 'tokens',
'type': 'csrf'
};
result = await bot.request(args);
const reNew: TokensResult = TokensConvert.toTokensResult(result);
token = reNew.query?.tokens?.csrftoken;
if (token) {
return token;
}
}
catch (error) {
console.log(error);
errors[0] = error;
}

args = {
'action': "tokens",
'type': "edit"
};
result = await bot.request(args);
const reOld: OldTokensResult = OldTokensConvert.toOldTokensResult(result);
token = reOld.tokens?.edittoken;
if (token) {
return token;
try {
args = {
'action': "tokens",
'type': "edit"
};
result = await bot.request(args);
const reOld: OldTokensResult = OldTokensConvert.toOldTokensResult(result);
token = reOld.tokens?.edittoken;
if (token) {
return token;
}
}
catch (error) {
console.log(error);
errors[1] = error;
}

throw new Error("Could not get edit token");
throw new Error(`Could not get edit token: NEW: ${errors[0].name}; OLD: ${errors[1].name}`);
}

if (bot === undefined) {
Expand Down Expand Up @@ -129,7 +142,7 @@ export async function writePage(): Promise<void> {
}
catch (error) {
console.log(error);
vscode.window.showErrorMessage(`Error:${error.name}. Your Token: ${bot?.editToken}`);
vscode.window.showErrorMessage(`Error:${error.name}, ${error.message}. Your Token: ${bot?.editToken}`);
}
}

Expand Down

0 comments on commit 7308750

Please sign in to comment.