Fix failure when using incorrect interwiki map

Fixes #35.
This commit is contained in:
Florine W. Dekker 2020-07-29 15:09:15 +02:00
parent 6a7aaaef7c
commit e84d87ced6
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
2 changed files with 23 additions and 5 deletions

View File

@ -53,8 +53,8 @@
<br />
To use the tool, you should enter the link to the
<a href="https://www.mediawiki.org/wiki/API:Main_page">API of the wiki</a> you want to check.
For <b>Wikimedia</b> wikis, this is <code>&lt;example.org&gt;/w/api.php</code>.
For <b>Fandom</b> wikis, this is <code>&lt;wiki&gt;.fandom.com/api.php</code>.<br />
For <b>Wikimedia</b> wikis, this is <code>https://&lt;example.org&gt;/w/api.php</code>.
For <b>Fandom</b> wikis, this is <code>https://&lt;wiki&gt;.fandom.com/api.php</code>.<br />
<br />
If you need <b>help</b>, have a <b>question</b>, or found a <b>bug</b>, please
<a href="https://git.fwdekker.com/FWDekker/interlanguage-checker/issues/new">open an issue</a>

View File

@ -348,7 +348,7 @@ export class MediaWiki {
return fetchJsonp(url)
.then(it => it.json())
.catch(() => {
throw new Error("Could not to connect to API. Is the URL correct?")
throw new Error("Could not to connect to API. Is the URL correct? See the <b>About</b> section for more information.")
});
}
@ -489,7 +489,13 @@ export class MediaWikiManager {
return undefined;
const url = this._iwMap.getUrl(lang);
const newMw = await new MediaWiki(url.slice(0, -this.articlePath.length) + this.apiPath).init();
let newMw;
try {
newMw = await new MediaWiki(url.slice(0, -this.articlePath.length) + this.apiPath).init();
} catch (error) {
return undefined;
}
if (this.hasMw(newMw.general.lang)) {
this.mws[lang] = this.mws[newMw.general.lang];
} else {
@ -566,6 +572,15 @@ export const discoverNetwork = async function (mwm, title, progressCb) {
// Normalize
const nextMw = await mwm.getMwOrWait(next.lang);
if (nextMw === undefined) {
history.push(next);
pages.push(new Page(mwm.getArticlePath(next), next, [], false));
if (history.length === 1)
throw new Error("Could not to connect to API. Is the URL correct? See the <b>About</b> section for more information.");
else
continue;
}
next = nextMw.normalize(next);
if (history.some(it => it.equals(next)))
continue;
@ -596,7 +611,10 @@ export const discoverNetwork = async function (mwm, title, progressCb) {
// Normalize links
pages.forEach(page => {
page.langLinks = page.langLinks.map(langLink => mwm.getMw(langLink.lang).normalize(langLink));
page.langLinks = page.langLinks.map(langLink => {
const mw = mwm.getMw(langLink.lang);
return mw !== undefined ? mw.normalize(langLink) : langLink;
});
});
return {pages: pages, redirects: redirects};