BREAKING(fs): throw Deno.errors.NotSupported instead of SubdirectoryMoveError in move[Sync]() (#5532)

* BREAKING(fs): replace `SubdirectoryMoveError` exception with `Deno.errors.InvalidData`

* NotSupported
This commit is contained in:
Asher Gomez 2024-07-25 15:53:22 +10:00 committed by GitHub
parent 42693da902
commit d6a6d8b806
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 47 deletions

View File

@ -4,38 +4,6 @@ import { isSamePath } from "./_is_same_path.ts";
const EXISTS_ERROR = new Deno.errors.AlreadyExists("dest already exists."); const EXISTS_ERROR = new Deno.errors.AlreadyExists("dest already exists.");
/**
* Error thrown in {@linkcode move} or {@linkcode moveSync} when the destination
* is a subdirectory of the source.
*
* @example Usage
* ```ts no-eval
* import { move, SubdirectoryMoveError } from "@std/fs/move";
*
* try {
* await move("./foo", "./foo/bar");
* } catch (error) {
* if (error instanceof SubdirectoryMoveError) {
* console.error(error.message);
* }
* }
* ```
*/
export class SubdirectoryMoveError extends Error {
/**
* Constructs a new instance.
*
* @param src The source file or directory as a string or URL.
* @param dest The destination file or directory as a string or URL.
*/
constructor(src: string | URL, dest: string | URL) {
super(
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
);
this.name = this.constructor.name;
}
}
/** Options for {@linkcode move} and {@linkcode moveSync}. */ /** Options for {@linkcode move} and {@linkcode moveSync}. */
export interface MoveOptions { export interface MoveOptions {
/** /**
@ -49,9 +17,6 @@ export interface MoveOptions {
/** /**
* Asynchronously moves a file or directory (along with its contents). * Asynchronously moves a file or directory (along with its contents).
* *
* If `src` is a sub-directory of `dest`, a {@linkcode SubdirectoryMoveError}
* will be thrown.
*
* Requires `--allow-read` and `--allow-write` permissions. * Requires `--allow-read` and `--allow-write` permissions.
* *
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access} * @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
@ -60,6 +25,9 @@ export interface MoveOptions {
* @param src The source file or directory as a string or URL. * @param src The source file or directory as a string or URL.
* @param dest The destination file or directory as a string or URL. * @param dest The destination file or directory as a string or URL.
* @param options Options for the move operation. * @param options Options for the move operation.
* @throws {Deno.errors.AlreadyExists} If `dest` already exists and
* `options.overwrite` is `false`.
* @throws {Deno.errors.NotSupported} If `src` is a sub-directory of `dest`.
* *
* @returns A void promise that resolves once the operation completes. * @returns A void promise that resolves once the operation completes.
* *
@ -96,7 +64,9 @@ export async function move(
srcStat.isDirectory && srcStat.isDirectory &&
(isSubdir(src, dest) || isSamePath(src, dest)) (isSubdir(src, dest) || isSamePath(src, dest))
) { ) {
throw new SubdirectoryMoveError(src, dest); throw new Deno.errors.NotSupported(
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
);
} }
if (overwrite) { if (overwrite) {
@ -123,9 +93,6 @@ export async function move(
/** /**
* Synchronously moves a file or directory (along with its contents). * Synchronously moves a file or directory (along with its contents).
* *
* If `src` is a sub-directory of `dest`, a {@linkcode SubdirectoryMoveError}
* will be thrown.
*
* Requires `--allow-read` and `--allow-write` permissions. * Requires `--allow-read` and `--allow-write` permissions.
* *
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access} * @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
@ -134,6 +101,9 @@ export async function move(
* @param src The source file or directory as a string or URL. * @param src The source file or directory as a string or URL.
* @param dest The destination file or directory as a string or URL. * @param dest The destination file or directory as a string or URL.
* @param options Options for the move operation. * @param options Options for the move operation.
* @throws {Deno.errors.AlreadyExists} If `dest` already exists and
* `options.overwrite` is `false`.
* @throws {Deno.errors.NotSupported} If `src` is a sub-directory of `dest`.
* *
* @returns A void value that returns once the operation completes. * @returns A void value that returns once the operation completes.
* *
@ -170,7 +140,9 @@ export function moveSync(
srcStat.isDirectory && srcStat.isDirectory &&
(isSubdir(src, dest) || isSamePath(src, dest)) (isSubdir(src, dest) || isSamePath(src, dest))
) { ) {
throw new SubdirectoryMoveError(src, dest); throw new Deno.errors.NotSupported(
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
);
} }
if (overwrite) { if (overwrite) {

View File

@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert"; import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
import * as path from "@std/path"; import * as path from "@std/path";
import { move, moveSync, SubdirectoryMoveError } from "./move.ts"; import { move, moveSync } from "./move.ts";
import { ensureFile, ensureFileSync } from "./ensure_file.ts"; import { ensureFile, ensureFileSync } from "./ensure_file.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { existsSync } from "./exists.ts"; import { existsSync } from "./exists.ts";
@ -416,9 +416,13 @@ Deno.test("move() accepts overwrite option set to true for directories", async f
const src = p[0]; const src = p[0];
const dest = p[1]; const dest = p[1];
await assertRejects(async () => { await assertRejects(
await move(src, dest); async () => {
}, SubdirectoryMoveError); await move(src, dest);
},
Deno.errors.NotSupported,
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
);
} }
await Deno.remove(dir, { recursive: true }); await Deno.remove(dir, { recursive: true });
@ -474,9 +478,13 @@ Deno.test("move() accepts overwrite option set to true for directories", functio
const src = p[0]; const src = p[0];
const dest = p[1]; const dest = p[1];
assertThrows(() => { assertThrows(
moveSync(src, dest); () => {
}, SubdirectoryMoveError); moveSync(src, dest);
},
Deno.errors.NotSupported,
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
);
} }
Deno.removeSync(dir, { recursive: true }); Deno.removeSync(dir, { recursive: true });