Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #61 from cpiber/online
Browse files Browse the repository at this point in the history
Better error handling for online save
  • Loading branch information
Glagan committed Oct 16, 2020
2 parents d7d0743 + 359e44c commit e0d8786
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 25 deletions.
2 changes: 1 addition & 1 deletion scripts/optionsManager.js
Expand Up @@ -215,7 +215,7 @@ class OptionsManager {
// FUNCTIONS

async sleep(time) {
return await new Promise((resolve) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, time);
Expand Down
107 changes: 83 additions & 24 deletions scripts/sharedFunctions.js
Expand Up @@ -315,26 +315,28 @@ async function updateLocalStorage(manga, options) {
{ position: 'bottom-left' }
);
}

// Update online
if (options.onlineSave && options.isLoggedIn) {
// Build body
let body = {
mal: manga.myAnimeListId,
last: manga.lastMangaDexChapter,
options: {
saveAllOpened: options.saveAllOpened,
maxChapterSaved: options.maxChapterSaved,
},
};
if (options.updateHistoryPage && manga.name != undefined && manga.chapterId > 0) {
body.options.updateHistoryPage = true;
body.volume = manga.currentChapter.volume;
body.title_name = manga.name;
body.chapter = manga.chapterId;
body.highest = manga.highest;
body.lastRead = manga.lastRead;
}
// Send the request
if (!options.onlineSave || !options.isLoggedIn) return;
// Build body
let body = {
mal: manga.myAnimeListId,
last: manga.lastMangaDexChapter,
options: {
saveAllOpened: options.saveAllOpened,
maxChapterSaved: options.maxChapterSaved,
},
};
if (options.updateHistoryPage && manga.name != undefined && manga.chapterId > 0) {
body.options.updateHistoryPage = true;
body.volume = manga.currentChapter.volume;
body.title_name = manga.name;
body.chapter = manga.chapterId;
body.highest = manga.highest;
body.lastRead = manga.lastRead;
}
// Send the request
const request = async () => {
try {
let response = await browser.runtime.sendMessage({
action: 'fetch',
Expand All @@ -352,21 +354,78 @@ async function updateLocalStorage(manga, options) {
isJson: true,
});

if (response.status != 200) {
if (response.status == 500) {
options.isLoggedIn = false;
} else if (response.status != 200 && options.showErrors) {
let msg = response.status == 0 ? "The request was blocked by your browser. Sometimes restarting it can help.\n" : "";
msg += "If this problem persists, please open a new issue at {{https://github.com/Glagan/MyMangaDex/issues}}";
SimpleNotification.error(
{
title: "Couldn't save Online",
image: 'https://ramune.nikurasu.org/mymangadex/128b.png',
text: `Online save responded with error ${response.status}.\n${msg}`,
buttons: [
{
value: 'Retry',
type: 'message',
onClick: (n) => {
n.close();
request();
}
},
{
value: 'Close',
type: 'message',
onClick: (n) => {
n.close();
},
},
],
},
{ sticky: true, position: 'bottom-left' }
);
}
} catch (error) {
options.isLoggedIn = false;
if (options.showErrors) {
SimpleNotification.error(
{
title: "Couldn't save Online",
image: 'https://ramune.nikurasu.org/mymangadex/128b.png',
text:
'Internal Error sending request to Online Service.\nSometimes restarting your browser can help.',
buttons: [
{
value: 'Retry',
type: 'message',
onClick: (n) => {
n.close();
request();
},
},
{
value: 'Close',
type: 'message',
onClick: (n) => {
n.close();
},
},
],
},
{ sticky: true, position: 'bottom-left' }
);
}
console.error(error);
}

if (options.isLoggedIn == false) {
await storageSet('options', options);
if (options.showErrors) {
SimpleNotification.error(
{
title: "Couldn't save Online",
image: 'https://ramune.nikurasu.org/mymangadex/128b.png',
text:
'The Online Service might have a problem, or your credentials have been changed.\nYou have been **logged out**, go to the options to log in again.',
'It seems the Online Service has a problem, or your credentials have been changed.\nYou have been **logged out**, go to the options to log in again\nIf this problem persists, please open a new issue at {{https://github.com/Glagan/MyMangaDex/issues}}',
buttons: {
value: 'Open Options',
type: 'message',
Expand All @@ -379,9 +438,9 @@ async function updateLocalStorage(manga, options) {
{ sticky: true, position: 'bottom-left' }
);
}
await storageSet('options', options);
}
}
};
request();
}

function clearDomNode(node) {
Expand Down

0 comments on commit e0d8786

Please sign in to comment.