From 746bd5abd042e9fe04f88cd4dd2dec00d900ba08 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 5 Aug 2018 10:50:47 -0600 Subject: [PATCH] * Don't crash with bad string formats * File listing: If we fail to get an archive listing, fix attempt to format the string with a non-object --- core/file_area_list.js | 16 ++++++++++++---- core/string_format.js | 26 +++++++++++++++++--------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/core/file_area_list.js b/core/file_area_list.js index e72589c3..35ce3ea6 100644 --- a/core/file_area_list.js +++ b/core/file_area_list.js @@ -589,15 +589,22 @@ exports.getModule = class FileAreaList extends MenuModule { }); } + setFileListNoListing(text) { + const fileListView = this.viewControllers.detailsFileList.getView(MciViewIds.detailsFileList.fileList); + if(fileListView) { + fileListView.complexItems = false; + fileListView.setItems( [ text ] ); + fileListView.redraw(); + } + } + populateFileListing() { const fileListView = this.viewControllers.detailsFileList.getView(MciViewIds.detailsFileList.fileList); if(this.currentFileEntry.entryInfo.archiveType) { this.cacheArchiveEntries( (err, cacheStatus) => { if(err) { - // :TODO: Handle me!!! - fileListView.setItems( [ 'Failed getting file listing' ] ); // :TODO: make this not suck - return; + return this.setFileListNoListing('Failed to get file listing'); } if('re-cached' === cacheStatus) { @@ -606,7 +613,8 @@ exports.getModule = class FileAreaList extends MenuModule { } }); } else { - fileListView.setItems( [ stringFormat(this.menuConfig.config.notAnArchiveFormat || 'Not an archive', { fileName : this.currentFileEntry.fileName } ) ] ); + const notAnArchiveFileName = stringFormat(this.menuConfig.config.notAnArchiveFormat || 'Not an archive', { fileName : this.currentFileEntry.fileName } ); + this.setFileListNoListing(notAnArchiveFileName); } } diff --git a/core/string_format.js b/core/string_format.js index cef937c4..a756db72 100644 --- a/core/string_format.js +++ b/core/string_format.js @@ -331,17 +331,25 @@ module.exports = function format(fmt, obj) { transformer = match[2]; formatSpec = match[3]; - value = getValue(obj, objPath); - if(transformer) { - value = transformValue(transformer, value); - } + try { + value = getValue(obj, objPath); + if(transformer) { + value = transformValue(transformer, value); + } - tokens = tokenizeFormatSpec(formatSpec || ''); + tokens = tokenizeFormatSpec(formatSpec || ''); - if(_.isNumber(value)) { - out += formatNumber(value, tokens); - } else { - out += formatString(value, tokens); + if(_.isNumber(value)) { + out += formatNumber(value, tokens); + } else { + out += formatString(value, tokens); + } + } catch(e) { + if(e instanceof KeyError) { + out += match[0]; // preserve full thing + } else if(e instanceof ValueError) { + out += value.toString(); + } } }