TileProviderError.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import defaultValue from './defaultValue.js';
  2. import defined from './defined.js';
  3. import formatError from './formatError.js';
  4. /**
  5. * Provides details about an error that occurred in an {@link ImageryProvider} or a {@link TerrainProvider}.
  6. *
  7. * @alias TileProviderError
  8. * @constructor
  9. *
  10. * @param {ImageryProvider|TerrainProvider} provider The imagery or terrain provider that experienced the error.
  11. * @param {String} message A message describing the error.
  12. * @param {Number} [x] The X coordinate of the tile that experienced the error, or undefined if the error
  13. * is not specific to a particular tile.
  14. * @param {Number} [y] The Y coordinate of the tile that experienced the error, or undefined if the error
  15. * is not specific to a particular tile.
  16. * @param {Number} [level] The level of the tile that experienced the error, or undefined if the error
  17. * is not specific to a particular tile.
  18. * @param {Number} [timesRetried=0] The number of times this operation has been retried.
  19. * @param {Error} [error] The error or exception that occurred, if any.
  20. */
  21. function TileProviderError(provider, message, x, y, level, timesRetried, error) {
  22. /**
  23. * The {@link ImageryProvider} or {@link TerrainProvider} that experienced the error.
  24. * @type {ImageryProvider|TerrainProvider}
  25. */
  26. this.provider = provider;
  27. /**
  28. * The message describing the error.
  29. * @type {String}
  30. */
  31. this.message = message;
  32. /**
  33. * The X coordinate of the tile that experienced the error. If the error is not specific
  34. * to a particular tile, this property will be undefined.
  35. * @type {Number}
  36. */
  37. this.x = x;
  38. /**
  39. * The Y coordinate of the tile that experienced the error. If the error is not specific
  40. * to a particular tile, this property will be undefined.
  41. * @type {Number}
  42. */
  43. this.y = y;
  44. /**
  45. * The level-of-detail of the tile that experienced the error. If the error is not specific
  46. * to a particular tile, this property will be undefined.
  47. * @type {Number}
  48. */
  49. this.level = level;
  50. /**
  51. * The number of times this operation has been retried.
  52. * @type {Number}
  53. * @default 0
  54. */
  55. this.timesRetried = defaultValue(timesRetried, 0);
  56. /**
  57. * True if the failed operation should be retried; otherwise, false. The imagery or terrain provider
  58. * will set the initial value of this property before raising the event, but any listeners
  59. * can change it. The value after the last listener is invoked will be acted upon.
  60. * @type {Boolean}
  61. * @default false
  62. */
  63. this.retry = false;
  64. /**
  65. * The error or exception that occurred, if any.
  66. * @type {Error}
  67. */
  68. this.error = error;
  69. }
  70. /**
  71. * Handles an error in an {@link ImageryProvider} or {@link TerrainProvider} by raising an event if it has any listeners, or by
  72. * logging the error to the console if the event has no listeners. This method also tracks the number
  73. * of times the operation has been retried and will automatically retry if requested to do so by the
  74. * event listeners.
  75. *
  76. * @param {TileProviderError} previousError The error instance returned by this function the last
  77. * time it was called for this error, or undefined if this is the first time this error has
  78. * occurred.
  79. * @param {ImageryProvider|TerrainProvider} provider The imagery or terrain provider that encountered the error.
  80. * @param {Event} event The event to raise to inform listeners of the error.
  81. * @param {String} message The message describing the error.
  82. * @param {Number} x The X coordinate of the tile that experienced the error, or undefined if the
  83. * error is not specific to a particular tile.
  84. * @param {Number} y The Y coordinate of the tile that experienced the error, or undefined if the
  85. * error is not specific to a particular tile.
  86. * @param {Number} level The level-of-detail of the tile that experienced the error, or undefined if the
  87. * error is not specific to a particular tile.
  88. * @param {TileProviderError~RetryFunction} retryFunction The function to call to retry the operation. If undefined, the
  89. * operation will not be retried.
  90. * @param {Error} [errorDetails] The error or exception that occurred, if any.
  91. * @returns {TileProviderError} The error instance that was passed to the event listeners and that
  92. * should be passed to this function the next time it is called for the same error in order
  93. * to track retry counts.
  94. */
  95. TileProviderError.handleError = function(previousError, provider, event, message, x, y, level, retryFunction, errorDetails) {
  96. var error = previousError;
  97. if (!defined(previousError)) {
  98. error = new TileProviderError(provider, message, x, y, level, 0, errorDetails);
  99. } else {
  100. error.provider = provider;
  101. error.message = message;
  102. error.x = x;
  103. error.y = y;
  104. error.level = level;
  105. error.retry = false;
  106. error.error = errorDetails;
  107. ++error.timesRetried;
  108. }
  109. if (event.numberOfListeners > 0) {
  110. event.raiseEvent(error);
  111. } else {
  112. console.log('An error occurred in "' + provider.constructor.name + '": ' + formatError(message));
  113. }
  114. if (error.retry && defined(retryFunction)) {
  115. retryFunction();
  116. }
  117. return error;
  118. };
  119. /**
  120. * Handles success of an operation by resetting the retry count of a previous error, if any. This way,
  121. * if the error occurs again in the future, the listeners will be informed that it has not yet been retried.
  122. *
  123. * @param {TileProviderError} previousError The previous error, or undefined if this operation has
  124. * not previously resulted in an error.
  125. */
  126. TileProviderError.handleSuccess = function(previousError) {
  127. if (defined(previousError)) {
  128. previousError.timesRetried = -1;
  129. }
  130. };
  131. /**
  132. * A function that will be called to retry the operation.
  133. * @callback TileProviderError~RetryFunction
  134. */
  135. export default TileProviderError;