Custom error messages

In the event of an error, the player will emit a corresponding error event. This error event will include an error message intended to technically outline the nature of the issue. However, these error messages may not always be user-friendly. It is possible to customize those error messages within our UI to present different errors to the user.

This configuration can be provided through the UIConfig during the initialization of the UIManager. It allows either a function to manage every error in a comprehensive manner or a mapping from error codes to the corresponding messages. These messages can be expressed as simple strings or generated by functions returning strings.

Catch-all translation function

To create custom error messages for errors, the recommended approach is to use a function that will be invoked in the event of an error. This method is valuable when implementing a generic error message to handle a broad spectrum of diverse error codes.

let uiConfig = {
  errorMessages: function(error) {
      switch (error.code) {
        // Overwrite error 1000 'Unknown error'
        case 1000:
          return 'Houston, we have a problem'
          
        // Transform error 1201 'The downloaded manifest is invalid' to uppercase
        case 1201:
          var description = ErrorUtils.defaultErrorMessages[error.code];
          return description.toUpperCase();
          
        // Customize error 1207 'The manifest could not be loaded'
        case 1207:
          var statusCode = error.data.statusCode;
          return 'Manifest loading failed with HTTP error ' + statusCode;
      }
      // Return a general error message for all other errors
      return "An error occurred. Please try again.";
    }
};

Translating specific errors

An alternative approach involves creating a mapping between error codes and custom messages for specific errors. This method allows for the selective overriding of particular error messages without altering others, offering a targeted solution to error message customization.

let uiConfig = {
  errorMessages: {
      // Overwrite error 1000 'Unknown error'
      1000: 'Houston, we have a problem',

      // Transform error 1201 'Unsupported manifest format' to uppercase
      1201: function(error) {
        var description = ErrorUtils.defaultErrorMessages[error.code];
        return description.toUpperCase();
      },

      // Customize error 1207 'The manifest could not be loaded'
      1207: function(error) {
        var statusCode = error.data.statusCode;
        return 'Manifest loading failed with HTTP error ' + statusCode;
      }
  }
};