Error handling ************** Video playback relies on a number of coordinated subsystems — manifest retrieval, DRM license acquisition, audio and video decoding, network I/O, and rendering. Failures in any of these subsystems may affect the user experience and must be surfaced to the application in a consistent and actionable form. This guide describes how errors are modeled in PRESTOplay for React Native, how they behave across the supported platforms, and how to handle them in an application. How errors are modeled ====================== Every error reported by the player is represented by a "PrestoPlayError" (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/PrestoPlayError/) instance. This class provides a normalized, cross-platform representation of the underlying native error (Android, iOS/tvOS, or Web). Its most relevant fields are: +----------------------------------------------------+--------------------------------------------------------------------------------------------------+ | Field | Description | |====================================================|==================================================================================================| | "code" | A stable numeric "ErrorCode" | | | (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/ErrorCode/) | | | identifying the kind of error (e.g. | | | "ManifestLoadingFailed", "MediaDownloadingFailed", | | | "DrmError"). | +----------------------------------------------------+--------------------------------------------------------------------------------------------------+ | "severity" | An "ErrorSeverity" | | | (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/ErrorSeverity/) | | | value, either "Recoverable" or "Fatal". | +----------------------------------------------------+--------------------------------------------------------------------------------------------------+ | "category" | An "ErrorCategory" | | | (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/ErrorCategory/) | | | value derived from the code ("Network", "Media", | | | "Manifest", "Drm", "Player", etc.), intended for | | | routing errors to the appropriate handler. | +----------------------------------------------------+--------------------------------------------------------------------------------------------------+ | "message" / "causeMessage" | Human-readable descriptions provided by the native | | | SDK. | +----------------------------------------------------+--------------------------------------------------------------------------------------------------+ | "network" | A "NetworkErrorMetadata" | | | (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/interfaces/NetworkErrorMetadata/) | | | object containing "httpStatusCode", "url", | | | "httpResponseBody", and a "cause" | | | ("NetworkErrorCause" | | | (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/NetworkErrorCause/)). | +----------------------------------------------------+--------------------------------------------------------------------------------------------------+ | "nativeError" | The raw, platform-specific error object, available | | | when additional inspection beyond the normalized | | | fields is required. | +----------------------------------------------------+--------------------------------------------------------------------------------------------------+ Severity: recoverable vs. fatal =============================== The SDK classifies every error with one of two severities: * "Recoverable" — informative. Playback has not been interrupted. The player typically continues from its buffer while retrying the failing operation internally. Recoverable errors provide the application with an opportunity to act before the situation escalates — for example, to refresh an authentication token before the buffer is exhausted. * "Fatal" — terminal. Playback cannot start or cannot continue. The player stops, and the application should present an appropriate message, fall back to an alternative stream, or retry with corrected configuration. A single underlying problem may manifest first as one or more recoverable warnings and, if left unaddressed, escalate into a fatal error. This pattern is illustrated in the CDN token recovery example below. Listening for errors ==================== Two complementary mechanisms are available for observing errors. 1. The "usePlayerFatalError" hook is appropriate for rendering a fallback user interface (banner, modal, error screen) when playback has definitively failed. It filters on fatal severity and clears automatically when "Player.open" or "Player.stop" is invoked. import { usePlayerFatalError } from "@castlabs/react-native-prestoplay"; function FatalErrorBanner() { const fatalError = usePlayerFatalError(); if (!fatalError) return null; return ( Playback failed (code {fatalError.code}) {fatalError.message} ); } 2. The "Error" event is appropriate when the application needs visibility into every error — including recoverable ones — in order to apply custom logic (retry, refresh a token, report to analytics). The same event is also the building block for implementing a custom hook tailored to the application's needs — for example, one that exposes only errors of a given category, keeps a rolling history for diagnostics, or couples error state with retry logic — when "usePlayerFatalError" does not fit the use case. import { EventType, ErrorDetails, ErrorSeverity, usePlayer, } from "@castlabs/react-native-prestoplay"; function useErrorLogger() { const player = usePlayer(); useEffect(() => { const onError = (event) => { const { error } = event.details as ErrorDetails; console.log( `[Player] ${ErrorSeverity[error.severity]} error ${error.code}:`, error.message, error.network, ); }; player.addEventListener(EventType.Error, onError); return () => player.removeEventListener(EventType.Error, onError); }, [player]); } Building a custom hook ---------------------- The "useErrorLogger" example above is a minimal illustration of a more general pattern. When the behaviour offered by "usePlayerFatalError" does not match a particular requirement, applications are encouraged to encapsulate their own error-observation logic in a dedicated hook. Typical examples include: * A hook that exposes only errors of a specific "ErrorCategory" (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/ErrorCategory/) (e.g. "useNetworkError", "useDrmError"). * A hook that maintains a rolling history of the last N errors, intended for diagnostics screens or telemetry reports. * A hook that combines error observation with retry policy (backoff, maximum attempts, fallback to an alternative stream). * A hook that filters on a specific condition, such as the recoverable HTTP "403" handler shown in the CDN token recovery example below. The recommended structure is the one used throughout this guide: subscribe to "EventType.Error" inside a "useEffect", update component state (or perform side effects) based on the error's "severity", "code", "category", or "network" fields, and unsubscribe in the cleanup function to respect the React component lifecycle. Network errors ============== Network errors are the most common category of errors in streaming applications. They cover every failure involving an HTTP request performed by the player — manifest retrieval, media segment download, DRM license acquisition, and connectivity changes. All errors in this category share the code range "1000–1999" and resolve to "ErrorCategory.Network". The "network" metadata ---------------------- For errors in this category, the "network" field of "PrestoPlayError" (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/PrestoPlayError/) is populated with additional diagnostic information, captured in the "NetworkErrorMetadata" (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/interfaces/NetworkErrorMetadata/) interface: +----------------------------------------------------+------------------------------------------------------------------------------------------------+ | Field | Description | |====================================================|================================================================================================| | "url" | The URL of the failed request, when available. | +----------------------------------------------------+------------------------------------------------------------------------------------------------+ | "httpStatusCode" | The HTTP status code returned by the server, when | | | the server could be reached. "-1" indicates no | | | response received. | +----------------------------------------------------+------------------------------------------------------------------------------------------------+ | "httpResponseBody" | The body of the response, when available. Useful | | | for surfacing backend error messages. | +----------------------------------------------------+------------------------------------------------------------------------------------------------+ | "cause" | A "NetworkErrorCause" | | | (https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/NetworkErrorCause/) | | | value describing the nature of the failure. | +----------------------------------------------------+------------------------------------------------------------------------------------------------+ The "cause" field takes one of the following values: +----------------------------------------------------+----------------------------------------------------+ | Value | Meaning | |====================================================|====================================================| | "HttpError" | The server was reached but returned a non-success | | | HTTP status code (e.g. "400", "401", "403", "404", | | | "5xx"). | +----------------------------------------------------+----------------------------------------------------+ | "ServerUnreachable" | The request could not reach the server (e.g. DNS | | | failure, connection refused, TLS failure, request | | | cancelled). | +----------------------------------------------------+----------------------------------------------------+ | "NoNetworkConnection" | The device has no active network connection at the | | | time the request was issued. | +----------------------------------------------------+----------------------------------------------------+ Fatal vs. recoverable network errors ------------------------------------ Network errors follow the general severity semantics described earlier in the guide. In practice: * Errors raised during opening (before the player reaches "Ready") are almost always fatal, since playback cannot start without a manifest and decoders. Fatal errors during opening are normalized across platforms to "ManifestLoadingFailed" (1002), regardless of whether the underlying failure was a manifest request, an initialization segment, or a license fetch. * Errors raised during playback are typically recoverable on the first occurrences, because the player continues from its buffer while retrying. They escalate to fatal once the retry policy is exhausted or the buffer drains. This is the pattern that makes mid-playback recovery (e.g. token refresh) possible. * "ConnectivityLost" (1003) and "ConnectivityGained" (1004) are always informational and never interrupt playback on their own. They can be used to drive network-status indicators in the user interface. Reading the HTTP status ----------------------- Filtering on the HTTP status code is a common requirement. Typical dispatch logic looks as follows: import { EventType, ErrorDetails, ErrorCategory, NetworkErrorCause, usePlayer, } from "@castlabs/react-native-prestoplay"; function useNetworkErrorHandler() { const player = usePlayer(); useEffect(() => { const onError = (event: { details: ErrorDetails }) => { const { error } = event.details; if (error.category !== ErrorCategory.Network) return; const { httpStatusCode, cause, url } = error.network; if (cause === NetworkErrorCause.NoNetworkConnection) { // Device is offline. Show a connectivity banner. return; } if (cause === NetworkErrorCause.ServerUnreachable) { // Request never reached the server (DNS, TLS, timeout, cancellation). return; } switch (httpStatusCode) { case 401: case 403: // Authentication or authorization problem. // See the CDN token recovery example below. break; case 404: // Resource not found. Typically a configuration or packaging problem. break; case 429: // Rate limited. Back off before retrying. break; default: if (httpStatusCode && httpStatusCode >= 500) { // Server error. Retry with backoff or fall back to another origin. } } }; player.addEventListener(EventType.Error, onError); return () => player.removeEventListener(EventType.Error, onError); }, [player]); } Platform notes -------------- * Safari does not surface HTTP "403" responses to the player. Applications that depend on observing a 403 mid-playback should treat Safari as a best-effort target. * "DnsLookupFailed" (1006) and "TimeoutOccurred" (1007) are currently emitted only on Android. Example: handling errors on playback opening ============================================ Between the invocation of "Player.open" and the player reaching the "Ready" state, the SDK retrieves the manifest, negotiates DRM, and prepares the decoders. A failure during this phase — an unreachable URL, an invalid or expired license, a "500 Internal Server Error" — is reported as a fatal error, since playback has not been established. The recommended approach for handling these failures is to drive the user interface from the "usePlayerFatalError" hook. The component below opens a stream and presents a retry control when opening fails. import React, { useCallback } from "react"; import { View, Text, Button, Alert } from "react-native"; import { ContentType, ErrorCategory, PlayerProvider, PlayerView, usePlayer, usePlayerFatalError, } from "@castlabs/react-native-prestoplay"; function PlaybackSurface({ streamUrl }: { streamUrl: string }) { const player = usePlayer(); const fatalError = usePlayerFatalError(); const retry = useCallback(() => { player.open({ autoPlay: true, source: { url: streamUrl, type: ContentType.Hls }, }); }, [player, streamUrl]); if (fatalError) { const title = fatalError.category === ErrorCategory.Network ? "Network problem" : fatalError.category === ErrorCategory.Drm ? "Playback not authorized" : "Playback failed"; return ( {title} {fatalError.message} (code {fatalError.code})