Skip to content

Commit 14121dd

Browse files
bsmthhamishwillee
andauthored
chore: move EmbedGH live sample into the page (mdn#40409)
* chore: move EmbedGH live sample into the page * Fix up arbitrary line breaks --------- Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
1 parent b579560 commit 14121dd

File tree

1 file changed

+45
-45
lines changed
  • files/en-us/web/api/mediaerror/message

1 file changed

+45
-45
lines changed

files/en-us/web/api/mediaerror/message/index.md

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,83 +8,83 @@ browser-compat: api.MediaError.message
88

99
{{APIRef("HTML DOM")}}
1010

11-
The read-only property **`MediaError.message`** returns a
12-
human-readable string offering specific
13-
diagnostic details related to the error described by the `MediaError` object,
14-
or an empty string (`""`) if no diagnostic information can be determined or
15-
provided.
11+
The read-only property **`MediaError.message`** returns a human-readable string offering specific diagnostic details related to the error described by the `MediaError` object, or an empty string (`""`) if no diagnostic information can be determined or provided.
1612

1713
## Value
1814

19-
A string providing a detailed, specific explanation of what went
20-
wrong and possibly how it might be fixed. This is _not_ a generic description of
21-
the {{domxref("MediaError.code")}} property's value, but instead goes deeper into the
22-
specifics of this particular error and its circumstances. If no specific details are
23-
available, this string is empty.
15+
A string providing a detailed, specific explanation of what went wrong and possibly how it might be fixed. This is _not_ a generic description of the {{domxref("MediaError.code")}} property's value, but instead goes deeper into the specifics of this particular error and its circumstances.
16+
If no specific details are available, this string is empty.
2417

2518
## Examples
2619

27-
This example creates an {{HTMLElement("audio")}} element, establishes an error handler
28-
for it, then lets the user click buttons to choose whether to assign a valid audio file
29-
or a missing file to the element's [`src`](/en-US/docs/Web/HTML/Reference/Elements/audio#src) attribute. The error
30-
handler outputs a message to a box onscreen describing the error, including both the
31-
`code` and the `message`.
20+
This example creates an {{HTMLElement("audio")}} element, establishes an error handler for it, then lets the user click buttons to choose whether to assign a valid audio file or a missing file to the element's [`src`](/en-US/docs/Web/HTML/Reference/Elements/audio#src) attribute.
21+
The error handler outputs a message to a box onscreen describing the error, including both the `code` and the `message`.
3222

33-
Only the relevant parts of the code are displayed; you can [see the complete source code here](https://github.com/mdn/dom-examples/tree/main/media/mediaerror).
23+
```html
24+
<audio controls id="audio"></audio>
25+
<div>
26+
<button id="valid-button">Valid File</button>
27+
<button id="invalid-button">Missing File</button>
28+
</div>
29+
<pre id="log"></pre>
30+
```
3431

35-
The example creates an {{HTMLElement("audio")}} element and lets the user assign either
36-
a valid music file to it, or a link to a file which doesn't exist. This lets us see the
37-
behavior of the {{domxref("HTMLMediaElement/error_event", "error")}} event handler, which is received by an event handler
38-
we add to the `<audio>` element itself.
32+
The example creates an {{HTMLElement("audio")}} element and lets the user assign either a valid music file to it, or a link to a file which doesn't exist.
33+
This lets us see the behavior of the {{domxref("HTMLMediaElement/error_event", "error")}} event handler, which is received by an event handler we add to the `<audio>` element itself.
3934

4035
The error handler looks like this:
4136

4237
```js
38+
const audioElement = document.getElementById("audio");
39+
const validButton = document.getElementById("valid-button");
40+
const invalidButton = document.getElementById("invalid-button");
41+
42+
const logMessage = (msg) => {
43+
const now = new Date();
44+
const timestamp = now.toLocaleTimeString();
45+
document.getElementById("log").innerText += `[${timestamp}] ${msg}\n`;
46+
};
47+
48+
validButton.addEventListener("click", () => {
49+
audioElement.src = "https://mdn.github.io/shared-assets/audio/guitar.mp3";
50+
});
51+
52+
invalidButton.addEventListener("click", () => {
53+
audioElement.src = "no-file-here.mp3";
54+
});
55+
4356
audioElement.onerror = () => {
44-
let s = "";
45-
const err = audioElement.error;
57+
let message = "";
58+
let err = audioElement.error;
4659

4760
switch (err.code) {
4861
case MediaError.MEDIA_ERR_ABORTED:
49-
s += "The user canceled the audio.";
62+
message += "The user canceled the audio.";
5063
break;
5164
case MediaError.MEDIA_ERR_NETWORK:
52-
s += "A network error occurred while fetching the audio.";
65+
message += "A network error occurred while fetching the audio.";
5366
break;
5467
case MediaError.MEDIA_ERR_DECODE:
55-
s += "An error occurred while decoding the audio.";
68+
message += "An error occurred while decoding the audio.";
5669
break;
5770
case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
58-
s +=
71+
message +=
5972
"The audio is missing or is in a format not supported by your browser.";
6073
break;
6174
default:
62-
s += "An unknown error occurred.";
75+
message += "An unknown error occurred.";
6376
break;
6477
}
6578

66-
const message = err.message;
67-
68-
if (message?.length > 0) {
69-
s += ` ${message}`;
70-
}
71-
72-
displayErrorMessage(`<strong>Error ${err.code}:</strong> ${s}<br>`);
79+
logMessage(`Error ${err.code}: ${message}`);
7380
};
7481
```
7582

76-
This gets the {{domxref("MediaError")}} object describing the error from the
77-
{{domxref("HTMLMediaElement.error", "error")}} property on the
78-
{{domxref("HTMLAudioElement")}} representing the audio player. The error's
79-
{{domxref("MediaError.code", "code")}} attribute is checked to determine a generic error
80-
message to display, and, if `message` is not empty, it's appended to provide
81-
additional details. Then the resulting text is output to the log.
82-
83-
### Result
84-
85-
You can try out this example below, and can [see the example in action outside this page here](https://mdn.github.io/dom-examples/media/mediaerror/).
83+
This gets the {{domxref("MediaError")}} object describing the error from the {{domxref("HTMLMediaElement.error", "error")}} property on the {{domxref("HTMLAudioElement")}} representing the audio player.
84+
The error's {{domxref("MediaError.code", "code")}} attribute is checked to determine a generic error message to display, and, if `message` is not empty, it's appended to provide additional details.
85+
Then the resulting text is output to the log.
8686

87-
{{ EmbedGHLiveSample('dom-examples/media/mediaerror', 650, 200) }}
87+
{{embedlivesample("", , '300')}}
8888

8989
## Specifications
9090

0 commit comments

Comments
 (0)