Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from AgoraIO-Community/feat-improveErrorHandling
Browse files Browse the repository at this point in the history
Feat improve error handling
  • Loading branch information
EkaanshArora authored Sep 15, 2021
2 parents 3fe3c80 + f2bcdf7 commit e0f4b5c
Showing 1 changed file with 107 additions and 7 deletions.
114 changes: 107 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,85 @@ import AgoraRTC, {
export default AgoraRTC;
export * from 'agora-rtc-sdk-ng';

export interface AgoraRTCError {
code: AgoraRTCErrorCode;
message: string;
data?: any;
name: string;
}

export declare enum AgoraRTCErrorCode {
UNEXPECTED_ERROR = "UNEXPECTED_ERROR",
UNEXPECTED_RESPONSE = "UNEXPECTED_RESPONSE",
TIMEOUT = "TIMEOUT",
INVALID_PARAMS = "INVALID_PARAMS",
NOT_SUPPORTED = "NOT_SUPPORTED",
INVALID_OPERATION = "INVALID_OPERATION",
OPERATION_ABORTED = "OPERATION_ABORTED",
WEB_SECURITY_RESTRICT = "WEB_SECURITY_RESTRICT",
NETWORK_ERROR = "NETWORK_ERROR",
NETWORK_TIMEOUT = "NETWORK_TIMEOUT",
NETWORK_RESPONSE_ERROR = "NETWORK_RESPONSE_ERROR",
API_INVOKE_TIMEOUT = "API_INVOKE_TIMEOUT",
ENUMERATE_DEVICES_FAILED = "ENUMERATE_DEVICES_FAILED",
DEVICE_NOT_FOUND = "DEVICE_NOT_FOUND",
ELECTRON_IS_NULL = "ELECTRON_IS_NULL",
ELECTRON_DESKTOP_CAPTURER_GET_SOURCES_ERROR = "ELECTRON_DESKTOP_CAPTURER_GET_SOURCES_ERROR",
CHROME_PLUGIN_NO_RESPONSE = "CHROME_PLUGIN_NO_RESPONSE",
CHROME_PLUGIN_NOT_INSTALL = "CHROME_PLUGIN_NOT_INSTALL",
MEDIA_OPTION_INVALID = "MEDIA_OPTION_INVALID",
PERMISSION_DENIED = "PERMISSION_DENIED",
CONSTRAINT_NOT_SATISFIED = "CONSTRAINT_NOT_SATISFIED",
TRACK_IS_DISABLED = "TRACK_IS_DISABLED",
SHARE_AUDIO_NOT_ALLOWED = "SHARE_AUDIO_NOT_ALLOWED",
LOW_STREAM_ENCODING_ERROR = "LOW_STREAM_ENCODING_ERROR",
INVALID_UINT_UID_FROM_STRING_UID = "INVALID_UINT_UID_FROM_STRING_UID",
CAN_NOT_GET_PROXY_SERVER = "CAN_NOT_GET_PROXY_SERVER",
CAN_NOT_GET_GATEWAY_SERVER = "CAN_NOT_GET_GATEWAY_SERVER",
VOID_GATEWAY_ADDRESS = "VOID_GATEWAY_ADDRESS",
UID_CONFLICT = "UID_CONFLICT",
INVALID_LOCAL_TRACK = "INVALID_LOCAL_TRACK",
INVALID_TRACK = "INVALID_TRACK",
SENDER_NOT_FOUND = "SENDER_NOT_FOUND",
CREATE_OFFER_FAILED = "CREATE_OFFER_FAILED",
SET_ANSWER_FAILED = "SET_ANSWER_FAILED",
ICE_FAILED = "ICE_FAILED",
PC_CLOSED = "PC_CLOSED",
SENDER_REPLACE_FAILED = "SENDER_REPLACE_FAILED",
GATEWAY_P2P_LOST = "GATEWAY_P2P_LOST",
NO_ICE_CANDIDATE = "NO_ICE_CANDIDATE",
CAN_NOT_PUBLISH_MULTIPLE_VIDEO_TRACKS = "CAN_NOT_PUBLISH_MULTIPLE_VIDEO_TRACKS",
EXIST_DISABLED_VIDEO_TRACK = "EXIST_DISABLED_VIDEO_TRACK",
INVALID_REMOTE_USER = "INVALID_REMOTE_USER",
REMOTE_USER_IS_NOT_PUBLISHED = "REMOTE_USER_IS_NOT_PUBLISHED",
CUSTOM_REPORT_SEND_FAILED = "CUSTOM_REPORT_SEND_FAILED",
CUSTOM_REPORT_FREQUENCY_TOO_HIGH = "CUSTOM_REPORT_FREQUENCY_TOO_HIGH",
FETCH_AUDIO_FILE_FAILED = "FETCH_AUDIO_FILE_FAILED",
READ_LOCAL_AUDIO_FILE_ERROR = "READ_LOCAL_AUDIO_FILE_ERROR",
DECODE_AUDIO_FILE_FAILED = "DECODE_AUDIO_FILE_FAILED",
WS_ABORT = "WS_ABORT",
WS_DISCONNECT = "WS_DISCONNECT",
WS_ERR = "WS_ERR",
LIVE_STREAMING_TASK_CONFLICT = "LIVE_STREAMING_TASK_CONFLICT",
LIVE_STREAMING_INVALID_ARGUMENT = "LIVE_STREAMING_INVALID_ARGUMENT",
LIVE_STREAMING_INTERNAL_SERVER_ERROR = "LIVE_STREAMING_INTERNAL_SERVER_ERROR",
LIVE_STREAMING_PUBLISH_STREAM_NOT_AUTHORIZED = "LIVE_STREAMING_PUBLISH_STREAM_NOT_AUTHORIZED",
LIVE_STREAMING_TRANSCODING_NOT_SUPPORTED = "LIVE_STREAMING_TRANSCODING_NOT_SUPPORTED",
LIVE_STREAMING_CDN_ERROR = "LIVE_STREAMING_CDN_ERROR",
LIVE_STREAMING_INVALID_RAW_STREAM = "LIVE_STREAMING_INVALID_RAW_STREAM",
LIVE_STREAMING_WARN_STREAM_NUM_REACH_LIMIT = "LIVE_STREAMING_WARN_STREAM_NUM_REACH_LIMIT",
LIVE_STREAMING_WARN_FAILED_LOAD_IMAGE = "LIVE_STREAMING_WARN_FAILED_LOAD_IMAGE",
LIVE_STREAMING_WARN_FREQUENT_REQUEST = "LIVE_STREAMING_WARN_FREQUENT_REQUEST",
WEBGL_INTERNAL_ERROR = "WEBGL_INTERNAL_ERROR",
BEAUTY_PROCESSOR_INTERNAL_ERROR = "BEAUTY_PROCESSOR_INTERNAL_ERROR",
CROSS_CHANNEL_WAIT_STATUS_ERROR = "CROSS_CHANNEL_WAIT_STATUS_ERROR",
CROSS_CHANNEL_FAILED_JOIN_SRC = "CROSS_CHANNEL_FAILED_JOIN_SEC",
CROSS_CHANNEL_FAILED_JOIN_DEST = "CROSS_CHANNEL_FAILED_JOIN_DEST",
CROSS_CHANNEL_FAILED_PACKET_SENT_TO_DEST = "CROSS_CHANNEL_FAILED_PACKET_SENT_TO_DEST",
CROSS_CHANNEL_SERVER_ERROR_RESPONSE = "CROSS_CHANNEL_SERVER_ERROR_RESPONSE",
METADATA_OUT_OF_RANGE = "METADATA_OUT_OF_RANGE"
}

export const createClient = (config: ClientConfig) => {
let client: IAgoraRTCClient
function createClosure() {
Expand All @@ -44,13 +123,16 @@ export function createMicrophoneAndCameraTracks(
}
return function useMicrophoneAndCameraTracks() {
const [ready, setReady] = useState(false)
const [agoraRTCError, setAgoraRTCError] = useState<null | AgoraRTCError>(null)
const ref = useRef(tracks)

useEffect(() => {
if (ref.current === null) {
createClosure().then((tracks) => {
ref.current = tracks
setReady(true)
}, (e) => {
setAgoraRTCError(e)
})
} else {
setReady(true)
Expand All @@ -59,7 +141,7 @@ export function createMicrophoneAndCameraTracks(
tracks = null
}
}, [])
return { ready, tracks: ref.current }
return { ready, tracks: ref.current, error: agoraRTCError }
}
}

Expand All @@ -73,13 +155,16 @@ export function createBufferSourceAudioTrack(
}
return function useBufferSourceAudioTrack() {
const [ready, setReady] = useState(false)
const [agoraRTCError, setAgoraRTCError] = useState<null | AgoraRTCError>(null)
const ref = useRef(track)

useEffect(() => {
if (ref.current === null) {
createClosure().then((track) => {
ref.current = track
setReady(true)
}, (e) => {
setAgoraRTCError(e)
})
} else {
setReady(true)
Expand All @@ -88,7 +173,7 @@ export function createBufferSourceAudioTrack(
track = null
}
}, [])
return { ready, track: ref.current }
return { ready, track: ref.current, error: agoraRTCError }
}
}

Expand All @@ -100,13 +185,16 @@ export function createCameraVideoTrack(config?: CameraVideoTrackInitConfig) {
}
return function useCameraVideoTrack() {
const [ready, setReady] = useState(false)
const [agoraRTCError, setAgoraRTCError] = useState<null | AgoraRTCError>(null)
const ref = useRef(track)

useEffect(() => {
if (ref.current === null) {
createClosure().then((track) => {
ref.current = track
setReady(true)
}, (e) => {
setAgoraRTCError(e)
})
} else {
setReady(true)
Expand All @@ -115,7 +203,7 @@ export function createCameraVideoTrack(config?: CameraVideoTrackInitConfig) {
track = null
}
}, [])
return { ready, track: ref.current }
return { ready, track: ref.current, error: agoraRTCError }
}
}

Expand All @@ -127,13 +215,16 @@ export function createCustomAudioTrack(config: CustomAudioTrackInitConfig) {
}
return function useCustomAudioTrack() {
const [ready, setReady] = useState(false)
const [agoraRTCError, setAgoraRTCError] = useState<null | AgoraRTCError>(null)
const ref = useRef(track)

useEffect(() => {
if (ref.current === null) {
createClosure().then((track) => {
ref.current = track
setReady(true)
}, (e) => {
setAgoraRTCError(e)
})
} else {
setReady(true)
Expand All @@ -142,7 +233,7 @@ export function createCustomAudioTrack(config: CustomAudioTrackInitConfig) {
track = null
}
}, [])
return { ready, track: ref.current }
return { ready, track: ref.current, error: agoraRTCError }
}
}

Expand All @@ -154,13 +245,16 @@ export function createCustomVideoTrack(config: CustomVideoTrackInitConfig) {
}
return function useCustomVideoTrack() {
const [ready, setReady] = useState(false)
const [agoraRTCError, setAgoraRTCError] = useState<null | AgoraRTCError>(null)
const ref = useRef(track)

useEffect(() => {
if (ref.current === null) {
createClosure().then((track) => {
ref.current = track
setReady(true)
}, (e) => {
setAgoraRTCError(e)
})
} else {
setReady(true)
Expand All @@ -169,7 +263,7 @@ export function createCustomVideoTrack(config: CustomVideoTrackInitConfig) {
track = null
}
}, [])
return { ready, track: ref.current }
return { ready, track: ref.current, error: agoraRTCError }
}
}

Expand All @@ -183,13 +277,16 @@ export function createMicrophoneAudioTrack(
}
return function useMicrophoneAudioTrack() {
const [ready, setReady] = useState(false)
const [agoraRTCError, setAgoraRTCError] = useState<null | AgoraRTCError>(null)
const ref = useRef(track)

useEffect(() => {
if (ref.current === null) {
createClosure().then((track) => {
ref.current = track
setReady(true)
}, (e) => {
setAgoraRTCError(e)
})
} else {
setReady(true)
Expand All @@ -198,7 +295,7 @@ export function createMicrophoneAudioTrack(
track = null
}
}, [])
return { ready, track: ref.current }
return { ready, track: ref.current, error: agoraRTCError }
}
}

Expand All @@ -213,19 +310,22 @@ export function createScreenVideoTrack(
}
return function useScreenVideoTrack() {
const [ready, setReady] = useState(false)
const [agoraRTCError, setAgoraRTCError] = useState<null | AgoraRTCError>(null)
const ref = useRef(tracks)

useEffect(() => {
if (ref.current === undefined) {
createClosure().then((tracks) => {
ref.current = tracks
setReady(true)
}, (e) => {
setAgoraRTCError(e)
})
} else {
setReady(true)
}
}, [])
return { ready, tracks: ref.current }
return { ready, tracks: ref.current, error: agoraRTCError }
}
}

Expand Down

0 comments on commit e0f4b5c

Please sign in to comment.