--- title: Notes and caveats group: Guides order: 90 summary: How errors work, and conventions to keep in mind. --- ## Errors A function that fails throws an `Error` the script can catch, and writes one line to the system log naming the error, the script, the line and the user: ``` [AGI] FileLibError in MyApp/backend/save.agi:12:20 (user: alice): open user:/Docs/a.txt: no such file or directory ``` ```javascript requirelib("filelib"); try { filelib.writeFile("user:/Docs/a.txt", "hello"); sendOK(); } catch (e) { //e.name is "FileLibError", e.message says what went wrong sendJSONResp({error: e.message}); } ``` An error nobody catches ends the script; the front end receives it as the response (HTTP 500), as for any other script error. Each library throws its own error name, so a `catch` can tell them apart: | Error name | Thrown by | |---|---| | `PermissionDenied` | Any function given a path the user may not read or write (`Path access denied: `), and admin-only functions called by other users | | `AGIError` | Core functions: database, users, `includes`, `execd`, `registerModule`, ... | | `FileLibError`, `ImageLibError`, `ZipLibError`, `HTTPError`, `AppdataError`, `ShareError` | `filelib`, `imagelib`, `ziplib` (7z included), `http`, `appdata`, `share` | | `SQLiteError`, `OfficeError`, `FFmpegError`, `AudioError` | `sqlite`, `office`, `ffmpeg`, `audio` | | `SharedSpaceError`, `MeetRoomError`, `NotificationError`, `SchedulerError`, `WebSocketError` | `sharedspace`, `meetroom`, `notification`, `scheduler`, `websocket` | | `LLMError`, `CNNError`, `ClusterError` | `llm`, `cnn`, `cluster` | Answers to a yes/no question are not failures and stay plain values: `filelib.fileExists` and `filelib.isDir` return `false` for a missing path, `readDBItem` returns `false` for a missing key, `share.getFileShareUUID` returns `null` for a file that is not shared, `imagelib.loadThumbString` returns `false` when there is no thumbnail, `websocket.read` returns `null` on timeout and `false` once the connection is closed, and `websocket.send` returns `false` once it is closed. ## Conventions - `git` and `email` report failures in their result objects (`{success: false, error}`), which the GitApp and Mail apps read, and log them; they do not throw, except `PermissionDenied`. - Errors from background work (`execd`, `websocket.onMessage`, `audio` with `wait: false`) are only logged: there is no script left to catch them. - Admin-only functions (`userExists`, `createUser`, `removeUser`, the `audio` library) throw for other users; check `userIsAdmin()` first when a script serves both. - AGI 4.0 changed failures from `false` / `null` to thrown errors. A script written for 3.x that tests a result for `false` to detect a failure must catch the error instead.