Errors
BashGres uses two error classes: FsError for filesystem operations and SqlError for database-level errors.
FsError
Thrown by all filesystem operations. The error message follows the format CODE: operation, 'path'.
import { FsError } from "bash-gres"
try {
await fs.readFile("/nonexistent")
} catch (e) {
if (e instanceof FsError) {
console.log(e.code) // "ENOENT"
console.log(e.op) // "readFile"
console.log(e.path) // "/nonexistent"
console.log(e.message) // "ENOENT: readFile, '/nonexistent'"
}
}Error Codes
| Code | Description |
|---|---|
| ENOENT | File or directory not found |
| EISDIR | Operation invalid on a directory (e.g. readFile on a dir) |
| ENOTDIR | Expected a directory but found a file |
| EEXIST | File or directory already exists |
| ENOTEMPTY | Directory not empty (rm without recursive) |
| EACCES | Access denied: outside rootDir or permissions disabled |
| EPERM | Operation not permitted (e.g. hard link on a directory) |
| ELOOP | Too many symlink levels (max 40) |
| EINVAL | Invalid argument (e.g. copy into own subdirectory) |
| E2BIG | File too large to read (exceeds maxReadSize) |
SqlError
Wraps PostgreSQL errors from the underlying driver. Includes the native error code for precise handling.
import { SqlError } from "bash-gres"
try {
await fs.writeFile("/file.txt", "content")
} catch (e) {
if (e instanceof SqlError) {
console.log(e.code) // PostgreSQL error code (e.g. "23505")
console.log(e.detail) // error detail
console.log(e.constraint) // constraint name if applicable
console.log(e.cause) // original driver error
}
}Common Patterns
import { FsError } from "bash-gres"
// Check if file exists before reading
try {
const content = await fs.readFile("/config.json")
} catch (e) {
if (e instanceof FsError && e.code === "ENOENT") {
// file doesn't exist, use default config
}
}
// Force remove (ignore if not found)
await fs.rm("/tmp/cache", { recursive: true, force: true })
// Guard against write in read-only mode
try {
await fs.writeFile("/protected/data.txt", "new data")
} catch (e) {
if (e instanceof FsError && e.code === "EACCES") {
console.error("Write permission denied")
}
}