TileProviderError.js 6.0 KB

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