Skip to content

Commit

Permalink
refactor: improve error handling in copyTo method
Browse files Browse the repository at this point in the history
  • Loading branch information
hughfenghen committed Dec 19, 2024
1 parent b4bd4b5 commit 1186860
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-radios-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'opfs-tools': patch
---

refactor: improve error handling in copyTo method
9 changes: 4 additions & 5 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,15 @@ export class OPFSFileWrap {
* if the target is a folder, copy the current file into that folder.
*/
async copyTo(target: OPFSDirWrap | OPFSFileWrap): Promise<OPFSFileWrap> {
if (!(await this.exists())) {
throw Error(`file ${this.path} not exists`);
}

if (target instanceof OPFSFileWrap) {
if (file(target.path) === this) return this;
if (target.path === this.path) return this;

await write(target.path, this);
return file(target.path);
} else if (target instanceof OPFSDirWrap) {
if (!(await this.exists())) {
throw Error(`file ${this.path} not exists`);
}
return await this.copyTo(file(joinPath(target.path, this.name)));
}
throw Error('Illegal target type');
Expand Down

0 comments on commit 1186860

Please sign in to comment.