mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
refactoring of server and suppressing error during conversion of attachment into note
This commit is contained in:
parent
a0d958bf12
commit
f78d96a3d4
@ -324,11 +324,12 @@ class Froca {
|
|||||||
// load all attachments for the given note even if one is requested, don't load one by one
|
// load all attachments for the given note even if one is requested, don't load one by one
|
||||||
let attachmentRows;
|
let attachmentRows;
|
||||||
try {
|
try {
|
||||||
attachmentRows = await server.get(`attachments/${attachmentId}/all`);
|
attachmentRows = await server.getWithSilentNotFound(`attachments/${attachmentId}/all`);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (silentNotFoundError) {
|
if (silentNotFoundError) {
|
||||||
logInfo(`Attachment '${attachmentId} not found, but silentNotFoundError is enabled: ` + e.message);
|
logInfo(`Attachment '${attachmentId} not found, but silentNotFoundError is enabled: ` + e.message);
|
||||||
|
return null;
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
@ -28,24 +28,28 @@ async function getHeaders(headers) {
|
|||||||
return allHeaders;
|
return allHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getWithSilentNotFound(url, componentId) {
|
||||||
|
return await call('GET', url, componentId, { silentNotFound: true });
|
||||||
|
}
|
||||||
|
|
||||||
async function get(url, componentId) {
|
async function get(url, componentId) {
|
||||||
return await call('GET', url, null, {'trilium-component-id': componentId});
|
return await call('GET', url, componentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function post(url, data, componentId) {
|
async function post(url, data, componentId) {
|
||||||
return await call('POST', url, data, {'trilium-component-id': componentId});
|
return await call('POST', url, componentId, { data });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function put(url, data, componentId) {
|
async function put(url, data, componentId) {
|
||||||
return await call('PUT', url, data, {'trilium-component-id': componentId});
|
return await call('PUT', url, componentId, { data });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function patch(url, data, componentId) {
|
async function patch(url, data, componentId) {
|
||||||
return await call('PATCH', url, data, {'trilium-component-id': componentId});
|
return await call('PATCH', url, componentId, { data });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function remove(url, componentId) {
|
async function remove(url, componentId) {
|
||||||
return await call('DELETE', url, null, {'trilium-component-id': componentId});
|
return await call('DELETE', url, componentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function upload(url, fileToUpload) {
|
async function upload(url, fileToUpload) {
|
||||||
@ -63,24 +67,29 @@ async function upload(url, fileToUpload) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let i = 1;
|
let idCounter = 1;
|
||||||
const reqResolves = {};
|
const idToRequestMap = {};
|
||||||
const reqRejects = {};
|
|
||||||
|
|
||||||
let maxKnownEntityChangeId = 0;
|
let maxKnownEntityChangeId = 0;
|
||||||
|
|
||||||
async function call(method, url, data, headers = {}) {
|
async function call(method, url, componentId, options = {}) {
|
||||||
let resp;
|
let resp;
|
||||||
|
|
||||||
headers = await getHeaders(headers);
|
const headers = await getHeaders({
|
||||||
|
'trilium-component-id': componentId
|
||||||
|
});
|
||||||
|
const {data} = options;
|
||||||
|
|
||||||
if (utils.isElectron()) {
|
if (utils.isElectron()) {
|
||||||
const ipc = utils.dynamicRequire('electron').ipcRenderer;
|
const ipc = utils.dynamicRequire('electron').ipcRenderer;
|
||||||
const requestId = i++;
|
const requestId = idCounter++;
|
||||||
|
|
||||||
resp = await new Promise((resolve, reject) => {
|
resp = await new Promise((resolve, reject) => {
|
||||||
reqResolves[requestId] = resolve;
|
idToRequestMap[requestId] = {
|
||||||
reqRejects[requestId] = reject;
|
resolve,
|
||||||
|
reject,
|
||||||
|
silentNotFound: !!options.silentNotFound
|
||||||
|
};
|
||||||
|
|
||||||
ipc.send('server-request', {
|
ipc.send('server-request', {
|
||||||
requestId: requestId,
|
requestId: requestId,
|
||||||
@ -92,7 +101,7 @@ async function call(method, url, data, headers = {}) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
resp = await ajax(url, method, data, headers);
|
resp = await ajax(url, method, data, headers, !!options.silentNotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxEntityChangeIdStr = resp.headers['trilium-max-entity-change-id'];
|
const maxEntityChangeIdStr = resp.headers['trilium-max-entity-change-id'];
|
||||||
@ -104,7 +113,7 @@ async function call(method, url, data, headers = {}) {
|
|||||||
return resp.body;
|
return resp.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
function ajax(url, method, data, headers) {
|
function ajax(url, method, data, headers, silentNotFound) {
|
||||||
return new Promise((res, rej) => {
|
return new Promise((res, rej) => {
|
||||||
const options = {
|
const options = {
|
||||||
url: window.glob.baseApiUrl + url,
|
url: window.glob.baseApiUrl + url,
|
||||||
@ -126,7 +135,11 @@ function ajax(url, method, data, headers) {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
error: async jqXhr => {
|
error: async jqXhr => {
|
||||||
await reportError(method, url, jqXhr.status, jqXhr.responseText);
|
if (silentNotFound && jqXhr.status === 404) {
|
||||||
|
// report nothing
|
||||||
|
} else {
|
||||||
|
await reportError(method, url, jqXhr.status, jqXhr.responseText);
|
||||||
|
}
|
||||||
|
|
||||||
rej(jqXhr.responseText);
|
rej(jqXhr.responseText);
|
||||||
}
|
}
|
||||||
@ -153,13 +166,16 @@ if (utils.isElectron()) {
|
|||||||
handleSuccessfulResponse(arg);
|
handleSuccessfulResponse(arg);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
await reportError(arg.method, arg.url, arg.statusCode, arg.body);
|
if (arg.statusCode === 404 && idToRequestMap[arg.requestId]?.silentNotFound) {
|
||||||
|
// report nothing
|
||||||
|
} else {
|
||||||
|
await reportError(arg.method, arg.url, arg.statusCode, arg.body);
|
||||||
|
}
|
||||||
|
|
||||||
reqRejects[arg.requestId]();
|
idToRequestMap[arg.requestId].reject();
|
||||||
}
|
}
|
||||||
|
|
||||||
delete reqResolves[arg.requestId];
|
delete idToRequestMap[arg.requestId];
|
||||||
delete reqRejects[arg.requestId];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function handleSuccessfulResponse(arg) {
|
function handleSuccessfulResponse(arg) {
|
||||||
@ -167,12 +183,12 @@ if (utils.isElectron()) {
|
|||||||
arg.body = JSON.parse(arg.body);
|
arg.body = JSON.parse(arg.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(arg.requestId in reqResolves)) {
|
if (!(arg.requestId in idToRequestMap)) {
|
||||||
// this can happen when reload happens between firing up the request and receiving the response
|
// this can happen when reload happens between firing up the request and receiving the response
|
||||||
throw new Error(`Unknown requestId="${arg.requestId}"`);
|
throw new Error(`Unknown requestId '${arg.requestId}'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
reqResolves[arg.requestId]({
|
idToRequestMap[arg.requestId].resolve({
|
||||||
body: arg.body,
|
body: arg.body,
|
||||||
headers: arg.headers
|
headers: arg.headers
|
||||||
});
|
});
|
||||||
@ -180,7 +196,6 @@ if (utils.isElectron()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function reportError(method, url, statusCode, response) {
|
async function reportError(method, url, statusCode, response) {
|
||||||
const toastService = (await import("./toast.js")).default;
|
|
||||||
let message = response;
|
let message = response;
|
||||||
|
|
||||||
if (typeof response === 'string') {
|
if (typeof response === 'string') {
|
||||||
@ -191,6 +206,8 @@ async function reportError(method, url, statusCode, response) {
|
|||||||
catch (e) {}
|
catch (e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const toastService = (await import("./toast.js")).default;
|
||||||
|
|
||||||
if ([400, 404].includes(statusCode) && response && typeof response === 'object') {
|
if ([400, 404].includes(statusCode) && response && typeof response === 'object') {
|
||||||
toastService.showError(message);
|
toastService.showError(message);
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
@ -208,6 +225,7 @@ async function reportError(method, url, statusCode, response) {
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
get,
|
get,
|
||||||
|
getWithSilentNotFound,
|
||||||
post,
|
post,
|
||||||
put,
|
put,
|
||||||
patch,
|
patch,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user