Skip to content

Commit

Permalink
fix: rename WcmeError to WebrtcCoreError (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: Bryce Tham <[email protected]>
  • Loading branch information
brycetham and Bryce Tham authored Dec 11, 2023
1 parent 59909b4 commit 6a4078b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
51 changes: 15 additions & 36 deletions src/device/device-management.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
import { WebrtcCoreError, WebrtcCoreErrorTypes } from '../errors';
import * as media from '../media';
import { LocalCameraStream } from '../media/local-camera-stream';
import { LocalDisplayStream } from '../media/local-display-stream';
import { LocalMicrophoneStream } from '../media/local-microphone-stream';
import { LocalSystemAudioStream } from '../media/local-system-audio-stream';
import { VideoContentHint } from '../media/local-video-stream';

export enum ErrorTypes {
DEVICE_PERMISSION_DENIED = 'DEVICE_PERMISSION_DENIED',
CREATE_STREAM_FAILED = 'CREATE_CAMERA_STREAM',
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Constructor<T> = new (...args: any[]) => T;

/**
* Represents a WCME error, which contains error type and error message.
*/
export class WcmeError {
type: string;

message: string;

/**
* Creates new error.
*
* @param type - Error type.
* @param message - Error message.
*/
constructor(type: ErrorTypes, message = '') {
this.type = type;
this.message = message;
}
}

export type AudioDeviceConstraints = Pick<
MediaTrackConstraints,
| 'autoGainControl'
Expand Down Expand Up @@ -68,8 +44,8 @@ export async function createCameraStream<T extends LocalCameraStream>(
try {
stream = await media.getUserMedia({ video: { ...constraints } });
} catch (error) {
throw new WcmeError(
ErrorTypes.CREATE_STREAM_FAILED,
throw new WebrtcCoreError(
WebrtcCoreErrorTypes.CREATE_STREAM_FAILED,
`Failed to create camera stream: ${error}`
);
}
Expand All @@ -91,8 +67,8 @@ export async function createMicrophoneStream<T extends LocalMicrophoneStream>(
try {
stream = await media.getUserMedia({ audio: { ...constraints } });
} catch (error) {
throw new WcmeError(
ErrorTypes.CREATE_STREAM_FAILED,
throw new WebrtcCoreError(
WebrtcCoreErrorTypes.CREATE_STREAM_FAILED,
`Failed to create microphone stream: ${error}`
);
}
Expand Down Expand Up @@ -124,8 +100,8 @@ export async function createCameraAndMicrophoneStreams<
audio: { ...constraints?.audio },
});
} catch (error) {
throw new WcmeError(
ErrorTypes.CREATE_STREAM_FAILED,
throw new WebrtcCoreError(
WebrtcCoreErrorTypes.CREATE_STREAM_FAILED,
`Failed to create camera and microphone streams: ${error}`
);
}
Expand Down Expand Up @@ -153,8 +129,8 @@ export async function createDisplayStream<T extends LocalDisplayStream>(
try {
stream = await media.getDisplayMedia({ video: true });
} catch (error) {
throw new WcmeError(
ErrorTypes.CREATE_STREAM_FAILED,
throw new WebrtcCoreError(
WebrtcCoreErrorTypes.CREATE_STREAM_FAILED,
`Failed to create display stream: ${error}`
);
}
Expand Down Expand Up @@ -187,8 +163,8 @@ export async function createDisplayStreamWithAudio<
try {
stream = await media.getDisplayMedia({ video: true, audio: true });
} catch (error) {
throw new WcmeError(
ErrorTypes.CREATE_STREAM_FAILED,
throw new WebrtcCoreError(
WebrtcCoreErrorTypes.CREATE_STREAM_FAILED,
`Failed to create display and system audio streams: ${error}`
);
}
Expand Down Expand Up @@ -221,7 +197,10 @@ export async function getDevices(deviceKind?: media.DeviceKind): Promise<MediaDe
media.enumerateDevices
);
} catch (error) {
throw new WcmeError(ErrorTypes.DEVICE_PERMISSION_DENIED, 'Failed to ensure device permissions');
throw new WebrtcCoreError(
WebrtcCoreErrorTypes.DEVICE_PERMISSION_DENIED,
'Failed to ensure device permissions'
);
}

return devices.filter((v: MediaDeviceInfo) => (deviceKind ? v.kind === deviceKind : true));
Expand Down
24 changes: 24 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export enum WebrtcCoreErrorTypes {
DEVICE_PERMISSION_DENIED = 'DEVICE_PERMISSION_DENIED',
CREATE_STREAM_FAILED = 'CREATE_STREAM_FAILED',
}

/**
* Represents a WebRTC core error, which contains error type and error message.
*/
export class WebrtcCoreError {
type: string;

message: string;

/**
* Creates new error.
*
* @param type - Error type.
* @param message - Error message.
*/
constructor(type: WebrtcCoreErrorTypes, message = '') {
this.type = type;
this.message = message;
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as media from './media';

export * from './device/device-management';
export * from './errors';
export * from './media/local-audio-stream';
export * from './media/local-camera-stream';
export * from './media/local-display-stream';
Expand Down

0 comments on commit 6a4078b

Please sign in to comment.