Debian Codegraph @

closure-library/closure/goog/storage/mechanism/errorhandlingmechanism.js

raw
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 /**
16 * @fileoverview Wraps a storage mechanism with a custom error handler.
17 *
18 * @author [email protected] (Rui do Nascimento Dias Lopes)
19 */
20
21 goog.provide('goog.storage.mechanism.ErrorHandlingMechanism');
22
23 goog.require('goog.storage.mechanism.Mechanism');
24
25
26
27 /**
28 * Wraps a storage mechanism with a custom error handler.
29 *
30 * @param {!goog.storage.mechanism.Mechanism} mechanism Underlying storage
31 * mechanism.
32 * @param {goog.storage.mechanism.ErrorHandlingMechanism.ErrorHandler}
33 * errorHandler An error handler.
34 * @constructor
35 * @struct
36 * @extends {goog.storage.mechanism.Mechanism}
37 * @final
38 */
39 goog.storage.mechanism.ErrorHandlingMechanism = function(
40 mechanism, errorHandler) {
41 goog.storage.mechanism.ErrorHandlingMechanism.base(this, 'constructor');
42
43 /**
44 * The mechanism to be wrapped.
45 * @type {!goog.storage.mechanism.Mechanism}
46 * @private
47 */
48 this.mechanism_ = mechanism;
49
50 /**
51 * The error handler.
52 * @type {goog.storage.mechanism.ErrorHandlingMechanism.ErrorHandler}
53 * @private
54 */
55 this.errorHandler_ = errorHandler;
56 };
57 goog.inherits(
58 goog.storage.mechanism.ErrorHandlingMechanism,
59 goog.storage.mechanism.Mechanism);
60
61
62 /**
63 * Valid storage mechanism operations.
64 * @enum {string}
65 */
66 goog.storage.mechanism.ErrorHandlingMechanism.Operation = {
67 SET: 'set',
68 GET: 'get',
69 REMOVE: 'remove'
70 };
71
72
73 /**
74 * A function that handles errors raised in goog.storage. Since some places in
75 * the goog.storage codebase throw strings instead of Error objects, we accept
76 * these as a valid parameter type. It supports the following arguments:
77 *
78 * 1) The raised error (either in Error or string form);
79 * 2) The operation name which triggered the error, as defined per the
80 * ErrorHandlingMechanism.Operation enum;
81 * 3) The key that is passed to a storage method;
82 * 4) An optional value that is passed to a storage method (only used in set
83 * operations).
84 *
85 * @typedef {function(
86 * (!Error|string),
87 * goog.storage.mechanism.ErrorHandlingMechanism.Operation,
88 * string,
89 * *=)}
90 */
91 goog.storage.mechanism.ErrorHandlingMechanism.ErrorHandler;
92
93
94 /** @override */
95 goog.storage.mechanism.ErrorHandlingMechanism.prototype.set = function(
96 key, value) {
97 try {
98 this.mechanism_.set(key, value);
99 } catch (e) {
100 this.errorHandler_(
101 e, goog.storage.mechanism.ErrorHandlingMechanism.Operation.SET, key,
102 value);
103 }
104 };
105
106
107 /** @override */
108 goog.storage.mechanism.ErrorHandlingMechanism.prototype.get = function(key) {
109 try {
110 return this.mechanism_.get(key);
111 } catch (e) {
112 this.errorHandler_(
113 e, goog.storage.mechanism.ErrorHandlingMechanism.Operation.GET, key);
114 return null;
115 }
116 };
117
118
119 /** @override */
120 goog.storage.mechanism.ErrorHandlingMechanism.prototype.remove = function(key) {
121 try {
122 this.mechanism_.remove(key);
123 } catch (e) {
124 this.errorHandler_(
125 e, goog.storage.mechanism.ErrorHandlingMechanism.Operation.REMOVE, key);
126 }
127 };