From d2e59cf7b696616367182e6ddc2be4e45be6560a Mon Sep 17 00:00:00 2001 From: WFH Brian Date: Tue, 3 Dec 2024 07:47:40 -0500 Subject: [PATCH] Refactor SmartSource import logic to improve error handling and file size checks - Added error handling for file statistics retrieval, ensuring that errors are properly managed during import. - Updated file size check to use a local variable for better readability. - Enhanced import error handling to differentiate between non-existent files and other errors, allowing for appropriate actions (re-queueing or deletion). - Improved logging for better debugging and user feedback during import operations. --- smart-sources/smart_source.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/smart-sources/smart_source.js b/smart-sources/smart_source.js index f821776f..e2e477e8 100644 --- a/smart-sources/smart_source.js +++ b/smart-sources/smart_source.js @@ -48,7 +48,9 @@ export class SmartSource extends SmartEntity { async import(){ this._queue_import = false; try{ - if(this.file_type === 'md' && this.file.stat.size > 1000000) { + const stat = this.file.stat; + if(stat.error) throw stat.error; + if(this.file_type === 'md' && stat.size > 1000000) { console.log(`Smart Connections: Skipping large file: ${this.path}`); return; } @@ -63,15 +65,20 @@ export class SmartSource extends SmartEntity { if(this.meta_changed){ this.data.blocks = null; await this.save(super.ajson); - this.data.mtime = this.file.stat.mtime; - this.data.size = this.file.stat.size; + this.data.mtime = stat.mtime; + this.data.size = stat.size; await this.source_adapter.import(); this.loaded_at = Date.now(); // Reset loaded_at to now to prevent unneeded reloads this.queue_embed(); } // else console.log(`Smart Connections: No changes to ${this.path}`); }catch(err){ - this.queue_import(); - console.error(err, err.stack); + if(err.code === "ENOENT"){ + console.log(`Smart Connections: Deleting ${this.path} data because it no longer exists on disk`); + this.delete(); + }else{ + console.warn("Smart Connections: Error during import: re-queueing import", err); + this.queue_import(); + } } }