common/errors

Unified error handling helpers

This module provides a simple wrapper around sigmaDeviceManager’s
error message API. It allows scripts to:

  • Set the current error message
  • Retrieve the last error message
  • Throw an error while recording it in the device manager

Usage:

var { setError, lastError, throwError } = require("sigma/common/errors");

// Record an error without throwing
setError("Device not found");

// Retrieve the most recent error
console.log(lastError());

// Record and throw an error
throwError("Connection failed");

(inner) lastError() → {string}

Get the most recent error message.

Example
// Example: Attempt to close an app by package name

// Get the current master device object
var device = Device.getMain();
if (device != null) {

    // Execute this JS API
    const ret = device.closeApp("com.tencent.qqmusic1");
    if (ret !== -1) {
        print("Congratulations, this API executes successfully.\nReturn value: " + ret);
    } else {
        print("Sorry! " + lastError() + "\nReturn value: " + ret);
    }
} else {
    print("Failed to get the master device object");
}

// Operation Result (if the app fails to close):
Sorry! The package name does not exist.
Return value: -1
Returns:
string

Error message.

(inner) setError(err)

Set the error message in sigmaDeviceManager

Example
setError("Device not found");
// sigmaDeviceManager now holds "Device not found"
Parameters:
string err

The error message

(inner) throwError(msg)

Set and throw an error
Records the error in sigmaDeviceManager and then throws a JavaScript Error

Example
try {
  throwError("Connection failed");
} catch (e) {
  console.error(e.message);
  // -> "Connection failed"
}
Parameters:
string msg

The error message

Throws:
Error