Return values (response types)
All Virtual File System (VFS) methods return one of the following response types. Each type provides a consistent way to check for success and retrieve results or error information.
| Type | Description |
|---|---|
respBase | Base type: success/failure, error message |
respSize | Size (in bytes) of a file or directory |
respStat | Metadata for a single file or directory |
respDir | Directory listing (array of items) |
respReadString | File read result as string or hex |
respReadBytes | File read result as byte array |
TIP
Always check the result's Ok() method before using any returned data. If Ok() returns false, use ErrorMsg() to retrieve a human-readable error message.
respBase
This is the ancestor to all possible VFS return values. Every other return value inherits from this one and therefore has the same methods (in addition to those of the derived class).
interface respBase {
function Ok(): boolean; // true if the method succeeded, false otherwise
function ErrorMsg(): string; // if (!Ok()) this method returns a human-readable error message
}IMPORTANT
All other return value types are derived from respBase, so all of them have the Ok() and ErrorMsg() methods even when not explicitly documented.
respSize
Returned by methods like TreeSize(), provides the size (in bytes) of a single file or directory.
interface respSize {
respBase;
function Size(): number; // originally a 64-bit integer in the host process
}respStat
Returned by methods like Stat() or Lstat(), provides metadata for a single file or directory.
interface respStat {
respBase;
function Info(): DirListItem; // exactly 1 (one) DirListItem
}Read more about the structure of the DirListItem object.
respDir
Returned by the ReadDir() method, provides a directory listing as an array of items.
interface respDir {
respBase;
function Infos(): []DirListItem; // array of DirListItem
}Read more about the structure of the DirListItem object.
respReadString
Returned when reading a file as text or as a hexadecimal string.
interface respReadString {
respBase;
function Data(): string;
}respReadBytes
Returned when reading a file as an array of bytes.
interface respReadBytes {
respBase;
function Data(): Uint8Array; // array of unsigned 8-bit integers (bytes)
}Example: Checking for Success and Handling Errors
var stat = vfs.Stat("/some/file.txt");
if (stat.Ok()) {
var info = stat.Info();
// use info here
} else {
Log("Stat failed: " + stat.ErrorMsg());
}