errorhandlingmechanism.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Wraps a storage mechanism with a custom error handler.
  16. *
  17. * @author ruilopes@google.com (Rui do Nascimento Dias Lopes)
  18. */
  19. goog.provide('goog.storage.mechanism.ErrorHandlingMechanism');
  20. goog.require('goog.storage.mechanism.Mechanism');
  21. /**
  22. * Wraps a storage mechanism with a custom error handler.
  23. *
  24. * @param {!goog.storage.mechanism.Mechanism} mechanism Underlying storage
  25. * mechanism.
  26. * @param {goog.storage.mechanism.ErrorHandlingMechanism.ErrorHandler}
  27. * errorHandler An error handler.
  28. * @constructor
  29. * @struct
  30. * @extends {goog.storage.mechanism.Mechanism}
  31. * @final
  32. */
  33. goog.storage.mechanism.ErrorHandlingMechanism = function(
  34. mechanism, errorHandler) {
  35. goog.storage.mechanism.ErrorHandlingMechanism.base(this, 'constructor');
  36. /**
  37. * The mechanism to be wrapped.
  38. * @type {!goog.storage.mechanism.Mechanism}
  39. * @private
  40. */
  41. this.mechanism_ = mechanism;
  42. /**
  43. * The error handler.
  44. * @type {goog.storage.mechanism.ErrorHandlingMechanism.ErrorHandler}
  45. * @private
  46. */
  47. this.errorHandler_ = errorHandler;
  48. };
  49. goog.inherits(
  50. goog.storage.mechanism.ErrorHandlingMechanism,
  51. goog.storage.mechanism.Mechanism);
  52. /**
  53. * Valid storage mechanism operations.
  54. * @enum {string}
  55. */
  56. goog.storage.mechanism.ErrorHandlingMechanism.Operation = {
  57. SET: 'set',
  58. GET: 'get',
  59. REMOVE: 'remove'
  60. };
  61. /**
  62. * A function that handles errors raised in goog.storage. Since some places in
  63. * the goog.storage codebase throw strings instead of Error objects, we accept
  64. * these as a valid parameter type. It supports the following arguments:
  65. *
  66. * 1) The raised error (either in Error or string form);
  67. * 2) The operation name which triggered the error, as defined per the
  68. * ErrorHandlingMechanism.Operation enum;
  69. * 3) The key that is passed to a storage method;
  70. * 4) An optional value that is passed to a storage method (only used in set
  71. * operations).
  72. *
  73. * @typedef {function(
  74. * (!Error|string),
  75. * goog.storage.mechanism.ErrorHandlingMechanism.Operation,
  76. * string,
  77. * *=)}
  78. */
  79. goog.storage.mechanism.ErrorHandlingMechanism.ErrorHandler;
  80. /** @override */
  81. goog.storage.mechanism.ErrorHandlingMechanism.prototype.set = function(
  82. key, value) {
  83. try {
  84. this.mechanism_.set(key, value);
  85. } catch (e) {
  86. this.errorHandler_(
  87. e, goog.storage.mechanism.ErrorHandlingMechanism.Operation.SET, key,
  88. value);
  89. }
  90. };
  91. /** @override */
  92. goog.storage.mechanism.ErrorHandlingMechanism.prototype.get = function(key) {
  93. try {
  94. return this.mechanism_.get(key);
  95. } catch (e) {
  96. this.errorHandler_(
  97. e, goog.storage.mechanism.ErrorHandlingMechanism.Operation.GET, key);
  98. return null;
  99. }
  100. };
  101. /** @override */
  102. goog.storage.mechanism.ErrorHandlingMechanism.prototype.remove = function(key) {
  103. try {
  104. this.mechanism_.remove(key);
  105. } catch (e) {
  106. this.errorHandler_(
  107. e, goog.storage.mechanism.ErrorHandlingMechanism.Operation.REMOVE, key);
  108. }
  109. };