Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error Handling

NicyRuntime provides a robust error handling system with concise output by default and verbose mode for debugging.

Concise Mode (Default)

Errors are displayed in a compact, readable format:

Error: module 'missing_module' not found
  searched:
    ./missing_module.luauc
    ./missing_module.luau
    ./missing_module.lua

Verbose Mode

Enable verbose mode with the NICY_VERBOSE_ERRORS environment variable:

NICY_VERBOSE_ERRORS=1 nicy run broken.luau

Verbose output includes:

  • Full stack trace
  • Require chain (which modules required which)
  • File paths and line numbers
  • Exception details (PowerShell-style formatting)

Error Codes

NicyRuntime extends standard Luau error codes with custom codes for better error categorization.

Standard Luau Codes

CodeNameDescription
0LUA_OKSuccess
1LUA_YIELDCoroutine yielded
2LUA_ERRRUNRuntime error
3LUA_ERRSYNTAXSyntax error
4LUA_ERRMEMMemory error
5LUA_ERRERRError handler error
6LUA_ERRFILEFile error

Nicy-Specific Codes

CodeNameDescriptionLuau Equivalent
100NICY_ERR_MODULE_NOT_FOUNDRequire failed to resolve moduleLUA_ERRFILE
101NICY_ERR_MODULE_LOAD_FAILEDModule found but failed to load/compileLUA_ERRSYNTAX
102NICY_ERR_MODULE_INIT_FAILEDModule loaded but init function failedLUA_ERRRUN
103NICY_ERR_CYCLIC_REQUIRECyclic dependency detectedLUA_ERRRUN
104NICY_ERR_TASK_CRASHTask/coroutine crashedLUA_ERRRUN
105NICY_ERR_NATIVE_CRASHNative DLL crashedLUA_ERRRUN
106NICY_ERR_TIMEOUTOperation timed outLUA_ERRRUN
107NICY_ERR_PERMISSION_DENIEDAccess deniedLUA_ERRFILE

Accessing Error Codes via FFI

When using NicyRuntime via FFI (C, C++, Rust, etc.), errors are returned as integer codes. The mapping is:

// In your C/C++ code:
int error_code = nicy_start("script.luau");

switch (error_code) {
    case 0:   // LUA_OK
        printf("Success\n");
        break;
    case 100: // NICY_ERR_MODULE_NOT_FOUND
        fprintf(stderr, "Module not found\n");
        break;
    case 103: // NICY_ERR_CYCLIC_REQUIRE
        fprintf(stderr, "Cyclic require detected\n");
        break;
    // ... etc
}

🔧 Future: A nicy_error_code() function will be exposed to convert error objects to codes. Currently, error codes are embedded in the error message returned by lua_tostring().

Error Colors

Errors are colorized by default using ANSI escape codes:

  • Red — Error messages
  • Yellow — Warnings
  • Cyan — Info/context

Disabling Colors

NICY_NO_COLOR=1 nicy run script.luau

pcall and Error Suppression

Errors inside pcall are not reported to the console. The error reporter tracks pcall state and suppresses errors accordingly:

local success, err = pcall(function()
    error("This error is caught")
end)

if not success then
    print("Caught: " .. err)
end
-- No error output to console

Require Chain Tracking

When an error occurs inside a require() chain, NicyRuntime tracks the full chain:

RequireChain:
  main.luau → a.luau → b.luau

SEH Crash Protection (Windows)

On Windows, runtime.loadlib() is wrapped in SEH (Structured Exception Handling) to catch access violations during library loading. This prevents the entire process from crashing when a library has bugs.