OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
kelvinp
2015/02/27 00:58:07
consider wrapping this file in a IEFE
/** @suppre
John Williams
2015/02/27 21:42:11
Adding this wrapper caused the compiler to act as
| |
6 | 6 |
7 /** @suppress {duplicate} */ | 7 /** @suppress {duplicate} */ |
8 var remoting = remoting || {}; | 8 var remoting = remoting || {}; |
9 | 9 |
10 /** | 10 /** |
11 * A wrapper for remoting.Error.Tag. Having a wrapper makes it | |
12 * possible to use instanceof checks on caught exceptions. It also | |
13 * allows adding more detailed error information if desired. | |
14 * | |
15 * @constructor | |
16 * @param {remoting.Error.Tag} tag | |
17 * @param {string=} opt_message | |
18 */ | |
19 remoting.Error = function(tag, opt_message) { | |
20 /** @const */ | |
21 this.tag = tag; | |
22 | |
23 /** @const {?string} */ | |
24 this.message = opt_message || null; | |
25 }; | |
26 | |
27 /** | |
11 * @enum {string} All error messages from messages.json | 28 * @enum {string} All error messages from messages.json |
12 */ | 29 */ |
13 remoting.Error = { | 30 remoting.Error.Tag = { |
14 NONE: '', | 31 NONE: '', |
15 | 32 |
16 // Used to signify that an operation was cancelled by the user. This should | 33 // Used to signify that an operation was cancelled by the user. This should |
17 // not normally cause the error text to be shown to the user, so the | 34 // not normally cause the error text to be shown to the user, so the |
18 // i18n-content prefix is not needed in this case. | 35 // i18n-content prefix is not needed in this case. |
19 CANCELLED: '__CANCELLED__', | 36 CANCELLED: '__CANCELLED__', |
20 | 37 |
21 INVALID_ACCESS_CODE: /*i18n-content*/'ERROR_INVALID_ACCESS_CODE', | 38 INVALID_ACCESS_CODE: /*i18n-content*/'ERROR_INVALID_ACCESS_CODE', |
22 MISSING_PLUGIN: /*i18n-content*/'ERROR_MISSING_PLUGIN', | 39 MISSING_PLUGIN: /*i18n-content*/'ERROR_MISSING_PLUGIN', |
23 AUTHENTICATION_FAILED: /*i18n-content*/'ERROR_AUTHENTICATION_FAILED', | 40 AUTHENTICATION_FAILED: /*i18n-content*/'ERROR_AUTHENTICATION_FAILED', |
24 HOST_IS_OFFLINE: /*i18n-content*/'ERROR_HOST_IS_OFFLINE', | 41 HOST_IS_OFFLINE: /*i18n-content*/'ERROR_HOST_IS_OFFLINE', |
25 INCOMPATIBLE_PROTOCOL: /*i18n-content*/'ERROR_INCOMPATIBLE_PROTOCOL', | 42 INCOMPATIBLE_PROTOCOL: /*i18n-content*/'ERROR_INCOMPATIBLE_PROTOCOL', |
26 BAD_PLUGIN_VERSION: /*i18n-content*/'ERROR_BAD_PLUGIN_VERSION', | 43 BAD_PLUGIN_VERSION: /*i18n-content*/'ERROR_BAD_PLUGIN_VERSION', |
27 NETWORK_FAILURE: /*i18n-content*/'ERROR_NETWORK_FAILURE', | 44 NETWORK_FAILURE: /*i18n-content*/'ERROR_NETWORK_FAILURE', |
28 HOST_OVERLOAD: /*i18n-content*/'ERROR_HOST_OVERLOAD', | 45 HOST_OVERLOAD: /*i18n-content*/'ERROR_HOST_OVERLOAD', |
29 UNEXPECTED: /*i18n-content*/'ERROR_UNEXPECTED', | 46 UNEXPECTED: /*i18n-content*/'ERROR_UNEXPECTED', |
30 SERVICE_UNAVAILABLE: /*i18n-content*/'ERROR_SERVICE_UNAVAILABLE', | 47 SERVICE_UNAVAILABLE: /*i18n-content*/'ERROR_SERVICE_UNAVAILABLE', |
31 NOT_AUTHENTICATED: /*i18n-content*/'ERROR_NOT_AUTHENTICATED', | 48 NOT_AUTHENTICATED: /*i18n-content*/'ERROR_NOT_AUTHENTICATED', |
32 INVALID_HOST_DOMAIN: /*i18n-content*/'ERROR_INVALID_HOST_DOMAIN', | 49 INVALID_HOST_DOMAIN: /*i18n-content*/'ERROR_INVALID_HOST_DOMAIN', |
33 P2P_FAILURE: /*i18n-content*/'ERROR_P2P_FAILURE', | 50 P2P_FAILURE: /*i18n-content*/'ERROR_P2P_FAILURE', |
34 REGISTRATION_FAILED: /*i18n-content*/'ERROR_HOST_REGISTRATION_FAILED', | 51 REGISTRATION_FAILED: /*i18n-content*/'ERROR_HOST_REGISTRATION_FAILED', |
35 NOT_AUTHORIZED: /*i18n-content*/'ERROR_NOT_AUTHORIZED', | 52 NOT_AUTHORIZED: /*i18n-content*/'ERROR_NOT_AUTHORIZED', |
36 | 53 |
37 // TODO(garykac): Move app-specific errors into separate location. | 54 // TODO(garykac): Move app-specific errors into separate location. |
38 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' | 55 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' |
39 }; | 56 }; |
40 | 57 |
58 // A whole bunch of semi-redundant constants, mostly to reduce to size | |
59 // of the diff that introduced the remoting.Error class. | |
60 | |
61 /** @const */ | |
62 remoting.Error.NONE = new remoting.Error(remoting.Error.Tag.NONE); | |
63 | |
64 /** @const */ | |
65 remoting.Error.CANCELLED = | |
66 new remoting.Error(remoting.Error.Tag.CANCELLED); | |
67 | |
68 /** @const */ | |
69 remoting.Error.INVALID_ACCESS_CODE = | |
70 new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); | |
71 | |
72 /** @const */ | |
73 remoting.Error.MISSING_PLUGIN = | |
74 new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN); | |
75 | |
76 /** @const */ | |
77 remoting.Error.AUTHENTICATION_FAILED = | |
78 new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); | |
79 | |
80 /** @const */ | |
81 remoting.Error.HOST_IS_OFFLINE = | |
82 new remoting.Error(remoting.Error.Tag.HOST_IS_OFFLINE); | |
83 | |
84 /** @const */ | |
85 remoting.Error.INCOMPATIBLE_PROTOCOL = | |
86 new remoting.Error(remoting.Error.Tag.INCOMPATIBLE_PROTOCOL); | |
87 | |
88 /** @const */ | |
89 remoting.Error.BAD_PLUGIN_VERSION = | |
90 new remoting.Error(remoting.Error.Tag.BAD_PLUGIN_VERSION); | |
91 | |
92 /** @const */ | |
93 remoting.Error.NETWORK_FAILURE = | |
94 new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); | |
95 | |
96 /** @const */ | |
97 remoting.Error.HOST_OVERLOAD = | |
98 new remoting.Error(remoting.Error.Tag.HOST_OVERLOAD); | |
99 | |
100 /** @const */ | |
101 remoting.Error.UNEXPECTED = | |
102 new remoting.Error(remoting.Error.Tag.UNEXPECTED); | |
103 | |
104 /** @const */ | |
105 remoting.Error.SERVICE_UNAVAILABLE = | |
106 new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); | |
107 | |
108 /** @const */ | |
109 remoting.Error.NOT_AUTHENTICATED = | |
110 new remoting.Error(remoting.Error.Tag.NOT_AUTHENTICATED); | |
111 | |
112 /** @const */ | |
113 remoting.Error.INVALID_HOST_DOMAIN = | |
114 new remoting.Error(remoting.Error.Tag.INVALID_HOST_DOMAIN); | |
115 | |
116 /** @const */ | |
117 remoting.Error.P2P_FAILURE = | |
118 new remoting.Error(remoting.Error.Tag.P2P_FAILURE); | |
119 | |
120 /** @const */ | |
121 remoting.Error.REGISTRATION_FAILED = | |
122 new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); | |
123 | |
124 /** @const */ | |
125 remoting.Error.NOT_AUTHORIZED = | |
126 new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); | |
127 | |
128 /** @const */ | |
129 remoting.Error.APP_NOT_AUTHORIZED = | |
130 new remoting.Error(remoting.Error.Tag.APP_NOT_AUTHORIZED); | |
131 | |
41 /** | 132 /** |
42 * @param {number} httpStatus An HTTP status code. | 133 * @param {number} httpStatus An HTTP status tag. |
Jamie
2015/02/26 18:57:42
Most of the changes in this block seem to global s
John Williams
2015/02/27 21:42:11
Fixed.
| |
43 * @return {remoting.Error} The remoting.Error enum corresponding to the | 134 * @return {!remoting.Error} The remoting.Error enum corresponding to the |
44 * specified HTTP status code. | 135 * specified HTTP status tag. |
45 */ | 136 */ |
46 remoting.Error.fromHttpStatus = function(httpStatus) { | 137 remoting.Error.fromHttpStatus = function(httpStatus) { |
47 if (httpStatus == 0) { | 138 if (httpStatus == 0) { |
48 return remoting.Error.NETWORK_FAILURE; | 139 return remoting.Error.NETWORK_FAILURE; |
49 } else if (httpStatus >= 200 && httpStatus < 300) { | 140 } else if (httpStatus >= 200 && httpStatus < 300) { |
50 return remoting.Error.NONE; | 141 return remoting.Error.NONE; |
51 } else if (httpStatus == 400 || httpStatus == 401) { | 142 } else if (httpStatus == 400 || httpStatus == 401) { |
52 return remoting.Error.AUTHENTICATION_FAILED; | 143 return remoting.Error.AUTHENTICATION_FAILED; |
53 } else if (httpStatus >= 500 && httpStatus < 600) { | 144 } else if (httpStatus >= 500 && httpStatus < 600) { |
54 return remoting.Error.SERVICE_UNAVAILABLE; | 145 return remoting.Error.SERVICE_UNAVAILABLE; |
55 } else { | 146 } else { |
56 console.warn('Unexpected HTTP error code: ' + httpStatus); | 147 console.warn('Unexpected HTTP error tag: ' + httpStatus); |
57 // Return AUTHENTICATION_FAILED by default, so that the user can try to | 148 |
149 // Tag = AUTHENTICATION_FAILED by default, so that the user can try to | |
58 // recover from an unexpected failure by signing in again. | 150 // recover from an unexpected failure by signing in again. |
59 // TODO(jamiewalch): Return UNEXPECTED here and let calling code treat that | 151 // TODO(jamiewalch): Tag = UNEXPECTED here and let calling tag treat that |
60 // as "sign-in required" if necessary. | 152 // as "sign-in required" if necessary. |
61 return remoting.Error.AUTHENTICATION_FAILED; | 153 return remoting.Error.AUTHENTICATION_FAILED; |
62 } | 154 } |
63 }; | 155 }; |
64 | 156 |
65 /** | 157 /** |
66 * Create an error-handling function suitable for passing to a | 158 * Create an error-handling function suitable for passing to a |
67 * Promise's "catch" method. | 159 * Promise's "catch" method. |
68 * | 160 * |
69 * @param {function(remoting.Error):void} onError | 161 * @param {function(!remoting.Error):void} onError |
70 * @return {function(*):void} | 162 * @return {function(*):void} |
71 */ | 163 */ |
72 remoting.Error.handler = function(onError) { | 164 remoting.Error.handler = function(onError) { |
73 return function(/** * */ error) { | 165 return function(/** * */ error) { |
74 if (typeof error == 'string') { | 166 if (error instanceof remoting.Error) { |
75 onError(/** @type {remoting.Error} */ (error)); | 167 onError(/** @type {!remoting.Error} */ (error)); |
76 } else { | 168 } else { |
77 console.error('Unexpected error: %o', error); | 169 console.error('Unexpected error: %o', error); |
78 onError(remoting.Error.UNEXPECTED); | 170 onError(remoting.Error.UNEXPECTED); |
79 } | 171 } |
80 }; | 172 }; |
81 }; | 173 }; |
82 | |
83 // /** | |
84 // * @param {(!Promise<T>| | |
85 // * function(function(T):void,function(remoting.Error):void))} arg | |
86 // * @constructor | |
87 // * @template T | |
88 // */ | |
89 // remoting.Promise = function(arg) { | |
90 // var promise; | |
91 // if (typeof arg == 'function') { | |
92 // promise = new Promise(arg); | |
93 // } else { | |
94 // promise = arg; | |
95 // } | |
96 | |
97 // /** @const */ | |
98 // this.promise = promise; | |
99 // }; | |
100 | |
101 // /** | |
102 // * @param {?function(T)} onResolve | |
103 // * @param {?function(remoting.Error)=} opt_onReject | |
104 // * @return {!remoting.Promise} | |
105 // */ | |
106 // remoting.Promise.prototype.then = function(onResolve, opt_onReject) { | |
107 // return new remoting.Promise(this.promise.then( | |
108 // onResolve, | |
109 // opt_onReject && function(/** * */ error) { | |
110 // if (typeof error == 'string') { | |
111 // opt_onReject(/** @type {remoting.Error} */ (error)); | |
112 // } else { | |
113 // console.error('Unexpected error: %o', error); | |
114 // opt_onReject(remoting.Error.UNEXPECTED); | |
115 // } | |
116 // })); | |
117 // }; | |
118 | |
119 // /** | |
120 // * @param {?function(remoting.Error)} onReject | |
121 // * @return {!remoting.Promise<T>} | |
122 // */ | |
123 // remoting.Promise.prototype.catch = function(onReject) { | |
124 // return this.then(null, onReject); | |
125 // }; | |
OLD | NEW |