Skip to content

Commit

Permalink
client: Make handleAjaxError only handle HTTP errors
Browse files Browse the repository at this point in the history
It should not capture errors in template for example.
  • Loading branch information
jtojnar committed Dec 17, 2020
1 parent 12ff916 commit 539ffed
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
7 changes: 7 additions & 0 deletions assets/js/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ export class TimeoutError extends Error {
this.name = 'TimeoutError';
}
}

export class HttpError extends Error {
constructor(message) {
super(message);
this.name = 'HttpError';
}
}
4 changes: 2 additions & 2 deletions assets/js/helpers/ajax.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import formurlencoded from 'form-urlencoded';
import mergeDeepLeft from 'ramda/src/mergeDeepLeft.js';
import pipe from 'ramda/src/pipe.js';
import { TimeoutError } from '../errors';
import { HttpError, TimeoutError } from '../errors';

/**
* Passing this function as a Promise handler will make the promise fail when the predicate is not true.
Expand All @@ -10,7 +10,7 @@ export const rejectUnless = (pred) => (response) => {
if (pred(response)) {
return response;
} else {
let err = new Error(response.statusText);
let err = new HttpError(response.statusText);
err.response = response;
throw err;
}
Expand Down
15 changes: 11 additions & 4 deletions assets/js/selfoss-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getInstanceInfo, login, logout } from './requests/common';
import * as itemsRequests from './requests/items';
import { getAllTags } from './requests/tags';
import * as ajax from './helpers/ajax';
import { HttpError, TimeoutError } from './errors';

/**
* base javascript application
Expand Down Expand Up @@ -566,14 +567,14 @@ var selfoss = {
selfoss.db.setOnline();
displayNextUnread();
}).catch(function(error) {
selfoss.handleAjaxError(error?.response?.status || 0).then(function() {
selfoss.handleAjaxError(error).then(function() {
let statuses = ids.map(id => ({
entryId: id,
name: 'unread',
value: false
}));
selfoss.dbOffline.enqueueStatuses(statuses);
}).catch(function() {
}).catch(function(error) {
content.html(articleList);
selfoss.ui.refreshStreamButtons(true, hadMore);
selfoss.ui.listReady();
Expand All @@ -583,15 +584,21 @@ var selfoss = {
},


handleAjaxError: function(httpCode, tryOffline = true) {
handleAjaxError: function(error, tryOffline = true) {
if (!(error instanceof HttpError || error instanceof TimeoutError)) {
throw error;
}

const httpCode = error?.response?.status || 0;

if (tryOffline && httpCode != 403) {
return selfoss.db.setOffline();
} else {
if (httpCode == 403) {
selfoss.ui.logout();
selfoss.ui.showLogin(selfoss.ui._('error_session_expired'));
}
return Promise.reject();
throw error;
}
},

Expand Down
6 changes: 3 additions & 3 deletions assets/js/selfoss-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ selfoss.dbOnline = {
}
}).catch(function(error) {
selfoss.dbOnline._syncDone(false);
selfoss.handleAjaxError(error?.response?.status || 0).catch(function() {
selfoss.handleAjaxError(error).catch(function(error) {
selfoss.ui.showError(selfoss.ui._('error_sync') + ' ' + error.message);
});
}).finally(function() {
Expand Down Expand Up @@ -282,10 +282,10 @@ selfoss.dbOnline = {
return;
}

selfoss.handleAjaxError(error?.response?.status || 0).then(function() {
selfoss.handleAjaxError(error).then(function() {
selfoss.dbOffline.reloadList();
selfoss.ui.afterReloadList();
}).catch(() => {
}).catch(function(error) {
selfoss.ui.showError(selfoss.ui._('error_loading') + ' ' + error.message);
selfoss.events.entries();
selfoss.ui.refreshStreamButtons();
Expand Down
12 changes: 6 additions & 6 deletions assets/js/selfoss-events-entriestoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ selfoss.events.entriesToolbar = function(parent) {

itemsRequests.starr(id, starr).then(() => {
selfoss.db.setOnline();
}).catch((error) => {
selfoss.handleAjaxError(error?.response?.status || 0).then(function() {
}).catch(function(error) {
selfoss.handleAjaxError(error).then(function() {
selfoss.dbOffline.enqueueStatus(id, 'starred', starr);
}).catch(function() {
}).catch(function(error) {
// rollback ui changes
selfoss.ui.entryStar(id, !starr);
updateStats(!starr);
Expand Down Expand Up @@ -151,10 +151,10 @@ selfoss.events.entriesToolbar = function(parent) {

itemsRequests.mark(id, !unread).then(() => {
selfoss.db.setOnline();
}).catch((error) => {
selfoss.handleAjaxError(error?.response?.status || 0).then(function() {
}).catch(function(error) {
selfoss.handleAjaxError(error).then(function() {
selfoss.dbOffline.enqueueStatus(id, 'unread', !unread);
}).catch(function() {
}).catch(function(error) {
// rollback ui changes
selfoss.ui.entryMark(id, unread);
updateStats(!unread);
Expand Down
2 changes: 1 addition & 1 deletion assets/js/selfoss-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ selfoss.events = {
return;
}

selfoss.handleAjaxError(error?.response?.status || 0, false).catch(function() {
selfoss.handleAjaxError(error, false).catch(function(error) {
selfoss.ui.showError(selfoss.ui._('error_loading') + ' ' + error.message);
});
}).finally(() => {
Expand Down

0 comments on commit 539ffed

Please sign in to comment.