<media-error-dialog>
The <media-error-dialog> component displays a message when the media element fails with an error. It is hidden by default and opens automatically when the media element fires an error event. It closes again when the media is reset, for example, by assigning a new src or calling load() (which fires emptied event) or when playback resumes (playing event).
Default usage
Section titled Default usage<media-controller> doesn’t add this component for you. Place it in the dialog slot so it renders on top of the media.
The example below uses a source that doesn’t exist, which triggers a MEDIA_ERR_SRC_NOT_SUPPORTED error.
Default error messages
Section titled Default error messagesThe dialog shows a title and a message based on the MediaError code:
| Code | Constant | Title |
|---|---|---|
| 1 | MEDIA_ERR_ABORTED | Not shown |
| 2 | MEDIA_ERR_NETWORK | Network Error |
| 3 | MEDIA_ERR_DECODE | Decode Error |
| 4 | MEDIA_ERR_SRC_NOT_SUPPORTED | Source Not Supported |
| 5 | MEDIA_ERR_ENCRYPTED | Encryption Error |
MEDIA_ERR_ABORTED means the user or the browser stopped fetching the media on purpose, so the dialog stays closed. For any other code, the title is Error <code> and the message is the error’s own message.
The default titles and messages can be translated. See Adding language support.
Customize error content
Section titled Customize error contentYou can replace the content for a specific error code using slots:
error-<code>replaces the whole content (title and message).error-<code>-titlereplaces only the title.error-<code>-messagereplaces only the message.
Replace the whole content
Section titled Replace the whole contentEverything assigned to the error-<code> slot replaces the default title and message, so you can use any markup, such as icons or links.
Replace only the title or message
Section titled Replace only the title or messageUse error-<code>-title or error-<code>-message to keep one of the defaults and replace the other.
Formatting messages with JavaScript
Section titled Formatting messages with JavaScriptTo build the content from the error object itself, override the static formatErrorMessage() method in a subclass. It receives the mediaError object and returns an HTML string. Media elements that add extra fields to their error object can use this to show more detail.
const MediaErrorDialog = customElements.get('media-error-dialog');
class MyErrorDialog extends MediaErrorDialog {
static formatErrorMessage(error) {
return `<h3>Something went wrong (${error.code})</h3><p>${error.message}</p>`;
}
}
customElements.define('my-error-dialog', MyErrorDialog);
<media-controller>
<video slot="media" src="..."></video>
<my-error-dialog slot="dialog"></my-error-dialog>
</media-controller>
Styling with attributes
Section titled Styling with attributesWhen an error occurs, <media-controller> sets the mediaerrorcode attribute on itself, and <media-error-dialog> gets both mediaerrorcode and mediaerrormessage. They are removed when the error clears. You can use mediaerrorcode to style other parts of the player, for example to hide the control bar while an error is displayed:
media-controller[mediaerrorcode] media-control-bar {
display: none;
}
The open attribute is set on <media-error-dialog> while it is visible.
Reference
Section titled ReferenceSlots
Section titled Slots| Name | Description |
|---|---|
| Not used; assign content to an `error-{code}` slot. |
error-{code} | Replaces the whole content for the given error code, e.g. `error-4`. |
error-{code}-title | Replaces the title for the given error code, e.g. `error-4-title`. |
error-{code}-message | Replaces the message for the given error code, e.g. `error-4-message`. |
Attributes
Section titled Attributes| Name | Type | Description |
|---|---|---|
open | boolean | The open state of the dialog. |
Media UI Attributes
Section titled Media UI Attributes
The media UI attributes will be set automatically by the controller if they are
connected via nesting or the mediacontroller attribute.
Only set these attributes manually if you know what you're doing.
| Name | Type | Description |
|---|---|---|
mediaerrorcode | number | The error code for the current media error. |
mediaerrormessage | string | The error message for the current media error. |
CSS Variables
Section titled CSS Variables| Name | Default | Description |
|---|---|---|
--media-control-background | background of control. | |
--media-primary-color | rgb(238 238 238) | Default color of text / icon. |
--media-secondary-color | Default color of background. | |
--media-text-color | var(--media-primary-color, rgb(238 238 238)) | color of text. |
--media-dialog-display | inline-flex | display of dialog. |
--media-font | var(--media-font-weight, normal) var(--media-font-size, 14px) / var(--media-text-content-height, var(--media-control-height, 24px)) var(--media-font-family, helvetica neue, segoe ui, roboto, arial, sans-serif) | font shorthand property. |
--media-font-weight | normal | font-weight property. |
--media-font-family | helvetica neue, segoe ui, roboto, arial, sans-serif | font-family property. |
--media-font-size | 14px | font-size property. |
--media-text-content-height | var(--media-control-height, 24px) | line-height of text. |