diff --git a/indax.cjs b/indax.cjs index d199f41..0b6748b 100644 --- a/indax.cjs +++ b/indax.cjs @@ -89,7 +89,7 @@ class SmbDirectoryHandle extends SmbHandle { if (errMsg == 'The path supplied exists, but was not an entry of requested type.') { reason.name = 'TypeMismatchError'; } - else if (errMsg.indexOf('not found') != -1) { + else if (errMsg.indexOf('not found') != -1 || errMsg.indexOf('ENOENT') != -1) { reason.name = 'NotFoundError'; } } @@ -107,7 +107,7 @@ class SmbDirectoryHandle extends SmbHandle { if (errMsg == 'The path supplied exists, but was not an entry of requested type.') { reason.name = 'TypeMismatchError'; } - else if (errMsg.indexOf('not found') != -1) { + else if (errMsg.indexOf('not found') != -1 || errMsg.indexOf('ENOENT') != -1) { reason.name = 'NotFoundError'; } } @@ -149,13 +149,35 @@ class SmbFileHandle extends SmbHandle { throw Error('createSyncAccessHandle not implemented'); } async getFile() { - return this._js.getFile(); + return new Promise(async (resolve, reject) => { + await this._js.getFile() + .then((file) => resolve(file)) + .catch((reason) => { + let errMsg = reason.message; + if (errMsg !== undefined) { + if (errMsg.indexOf('not found') != -1 || errMsg.indexOf('ENOENT') != -1) { + reason.message = `File "${this.name}" not found`; + reason.name = 'NotFoundError'; + } + } + reject(reason); + }); + }); } async createWritable(options) { return new Promise(async (resolve, reject) => { await this._js.createWritable(options) .then((stream) => resolve(new SmbWritableFileStream(stream))) - .catch((reason) => reject(reason)); + .catch((reason) => { + let errMsg = reason.message; + if (errMsg !== undefined) { + if (errMsg.indexOf('not found') != -1 || errMsg.indexOf('ENOENT') != -1) { + reason.message = `File "${this.name}" not found`; + reason.name = 'NotFoundError'; + } + } + reject(reason); + }); }); } } diff --git a/indax.ts b/indax.ts index 34f4780..6120453 100644 --- a/indax.ts +++ b/indax.ts @@ -107,7 +107,7 @@ export class SmbDirectoryHandle extends SmbHandle implements FileSystemDirectory if (errMsg !== undefined) { if (errMsg == 'The path supplied exists, but was not an entry of requested type.') { reason.name = 'TypeMismatchError'; - } else if (errMsg.indexOf('not found') != -1) { + } else if (errMsg.indexOf('not found') != -1 || errMsg.indexOf('ENOENT') != -1) { reason.name = 'NotFoundError'; } } @@ -124,7 +124,7 @@ export class SmbDirectoryHandle extends SmbHandle implements FileSystemDirectory if (errMsg !== undefined) { if (errMsg == 'The path supplied exists, but was not an entry of requested type.') { reason.name = 'TypeMismatchError'; - } else if (errMsg.indexOf('not found') != -1) { + } else if (errMsg.indexOf('not found') != -1 || errMsg.indexOf('ENOENT') != -1) { reason.name = 'NotFoundError'; } } @@ -172,13 +172,35 @@ export class SmbFileHandle extends SmbHandle implements FileSystemFileHandle { } async getFile(): Promise { - return this._js.getFile(); + return new Promise(async (resolve, reject) => { + await this._js.getFile() + .then((file) => resolve(file)) + .catch((reason) => { + let errMsg: string = reason.message; + if (errMsg !== undefined) { + if (errMsg.indexOf('not found') != -1 || errMsg.indexOf('ENOENT') != -1) { + reason.message = `File "${this.name}" not found`; + reason.name = 'NotFoundError'; + } + } + reject(reason); + }); + }); } async createWritable(options?: SmbCreateWritableOptions): Promise { return new Promise(async (resolve, reject) => { await this._js.createWritable(options as JsSmbCreateWritableOptions) .then((stream) => resolve(new SmbWritableFileStream(stream) as FileSystemWritableFileStream)) - .catch((reason) => reject(reason)); + .catch((reason) => { + let errMsg: string = reason.message; + if (errMsg !== undefined) { + if (errMsg.indexOf('not found') != -1 || errMsg.indexOf('ENOENT') != -1) { + reason.message = `File "${this.name}" not found`; + reason.name = 'NotFoundError'; + } + } + reject(reason); + }); }); } } diff --git a/libsmb2-rs/src/lib.rs b/libsmb2-rs/src/lib.rs index 444bd5a..f6e0b67 100644 --- a/libsmb2-rs/src/lib.rs +++ b/libsmb2-rs/src/lib.rs @@ -123,8 +123,13 @@ fn check_mut_ptr(ptr: *mut T) -> Result<*mut T> { fn check_retcode(ctx: *mut smb2_context, code: i32) -> Result<()> { if code < 0 { unsafe { - let err_str = smb2_get_error(ctx); - let e = CStr::from_ptr(err_str).to_string_lossy().into_owned(); + let err_ptr = smb2_get_error(ctx); + let err_str = CStr::from_ptr(err_ptr).to_string_lossy().into_owned(); + let e = if !err_str.is_empty() { + err_str + } else { + nix::errno::Errno::from_raw(-code).to_string() + }; Err(Error::new(ErrorKind::Other, e)) } } else {