Skip to content

Commit

Permalink
add error handling to resource editor - so its harder to get stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Sep 6, 2024
1 parent 6d8907d commit 5c41a69
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/js/classes/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export const StorageJs = (type = 'gist') => {
// try to fetch without authorisation
return fetch(fetchAddress).then(data=> {
console.warn("Got data without authorisation")
this.isTokenInvalid = true;
// this.isTokenInvalid = true;
return data.json()
})
}
Expand Down Expand Up @@ -318,7 +318,7 @@ export const StorageJs = (type = 'gist') => {
}).then(result=> result.json()).then(response => {
this.setLastStorageHost('GIST');
const file = response.files && fileName in response.files ? response.files[fileName] : undefined;
return { response, file };
return { response, file, gistId: this.gistId };
});
},
editGistFile: function(fileName, content) {
Expand Down
121 changes: 79 additions & 42 deletions src/public/plugins/resources-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export var ResourcesEditor = function({
return true;
})
this.getFromGist = () => {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
// todo copypasta
app.data.storage.getGistFiles(app.ui.openSettingsDialog).then(({ filesInGist }) => {
app.data.storage.getGistFiles(reject).then(({ filesInGist }) => {
const fileFound = Object.values(filesInGist).find(
item => item.filename === 'resources.json'
);
Expand Down Expand Up @@ -56,12 +56,9 @@ export var ResourcesEditor = function({
})
}
// resource in local or at gist
this.initResourcesFile = () => {
return new Promise(resolve => {
this.initResourcesFile = (createVolatile = false) => {
return new Promise((resolve, reject) => {
this.getVloatileResource().then(volatile => {
const volatileData = {};
console.log('------------', {volatile, volatileData})

if(volatile && volatile.content) {
ToastWc.show({
type: 'success',
Expand All @@ -71,48 +68,63 @@ export var ResourcesEditor = function({
console.log({volatile})
resolve(volatile);
return
} else {
app.data.storage.getGistFiles(app.ui.openSettingsDialog).then(({ filesInGist }) => {
const fileFound = Object.values(filesInGist).find(
item => item.filename === 'resources.json'
);
if (!fileFound) {
app.data.storage.editGistFile('resources.json', '{}').then(({file})=>{
console.log({result})
ToastWc.show({
type: 'success',
content: `Created a resources.json file`,
time: 3000,
});
this.resourcesFileUrl = file.raw_url;
this.setVolatileResource(file);
resolve(file);
});
} else {
this.resourcesFileUrl = fileFound.raw_url;
return app.data.storage
.getContentOrRaw(fileFound.content, fileFound.raw_url)
.then(content => {
const fileWithContent = { ...fileFound, content };
this.setVolatileResource(fileWithContent);
resolve(fileWithContent)
});
}
});
}
if(createVolatile) {
const newFile = {
filename:'resources.json',
content: '{}',
}
this.setVolatileResource(newFile);
resolve(newFile);
return
}
app.data.storage.getGistFiles(reject).then(({ filesInGist }) => {
const fileFound = Object.values(filesInGist).find(
item => item.filename === 'resources.json'
);
if (!fileFound) {
app.data.storage.editGistFile('resources.json', '{}').then(({file})=>{
ToastWc.show({
type: 'success',
content: `Created a resources.json file`,
time: 3000,
});
this.resourcesFileUrl = file.raw_url;
this.setVolatileResource(file);
resolve(file);
});
} else {
this.resourcesFileUrl = fileFound.raw_url;
return app.data.storage
.getContentOrRaw(fileFound.content, fileFound.raw_url)
.then(content => {
const fileWithContent = { ...fileFound, content };
this.setVolatileResource(fileWithContent);
resolve(fileWithContent)
});
}
});
})
});
};

this.onCommitResourceFiles = newContent => {
this.isBusy('Uploading changes to gist...');
app.data.storage.editGistFile('resources.json', newContent).then(() => {
ToastWc.show({
type: 'info',
content: 'Saved resources on gist',
time: 1000,
});
document.querySelector('resources-component').setIsNew(false)
app.data.storage.editGistFile('resources.json', newContent).then(({response, gistId}) => {
if(response.ok){
ToastWc.show({
type: 'info',
content: 'Saved resources on gist',
time: 1000,
});
document.querySelector('resources-component').setIsNew(false);
} else {
ToastWc.show({
type: 'error',
content: `Could not save changes to ${gistId}!`,
time: 3000,
});
}
this.isBusy('')
});
};
Expand Down Expand Up @@ -164,6 +176,13 @@ export var ResourcesEditor = function({
time: 3000,
});
this.isBusy('');
}).catch(() => {
ToastWc.show({
type: 'error',
content: `Could not Load changed from Gist file!`,
time: 3000,
});
this.isBusy('');
})
}
if(action === 'push'){
Expand All @@ -184,6 +203,24 @@ export var ResourcesEditor = function({
darkMode: app.settings.theme() === 'dracula',
headerButtons: [{title: 'Load from gist', action: 'pull'}, {title: 'Save to gist', action: 'push'}]
})
}). catch(error=> {
ToastWc.show({
type: 'error',
content: error,
time: 2000
}).then(()=> {
ToastWc.show({
type: 'info',
content: 'Yarn will store resources in cache for now',
time: 2000
})
});
this.isBusy('');
this.initResourcesFile(true);
// this.setVolatileResource({
// filename:'resources.json',
// content: '', // todo not having raw_url can be used to detect if new file
// });
});
},
preConfirm: () => {
Expand Down

0 comments on commit 5c41a69

Please sign in to comment.