Skip to content
Standard library

velt/io#

import { IoError } from "velt/io". The error type the I/O modules share: files (velt/fs), sockets (velt/net), child processes and WebSockets all throw IoError. The module also holds the streams (ReadableStream and its reader and controller), which are globals and need no import.

ts
import { IoError } from "velt/io";
import { readFileSync } from "velt/fs";

try {
  readFileSync("/definitely/not/here");
} catch (e) {
  console.log(e instanceof IoError, e.code); // true ENOENT
  console.log(e.message); // ENOENT: no such file or directory, open '/definitely/not/here'
}

IoError#

class IoError extends Error { code: string }. code is a Node-style name, so code that checks err.code === "ENOENT" in Node reads the same in Velt:

CodeMeaning
ENOENTno such file or directory
EACCESpermission denied
EEXISTthe file already exists
EISDIR, ENOTDIR, ENOTEMPTYa directory where a file was expected, the other way round, a directory that is not empty
EINVAL, EILSEQan invalid argument, text that is not valid UTF-8
ETIMEDOUT, ECONNREFUSED, ECONNRESET, EADDRINUSE, EPIPEnetwork failures
EOFthe stream ended before the read could finish
EBADFthe socket, file stream, child process or WebSocket was closed
ENOTSUP, UNKNOWNnot supported, anything else

A failed file-system call has Node's message: the code, the operating system's description, the system call and the path, ENOENT: no such file or directory, open 'data.txt' (rename 'a' -> 'b' for two paths, and symlink 'target' -> 'path'). A failed read or write of an opened file names no path (EISDIR: illegal operation on a directory, read). The system calls named are Node's: open, read, write, scandir, stat, lstat, readlink, symlink, mkdir, unlink, rmdir, rm, rename, copyfile. The path is the one you passed (Node on Windows prints the absolute path).

A package that wraps a native library turns its failures into IoErrors with velt/native.

Edit this page on GitHub