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'; |
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 // Please don't add any more constants here; just call the | |
62 // remoting.Error constructor directly | |
63 | |
64 /** @const */ | |
65 remoting.Error.NONE = new remoting.Error(remoting.Error.Tag.NONE); | |
66 | |
67 /** @const */ | |
68 remoting.Error.CANCELLED = | |
69 new remoting.Error(remoting.Error.Tag.CANCELLED); | |
70 | |
71 /** @const */ | |
72 remoting.Error.INVALID_ACCESS_CODE = | |
73 new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); | |
74 | |
75 /** @const */ | |
76 remoting.Error.MISSING_PLUGIN = | |
77 new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN); | |
78 | |
79 /** @const */ | |
80 remoting.Error.AUTHENTICATION_FAILED = | |
81 new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); | |
82 | |
83 /** @const */ | |
84 remoting.Error.HOST_IS_OFFLINE = | |
85 new remoting.Error(remoting.Error.Tag.HOST_IS_OFFLINE); | |
86 | |
87 /** @const */ | |
88 remoting.Error.INCOMPATIBLE_PROTOCOL = | |
89 new remoting.Error(remoting.Error.Tag.INCOMPATIBLE_PROTOCOL); | |
90 | |
91 /** @const */ | |
92 remoting.Error.BAD_PLUGIN_VERSION = | |
93 new remoting.Error(remoting.Error.Tag.BAD_PLUGIN_VERSION); | |
94 | |
95 /** @const */ | |
96 remoting.Error.NETWORK_FAILURE = | |
97 new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); | |
98 | |
99 /** @const */ | |
100 remoting.Error.HOST_OVERLOAD = | |
101 new remoting.Error(remoting.Error.Tag.HOST_OVERLOAD); | |
102 | |
103 /** @const */ | |
104 remoting.Error.UNEXPECTED = | |
105 new remoting.Error(remoting.Error.Tag.UNEXPECTED); | |
106 | |
107 /** @const */ | |
108 remoting.Error.SERVICE_UNAVAILABLE = | |
109 new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); | |
110 | |
111 /** @const */ | |
112 remoting.Error.NOT_AUTHENTICATED = | |
113 new remoting.Error(remoting.Error.Tag.NOT_AUTHENTICATED); | |
114 | |
115 /** @const */ | |
116 remoting.Error.INVALID_HOST_DOMAIN = | |
117 new remoting.Error(remoting.Error.Tag.INVALID_HOST_DOMAIN); | |
118 | |
119 /** @const */ | |
120 remoting.Error.P2P_FAILURE = | |
121 new remoting.Error(remoting.Error.Tag.P2P_FAILURE); | |
122 | |
123 /** @const */ | |
124 remoting.Error.REGISTRATION_FAILED = | |
125 new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); | |
126 | |
127 /** @const */ | |
128 remoting.Error.NOT_AUTHORIZED = | |
129 new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); | |
130 | |
131 /** @const */ | |
132 remoting.Error.APP_NOT_AUTHORIZED = | |
133 new remoting.Error(remoting.Error.Tag.APP_NOT_AUTHORIZED); | |
134 | |
41 /** | 135 /** |
42 * @param {number} httpStatus An HTTP status code. | 136 * @param {number} httpStatus An HTTP status code. |
43 * @return {remoting.Error} The remoting.Error enum corresponding to the | 137 * @return {!remoting.Error} The remoting.Error enum corresponding to the |
44 * specified HTTP status code. | 138 * specified HTTP status code. |
45 */ | 139 */ |
46 remoting.Error.fromHttpStatus = function(httpStatus) { | 140 remoting.Error.fromHttpStatus = function(httpStatus) { |
47 if (httpStatus == 0) { | 141 if (httpStatus == 0) { |
48 return remoting.Error.NETWORK_FAILURE; | 142 return remoting.Error.NETWORK_FAILURE; |
49 } else if (httpStatus >= 200 && httpStatus < 300) { | 143 } else if (httpStatus >= 200 && httpStatus < 300) { |
50 return remoting.Error.NONE; | 144 return remoting.Error.NONE; |
51 } else if (httpStatus == 400 || httpStatus == 401) { | 145 } else if (httpStatus == 400 || httpStatus == 401) { |
52 return remoting.Error.AUTHENTICATION_FAILED; | 146 return remoting.Error.AUTHENTICATION_FAILED; |
53 } else if (httpStatus >= 500 && httpStatus < 600) { | 147 } else if (httpStatus >= 500 && httpStatus < 600) { |
54 return remoting.Error.SERVICE_UNAVAILABLE; | 148 return remoting.Error.SERVICE_UNAVAILABLE; |
55 } else { | 149 } else { |
56 console.warn('Unexpected HTTP error code: ' + httpStatus); | 150 console.warn('Unexpected HTTP error code: ' + httpStatus); |
151 | |
Jamie
2015/03/02 18:28:09
No need for a blank line here.
John Williams
2015/03/02 22:01:37
Done.
| |
57 // Return AUTHENTICATION_FAILED by default, so that the user can try to | 152 // Return AUTHENTICATION_FAILED by default, so that the user can try to |
58 // recover from an unexpected failure by signing in again. | 153 // recover from an unexpected failure by signing in again. |
59 // TODO(jamiewalch): Return UNEXPECTED here and let calling code treat that | 154 // TODO(jamiewalch): Tag = UNEXPECTED here and let calling tag treat that |
Jamie
2015/03/02 18:28:09
Search/replace error?
John Williams
2015/03/02 22:01:36
Done.
| |
60 // as "sign-in required" if necessary. | 155 // as "sign-in required" if necessary. |
61 return remoting.Error.AUTHENTICATION_FAILED; | 156 return remoting.Error.AUTHENTICATION_FAILED; |
62 } | 157 } |
63 }; | 158 }; |
64 | 159 |
65 /** | 160 /** |
66 * Create an error-handling function suitable for passing to a | 161 * Create an error-handling function suitable for passing to a |
67 * Promise's "catch" method. | 162 * Promise's "catch" method. |
68 * | 163 * |
69 * @param {function(remoting.Error):void} onError | 164 * @param {function(!remoting.Error):void} onError |
70 * @return {function(*):void} | 165 * @return {function(*):void} |
71 */ | 166 */ |
72 remoting.Error.handler = function(onError) { | 167 remoting.Error.handler = function(onError) { |
73 return function(/** * */ error) { | 168 return function(/** * */ error) { |
74 if (typeof error == 'string') { | 169 if (error instanceof remoting.Error) { |
75 onError(/** @type {remoting.Error} */ (error)); | 170 onError(/** @type {!remoting.Error} */ (error)); |
76 } else { | 171 } else { |
77 console.error('Unexpected error: %o', error); | 172 console.error('Unexpected error: %o', error); |
78 onError(remoting.Error.UNEXPECTED); | 173 onError(remoting.Error.UNEXPECTED); |
79 } | 174 } |
80 }; | 175 }; |
81 }; | 176 }; |
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 |