Skip to content

Commit

Permalink
Add Error Callback (#302)
Browse files Browse the repository at this point in the history
* fix: added ID element

* chore: added changeset

* fix: added onError callback
  • Loading branch information
0xcadams committed Mar 29, 2023
1 parent 0054108 commit 4ebec15
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .changeset/sour-pugs-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@livepeer/core': patch
'@livepeer/core-react': patch
'@livepeer/react': patch
'@livepeer/react-native': patch
'livepeer': patch
---

**Fix:** added an `onError` callback to the Player to allow users to catch and handle miscellaneous errors which occur in the Player, which are not already handled.
5 changes: 5 additions & 0 deletions packages/core-react/src/components/player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export type PlayerProps<TElement, TPoster> = {
/** Callback called when the media sources are changed */
onSourceUpdated?: (sources: Src[]) => void;

/** Callback called when an error occurs that is not access control or metrics */
onError?: (error: Error) => void;

/** Callback ref passed to the underlying media element. Simple refs are not supported, due to the use of HLS.js under the hood. */
mediaElementRef?: React.RefCallback<TElement | null | undefined>;
} & (
Expand Down Expand Up @@ -131,6 +134,7 @@ export const usePlayer = <TElement, TPoster>(
onStreamStatusChange,
onMetricsError,
onAccessControlError,
onError,
onSourceUpdated,
jwt,

Expand Down Expand Up @@ -256,6 +260,7 @@ export const usePlayer = <TElement, TPoster>(
onStreamStatusChange: onStreamStatusChangeCallback,
onMetricsError,
onAccessControlError: accessControlErrorCallback,
onError,
isCurrentlyShown: _isCurrentlyShown,
},
controlsContainerProps: {
Expand Down
1 change: 1 addition & 0 deletions packages/core-react/src/components/player/players.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type VideoPlayerProps<TElement, TPoster> = {
onStreamStatusChange?: (isLive: boolean) => void;
onMetricsError?: (error: Error) => void;
onAccessControlError?: (error: Error) => void;
onError?: (error: Error) => void;
};

export type AudioPlayerProps<TElement, TPoster> = Omit<
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/providers/version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const core = `@livepeer/core@1.3.1`;
export const react = `@livepeer/react@2.3.1`;
export const reactNative = `@livepeer/react-native@1.3.1`;
export const core = `@livepeer/core@1.3.2`;
export const react = `@livepeer/react@2.3.2`;
export const reactNative = `@livepeer/react-native@1.3.2`;
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const VideoPlayer = React.forwardRef<MediaElement, VideoPlayerProps>(
poster,
audioMode,
onMetricsError,
onError,
isCurrentlyShown,
},
ref,
Expand Down Expand Up @@ -80,12 +81,6 @@ export const VideoPlayer = React.forwardRef<MediaElement, VideoPlayerProps>(
[autoPlay, isCurrentlyShown],
);

const onError = async (_e: string) => {
// await new Promise((r) => setTimeout(r, 1000 * ++retryCount));
// await state._element?.unloadAsync();
// TODO add error handling
};

React.useEffect(() => {
const removeEffectsFromStore = addEffectsToStore(
context,
Expand Down Expand Up @@ -183,7 +178,7 @@ export const VideoPlayer = React.forwardRef<MediaElement, VideoPlayerProps>(
progressUpdateIntervalMillis={defaultProgressUpdateInterval}
onFullscreenUpdate={onFullscreenUpdate}
onPlaybackStatusUpdate={onPlaybackStatusUpdate}
onError={onError}
onError={(err) => onError?.(new Error(err))}
shouldPlay={shouldPlay}
ref={ref}
isMuted={muted}
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/components/media/players/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const VideoPlayer = React.forwardRef<HTMLVideoElement, VideoPlayerProps>(
onStreamStatusChange,
onMetricsError,
onAccessControlError,
onError: onMiscError,
priority,
allowCrossOriginCredentials,
} = props;
Expand Down Expand Up @@ -107,6 +108,8 @@ export const VideoPlayer = React.forwardRef<HTMLVideoElement, VideoPlayerProps>(
onStreamStatusChange?.(false);
} else if (isAccessControlError(cleanError)) {
onAccessControlError?.(cleanError);
} else {
onMiscError?.(cleanError);
}
console.warn(cleanError.message);
};
Expand Down

2 comments on commit 4ebec15

@vercel
Copy link

@vercel vercel bot commented on 4ebec15 Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 4ebec15 Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.