Skip to content

Error Handling ​

This guide covers the TResult error handling pattern, including type structure, usage, error code design, and best practices.

TResult Design Philosophy ​

All async public methods in the SDK return TResult<T> — never throw:

  • Expected errors are expressed via result.ok === false
  • Localized messages via result.message can be shown directly to users
  • Debug info via result.detail is provided for developers
  • Error codes via result.code support programmatic handling

TResult<T> Type Structure ​

typescript
// Success result
interface TResultOk<T> {
  ok: true;
  data: T;           // Return data on success
  code: 0;
  message: '';
  detail: '';
}

// Failure result
interface TResultFail {
  ok: false;
  data: undefined;
  code: TResultCode; // Error code enum
  message: string;   // Localized message (show directly to users)
  detail: string;    // English debug info (for developer troubleshooting)
}

// Union type
type TResult<T = void> = TResultOk<T> | TResultFail;

Basic Usage ​

typescript
// Standard usage: check ok before accessing data
const result = await classroom.joinClass(params);
if (!result.ok) {
  showToast(result.message);           // Show to user
  console.error(result.detail);        // Developer debug
  console.error('Error code:', result.code); // Programmatic handling
  return;
}
// Success branch — TypeScript auto-narrows type, result.data safely accessible
console.log('Joined successfully');

Methods with Return Data ​

typescript
const result = await classroom.getCameraList();
if (result.ok) {
  // result.data is typed as TDeviceInfo[]
  const cameras = result.data;
  cameras.forEach(cam => console.log(cam.deviceId, cam.label));
}

TResultCode Segment Design ​

Error codes are segmented by module for quick problem source identification:

RangeModuleDescription
1000 ~ 1999FrameworkInitialization, module registration, lifecycle
10000 ~ 10999APINetwork requests, server-returned errors
20000 ~ 20999RTCAudio/video (devices, publishing, subscribing)
30000 ~ 30999IMInstant messaging (connection, send, receive)
40000 ~ 40999BoardWhiteboard (initialization, rendering, sync)

Common Error Codes ​

typescript
import { TResultCode } from '@tencent-classroom/sdk';

const result = await classroom.startCamera(element);
if (!result.ok) {
  switch (result.code) {
    case TResultCode.NOT_INITIALIZED:
      console.error('SDK not yet initialized');
      break;
    case TResultCode.DEVICE_NOT_FOUND:
      console.error('Camera device not found');
      break;
    case TResultCode.PERMISSION_DENIED:
      console.error('Camera permission not granted');
      break;
    default:
      console.error('Unknown error:', result.message);
  }
}

Unified Error Channel ​

The SDK provides a unified error event where all module runtime errors are collected:

typescript
import { TEvent } from '@tencent-classroom/sdk';

classroom.on(TEvent.ERROR, ({ module, code, message, detail }) => {
  console.error(`[${module}] ${code}: ${message}`);
  // Report to monitoring system
  reportError({ module, code, message, detail });
});

classroom.on(TEvent.WARN, ({ module, message }) => {
  console.warn(`[${module}] ${message}`);
});