Skip to content

Commit

Permalink
improved handling of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaorio committed May 19, 2022
1 parent 2ae2c04 commit 1b10692
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/img-gallery-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ export class imgGalleryRenderer extends MarkdownRenderChild {
settingsObj[setting[0]] = setting[1]
})

// check for required settings
if (!settingsObj.path) {
const error = 'Please specify a path!'
this._renderError(error)
throw new Error(error);
}

// store settings, normalize and set sensible defaults
this._settings.path = normalizePath(settingsObj.path)
if (!settingsObj.path) console.error('Please specify a path');

this._settings.type = settingsObj.type || 'horizontal'
this._settings.radius = settingsObj.radius || '0'
Expand All @@ -65,7 +71,11 @@ export class imgGalleryRenderer extends MarkdownRenderChild {

let files
if (folder instanceof TFolder) { files = folder.children }
else console.error('The folder doesn\'t exist, or it\'s empty!')
else {
const error = 'The folder doesn\'t exist, or it\'s empty!'
this._renderError(error)
throw new Error(error);
}

// filter the list of files to make sure we're dealing with images only
const validExtensions = ["jpeg", "jpg", "gif", "png", "webp", ".tiff", ".tif"]
Expand Down Expand Up @@ -137,4 +147,18 @@ export class imgGalleryRenderer extends MarkdownRenderChild {
img.src = uri
})
}

private _renderError(error: string) {
// inject the error wrapper
const wrapper = this.container.createEl('div')
wrapper.style.borderRadius = '4px'
wrapper.style.padding = '2px 16px'
wrapper.style.backgroundColor = '#e50914'
wrapper.style.color = '#fff'

const content = wrapper.createEl('p')
content.style.fontWeight = 'bolder'
const prefix = '(Error) Image Gallery: '
content.innerHTML = `${prefix}${error}`
}
}

0 comments on commit 1b10692

Please sign in to comment.