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

CodeDescription
ENOENTFile or directory not found
EISDIROperation invalid on a directory (e.g. readFile on a dir)
ENOTDIRExpected a directory but found a file
EEXISTFile or directory already exists
ENOTEMPTYDirectory not empty (rm without recursive)
EACCESAccess denied: outside rootDir or permissions disabled
EPERMOperation not permitted (e.g. hard link on a directory)
ELOOPToo many symlink levels (max 40)
EINVALInvalid argument (e.g. copy into own subdirectory)
E2BIGFile 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")
  }
}