OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 'use strict'; | |
6 | |
7 /** @suppress {duplicate} */ | |
8 var remoting = remoting || {}; | |
9 | |
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_detail | |
18 */ | |
19 remoting.Error = function(tag, opt_detail) { | |
20 /** @private @const {remoting.Error.Tag} */ | |
21 this.tag_ = tag; | |
22 | |
23 /** @const {?string} */ | |
24 this.detail_ = opt_detail || null; | |
25 }; | |
26 | |
27 /** | |
28 * @override | |
29 */ | |
30 remoting.Error.prototype.toString = function() { | |
31 var result = this.tag_; | |
32 if (this.detail_ != null) { | |
33 result += ' (' + this.detail_ + ')'; | |
34 } | |
35 return result; | |
36 }; | |
37 | |
38 /** | |
39 * @return {remoting.Error.Tag} The tag used to create this Error. | |
40 */ | |
41 remoting.Error.prototype.getTag = function() { | |
42 return this.tag_; | |
43 }; | |
44 | |
45 /** | |
46 * @return {?string} The detail string passed to the constructor, if any. | |
47 */ | |
48 remoting.Error.prototype.getDetail = function() { | |
49 return this.detail_; | |
50 }; | |
51 | |
52 /** | |
53 * Checks the type of an error. | |
54 * @param {remoting.Error.Tag} tag | |
55 * @param {...remoting.Error.Tag} var_args | |
56 * @return {boolean} True if this object has one of the specified tags. | |
57 */ | |
58 remoting.Error.prototype.hasTag = function(tag, var_args) { | |
59 var thisTag = this.tag_; | |
60 return Array.prototype.some.call( | |
61 arguments, | |
62 function(/** remoting.Error.Tag */ tag) { | |
63 return thisTag == tag; | |
64 }); | |
65 }; | |
66 | |
67 /** | |
68 * @return {boolean} True if this object's tag is NONE, meaning this | |
69 * object represents the lack of an error. | |
70 */ | |
71 remoting.Error.prototype.isNone = function() { | |
72 return this.hasTag(remoting.Error.Tag.NONE); | |
73 }; | |
74 | |
75 /** | |
76 * Convenience method for creating the second most common error type. | |
77 * @return {!remoting.Error} | |
78 */ | |
79 remoting.Error.none = function() { | |
80 return new remoting.Error(remoting.Error.Tag.NONE); | |
81 }; | |
82 | |
83 /** | |
84 * Convenience method for creating the most common error type. | |
85 * @param {string=} opt_detail | |
86 * @return {!remoting.Error} | |
87 */ | |
88 remoting.Error.unexpected = function(opt_detail) { | |
89 return new remoting.Error(remoting.Error.Tag.UNEXPECTED, opt_detail); | |
90 }; | |
91 | |
92 /** | |
93 * @enum {string} All error messages from messages.json | |
94 */ | |
95 remoting.Error.Tag = { | |
96 NONE: '', | |
97 | |
98 // Used to signify that an operation was cancelled by the user. This should | |
99 // not normally cause the error text to be shown to the user, so the | |
100 // i18n-content prefix is not needed in this case. | |
101 CANCELLED: '__CANCELLED__', | |
102 | |
103 INVALID_ACCESS_CODE: /*i18n-content*/'ERROR_INVALID_ACCESS_CODE', | |
104 MISSING_PLUGIN: /*i18n-content*/'ERROR_MISSING_PLUGIN', | |
105 AUTHENTICATION_FAILED: /*i18n-content*/'ERROR_AUTHENTICATION_FAILED', | |
106 HOST_IS_OFFLINE: /*i18n-content*/'ERROR_HOST_IS_OFFLINE', | |
107 INCOMPATIBLE_PROTOCOL: /*i18n-content*/'ERROR_INCOMPATIBLE_PROTOCOL', | |
108 BAD_PLUGIN_VERSION: /*i18n-content*/'ERROR_BAD_PLUGIN_VERSION', | |
109 NETWORK_FAILURE: /*i18n-content*/'ERROR_NETWORK_FAILURE', | |
110 HOST_OVERLOAD: /*i18n-content*/'ERROR_HOST_OVERLOAD', | |
111 UNEXPECTED: /*i18n-content*/'ERROR_UNEXPECTED', | |
112 SERVICE_UNAVAILABLE: /*i18n-content*/'ERROR_SERVICE_UNAVAILABLE', | |
113 NOT_AUTHENTICATED: /*i18n-content*/'ERROR_NOT_AUTHENTICATED', | |
114 NOT_FOUND: /*i18n-content*/'ERROR_NOT_FOUND', | |
115 INVALID_HOST_DOMAIN: /*i18n-content*/'ERROR_INVALID_HOST_DOMAIN', | |
116 P2P_FAILURE: /*i18n-content*/'ERROR_P2P_FAILURE', | |
117 REGISTRATION_FAILED: /*i18n-content*/'ERROR_HOST_REGISTRATION_FAILED', | |
118 NOT_AUTHORIZED: /*i18n-content*/'ERROR_NOT_AUTHORIZED', | |
119 | |
120 // TODO(garykac): Move app-specific errors into separate location. | |
121 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' | |
122 }; | |
123 | |
124 // A whole bunch of semi-redundant constants, mostly to reduce to size | |
125 // of the diff that introduced the remoting.Error class. | |
126 // | |
127 // Please don't add any more constants here; just call the | |
128 // remoting.Error constructor directly | |
129 | |
130 /** | |
131 * @param {number} httpStatus An HTTP status code. | |
132 * @return {!remoting.Error} The remoting.Error enum corresponding to the | |
133 * specified HTTP status code. | |
134 */ | |
135 remoting.Error.fromHttpStatus = function(httpStatus) { | |
136 if (httpStatus == 0) { | |
137 return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); | |
138 } else if (httpStatus >= 200 && httpStatus < 300) { | |
139 return remoting.Error.none(); | |
140 } else if (httpStatus == 400 || httpStatus == 401) { | |
141 return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); | |
142 } else if (httpStatus == 403) { | |
143 return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); | |
144 } else if (httpStatus == 404) { | |
145 return new remoting.Error(remoting.Error.Tag.NOT_FOUND); | |
146 } else if (httpStatus >= 500 && httpStatus < 600) { | |
147 return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); | |
148 } else { | |
149 console.warn('Unexpected HTTP error code: ' + httpStatus); | |
150 return remoting.Error.unexpected(); | |
151 } | |
152 }; | |
153 | |
154 /** | |
155 * Create an error-handling function suitable for passing to a | |
156 * Promise's "catch" method. | |
157 * | |
158 * @param {function(!remoting.Error):void} onError | |
159 * @return {function(*):void} | |
160 */ | |
161 remoting.Error.handler = function(onError) { | |
162 return function(/** * */ error) { | |
163 if (error instanceof remoting.Error) { | |
164 onError(/** @type {!remoting.Error} */ (error)); | |
165 } else { | |
166 console.error('Unexpected error:', error); | |
167 onError(remoting.Error.unexpected()); | |
168 } | |
169 }; | |
170 }; | |
OLD | NEW |