@visulima/fs
Version:
Human friendly file system utilities for Node.js
52 lines (51 loc) • 1.91 kB
TypeScript
/**
* Error thrown when a directory is not empty.
* @example
* ```javascript
* import { NotEmptyError } from "@visulima/fs/error";
* import { rmdir } from "node:fs/promises"; // Or any fs function that might throw this system error
* import { join } from "node:path";
*
* const attemptToRemoveNonEmptyDir = async () => {
* const dirPath = join("/tmp", "my-non-empty-dir"); // Assume this directory exists and has files
* try {
* // Forcing the scenario for demonstration, as rmdir might throw its own specific error.
* // Node.js fs operations that encounter a non-empty directory when expecting an empty one
* // typically throw an error with code ENOTEMPTY.
* const simulateNotEmpty = (path) => {
* if (path === dirPath) { // Simulate check for non-empty
* throw new NotEmptyError(`rmdir '${dirPath}'`);
* }
* }
* simulateNotEmpty(dirPath);
* // await rmdir(dirPath); // This would likely throw an error with code ENOTEMPTY
* } catch (error) {
* if (error instanceof NotEmptyError) {
* console.error(`Operation failed, directory is not empty: ${error.message}`);
* console.error(`Error code: ${error.code}`); // ENOTEMPTY
* } else {
* console.error("An unexpected error occurred:", error);
* }
* }
* };
*
* // You would need to set up a non-empty directory at /tmp/my-non-empty-dir for a real test
* // import { ensureDirSync, writeFileSync } from "@visulima/fs";
* // ensureDirSync(dirPath);
* // writeFileSync(join(dirPath, "somefile.txt"), "content");
*
* attemptToRemoveNonEmptyDir();
* ```
*/
declare class NotEmptyError extends Error {
/**
* Creates a new instance.
* @param message
*/
constructor(message: string);
get code(): string;
set code(_name: string);
get name(): string;
set name(_name: string);
}
export default NotEmptyError;