Handling Errors
This tutorial will show you how you can handle AMP errors.
You can handle AMP errors in two main ways:
-
Using the akamai.amp.TransformType.ERROR transform (Recommended)
-
Using the akamai.amp.Events.ERROR event
To handle AMP errors using transforms, you need to add a transform to the amp with the type TransformType.ERROR
:
akamai.amp.AMP.create("amp", config)
.then(function (player) {
amp = player;
amp.addTransform(akamai.amp.TransformType.ERROR, function (error) {
console.log("An error just happened")
return error;
});
});
By using the addTransform method, we have access to the argument error. The argument error can be used to identify the source of the error and react accordingly. The following example handles an error occurred when the player doesn’t support the provided media file.
akamai.amp.AMP.create("amp", config)
.then(function (player) {
amp = player;
amp.addTransform(akamai.amp.TransformType.ERROR, function (error) {
if (error.code === MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED) {
var media = Object.assign({}, amp.media);
media.src = "https://mdtp-a.akamaihd.net/customers/akamai/video/VfE.mp4"; // A fallback video for this example.
amp.media = media;
return; // By returning null we let the player know the error was handled.
}
return error;
});
});
The possible values for error.code
can be known by printing the object error
. In the following example, error. code
is four which means that the error is a MEDIA_ERR_SRC_NOT_SUPPORTED.
[object Error] {
code: 4,
message: "Error",
metadata: [object MediaError] {
code: 4,
MEDIA_ERR_ABORTED: 1,
MEDIA_ERR_DECODE: 3,
MEDIA_ERR_ENCRYPTED: 5,
MEDIA_ERR_NETWORK: 2,
MEDIA_ERR_SRC_NOT_SUPPORTED: 4
}
}
A second way that AMP errors can be handled is by listening for the player event ERROR. Notice that we’re accessing the player
attribute error
. The attribute error
holds the last error that occurred.
akamai.amp.AMP.create("amp", config, (event) => {
amp = event.player;
amp.addEventListener(akamai.amp.Events.ERROR, (event) => {
console.log("An error just happened.");
console.log( amp.error );
});
});
Even though it is possible to handle errors by adding a listener for the Events.ERROR event, consider the execution order of both the transform and the listener for Events.ERROR. The transform will be executed first, and if the transform does not handle the error, then the listener for Events.ERROR will be called. In the following example, this interaction is shown. In the example, the message "Oh no we have no video." will never be printed instead, the letter "Don’t worry. I can fix it!" will be printed since we have adequately managed the error using a transform.
akamai.amp.AMP.create("amp", config, (event) => {
amp = event.player;
amp.addEventListener(akamai.amp.Events.ERROR, (event) => {
console.log('Oh no we have no video.');
});
amp.addTransform(akamai.amp.TransformType.ERROR, function (error) {
if (error.code === MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED) {
console.log('Don\'t worry. I can fix it!')
var media = Object.assign({}, amp.media);
media.src = "https://mdtp-a.akamaihd.net/customers/akamai/video/VfE.mp4"; // A fallback video for this example.
amp.media = media;
return; // By returning null we let the player know the error was handled.
}
return error;
});
});