Errors
Standard error handling for Cosmo Widgets.
CosmoError
The CosmoError class is thrown when a native bridge request fails. It provides structured error information to help you debug issues.
Structure
class CosmoError extends Error {
type: string; // The category of the error (e.g., "PERMISSION_DENIED", "INTERNAL_ERROR")
code: string; // A specific error code
message: string; // A human-readable description
}Usage
You should wrap asynchronous SDK calls in try/catch blocks to handle potential errors, especially when dealing with permissions or hardware resources.
import { systemResources, errors } from "@buildcosmo/widget";
try {
const cpu = await systemResources.getSystemCpu();
console.log("CPU:", cpu);
} catch (error) {
if (error instanceof errors.CosmoError) {
console.error(`SDK Error [${error.code}]: ${error.message}`);
} else {
console.error("Unexpected error:", error);
}
}