Chromium Code Reviews| 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 | 11 * A wrapper for remoting.Error.Tag. Having a wrapper makes it |
| 12 * possible to use instanceof checks on caught exceptions. It also | 12 * possible to use instanceof checks on caught exceptions. It also |
| 13 * allows adding more detailed error information if desired. | 13 * allows adding more detailed error information if desired. |
| 14 * | 14 * |
| 15 * @constructor | 15 * @constructor |
| 16 * @param {remoting.Error.Tag} tag | 16 * @param {remoting.Error.Tag} tag |
| 17 * @param {string=} opt_message | 17 * @param {string=} opt_message |
| 18 */ | 18 */ |
| 19 remoting.Error = function(tag, opt_message) { | 19 remoting.Error = function(tag, opt_message) { |
| 20 /** @const {remoting.Error.Tag} */ | 20 /** @private @const {remoting.Error.Tag} */ |
| 21 this.tag = tag; | 21 this.tag_ = tag; |
| 22 | 22 |
| 23 /** @const {?string} */ | 23 /** @const {?string} */ |
| 24 this.message = opt_message || null; | 24 this.message = opt_message || null; |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * @return {boolean} True if this object represents an error | 28 * @return {string} |
| 29 * condition. | |
| 30 */ | 29 */ |
| 31 remoting.Error.prototype.isError = function() { | 30 remoting.Error.prototype.getTagForLocalization = function() { |
|
Jamie
2015/03/13 17:56:17
I think getTag is a better name, but then I'm not
John Williams
2015/03/13 19:30:29
No, == vs. === is not a major issue AFAICT. What
Jamie
2015/03/13 20:26:00
I'm inclined to agree with you, and the new form o
| |
| 32 return this.tag != remoting.Error.Tag.NONE; | 31 return this.tag_; |
| 33 }; | 32 }; |
| 34 | 33 |
| 35 /** | 34 /** |
| 35 * @return {string} | |
| 36 */ | |
| 37 remoting.Error.prototype.getTagForServerLog = function() { | |
|
Jamie
2015/03/13 17:56:17
Another point in favour of calling the previous me
John Williams
2015/03/13 19:30:29
Done.
| |
| 38 // Directory service should be updated if a new string is added here as | |
| 39 // otherwise the error code will be ignored (i.e. recorded as 0 instead). | |
| 40 switch (this.tag_) { | |
| 41 case remoting.Error.Tag.NONE: | |
| 42 return 'none'; | |
| 43 case remoting.Error.Tag.INVALID_ACCESS_CODE: | |
| 44 return 'invalid-access-code'; | |
| 45 case remoting.Error.Tag.MISSING_PLUGIN: | |
| 46 return 'missing_plugin'; | |
| 47 case remoting.Error.Tag.AUTHENTICATION_FAILED: | |
| 48 return 'authentication-failed'; | |
| 49 case remoting.Error.Tag.HOST_IS_OFFLINE: | |
| 50 return 'host-is-offline'; | |
| 51 case remoting.Error.Tag.INCOMPATIBLE_PROTOCOL: | |
| 52 return 'incompatible-protocol'; | |
| 53 case remoting.Error.Tag.BAD_PLUGIN_VERSION: | |
| 54 return 'bad-plugin-version'; | |
| 55 case remoting.Error.Tag.NETWORK_FAILURE: | |
| 56 return 'network-failure'; | |
| 57 case remoting.Error.Tag.HOST_OVERLOAD: | |
| 58 return 'host-overload'; | |
| 59 case remoting.Error.Tag.P2P_FAILURE: | |
| 60 return 'p2p-failure'; | |
| 61 case remoting.Error.Tag.UNEXPECTED: | |
| 62 return 'unexpected'; | |
| 63 default: | |
| 64 return 'unknown-' + this.tag_; | |
| 65 } | |
| 66 }; | |
| 67 | |
| 68 /** | |
| 69 * Checks the type of an error. | |
| 70 * @param {remoting.Error.Tag} tag | |
| 71 * @return {boolean} True if this object has the specified tag. | |
| 72 */ | |
| 73 remoting.Error.prototype.hasTag = function(tag) { | |
| 74 return this.tag_ == tag; | |
| 75 }; | |
| 76 | |
| 77 /** | |
| 78 * Checks the type of an error. | |
| 79 * @param {!Array<remoting.Error.Tag>} tags | |
| 80 * @return {boolean} True if this object has one of the specified tags. | |
| 81 */ | |
| 82 remoting.Error.prototype.hasAnyTag = function(tags) { | |
|
Jamie
2015/03/13 17:56:17
I think you can omit this method and just use vara
John Williams
2015/03/13 19:30:29
Done.
| |
| 83 var thisTag = this.tag_; | |
| 84 return tags.some(function(tag) { | |
| 85 return thisTag == tag; | |
| 86 }); | |
| 87 }; | |
| 88 | |
| 89 /** | |
| 90 * @return {boolean} True if this object's tag is NONE, meaning this | |
| 91 * object represents the lack of an error. | |
| 92 */ | |
| 93 remoting.Error.prototype.isNone = function() { | |
| 94 return this.hasTag(remoting.Error.Tag.NONE); | |
| 95 }; | |
| 96 | |
| 97 /** | |
| 98 * Convenience method for creating the second most common error type. | |
| 99 * @return {!remoting.Error} | |
| 100 */ | |
| 101 remoting.Error.none = function() { | |
| 102 return new remoting.Error(remoting.Error.Tag.NONE); | |
| 103 }; | |
| 104 | |
| 105 /** | |
| 106 * Convenience method for creating the most common error type. | |
| 107 * @return {!remoting.Error} | |
| 108 */ | |
| 109 remoting.Error.unexpected = function() { | |
| 110 return new remoting.Error(remoting.Error.Tag.UNEXPECTED); | |
| 111 }; | |
| 112 | |
| 113 /** | |
| 36 * @enum {string} All error messages from messages.json | 114 * @enum {string} All error messages from messages.json |
| 37 */ | 115 */ |
| 38 remoting.Error.Tag = { | 116 remoting.Error.Tag = { |
| 39 NONE: '', | 117 NONE: '', |
| 40 | 118 |
| 41 // Used to signify that an operation was cancelled by the user. This should | 119 // Used to signify that an operation was cancelled by the user. This should |
| 42 // not normally cause the error text to be shown to the user, so the | 120 // not normally cause the error text to be shown to the user, so the |
| 43 // i18n-content prefix is not needed in this case. | 121 // i18n-content prefix is not needed in this case. |
| 44 CANCELLED: '__CANCELLED__', | 122 CANCELLED: '__CANCELLED__', |
| 45 | 123 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 63 // TODO(garykac): Move app-specific errors into separate location. | 141 // TODO(garykac): Move app-specific errors into separate location. |
| 64 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' | 142 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' |
| 65 }; | 143 }; |
| 66 | 144 |
| 67 // A whole bunch of semi-redundant constants, mostly to reduce to size | 145 // A whole bunch of semi-redundant constants, mostly to reduce to size |
| 68 // of the diff that introduced the remoting.Error class. | 146 // of the diff that introduced the remoting.Error class. |
| 69 // | 147 // |
| 70 // Please don't add any more constants here; just call the | 148 // Please don't add any more constants here; just call the |
| 71 // remoting.Error constructor directly | 149 // remoting.Error constructor directly |
| 72 | 150 |
| 73 /** @const */ | |
| 74 remoting.Error.NONE = new remoting.Error(remoting.Error.Tag.NONE); | |
| 75 | |
| 76 /** @const */ | |
| 77 remoting.Error.CANCELLED = | |
| 78 new remoting.Error(remoting.Error.Tag.CANCELLED); | |
| 79 | |
| 80 /** @const */ | |
| 81 remoting.Error.INVALID_ACCESS_CODE = | |
| 82 new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); | |
| 83 | |
| 84 /** @const */ | |
| 85 remoting.Error.MISSING_PLUGIN = | |
| 86 new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN); | |
| 87 | |
| 88 /** @const */ | |
| 89 remoting.Error.AUTHENTICATION_FAILED = | |
| 90 new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); | |
| 91 | |
| 92 /** @const */ | |
| 93 remoting.Error.HOST_IS_OFFLINE = | |
| 94 new remoting.Error(remoting.Error.Tag.HOST_IS_OFFLINE); | |
| 95 | |
| 96 /** @const */ | |
| 97 remoting.Error.INCOMPATIBLE_PROTOCOL = | |
| 98 new remoting.Error(remoting.Error.Tag.INCOMPATIBLE_PROTOCOL); | |
| 99 | |
| 100 /** @const */ | |
| 101 remoting.Error.BAD_PLUGIN_VERSION = | |
| 102 new remoting.Error(remoting.Error.Tag.BAD_PLUGIN_VERSION); | |
| 103 | |
| 104 /** @const */ | |
| 105 remoting.Error.NETWORK_FAILURE = | |
| 106 new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); | |
| 107 | |
| 108 /** @const */ | |
| 109 remoting.Error.HOST_OVERLOAD = | |
| 110 new remoting.Error(remoting.Error.Tag.HOST_OVERLOAD); | |
| 111 | |
| 112 /** @const */ | |
| 113 remoting.Error.UNEXPECTED = | |
| 114 new remoting.Error(remoting.Error.Tag.UNEXPECTED); | |
| 115 | |
| 116 /** @const */ | |
| 117 remoting.Error.SERVICE_UNAVAILABLE = | |
| 118 new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); | |
| 119 | |
| 120 /** @const */ | |
| 121 remoting.Error.NOT_AUTHENTICATED = | |
| 122 new remoting.Error(remoting.Error.Tag.NOT_AUTHENTICATED); | |
| 123 | |
| 124 /** @const */ | |
| 125 remoting.Error.NOT_FOUND = | |
| 126 new remoting.Error(remoting.Error.Tag.NOT_FOUND); | |
| 127 | |
| 128 /** @const */ | |
| 129 remoting.Error.INVALID_HOST_DOMAIN = | |
| 130 new remoting.Error(remoting.Error.Tag.INVALID_HOST_DOMAIN); | |
| 131 | |
| 132 /** @const */ | |
| 133 remoting.Error.P2P_FAILURE = | |
| 134 new remoting.Error(remoting.Error.Tag.P2P_FAILURE); | |
| 135 | |
| 136 /** @const */ | |
| 137 remoting.Error.REGISTRATION_FAILED = | |
| 138 new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); | |
| 139 | |
| 140 /** @const */ | |
| 141 remoting.Error.NOT_AUTHORIZED = | |
| 142 new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); | |
| 143 | |
| 144 /** @const */ | |
| 145 remoting.Error.APP_NOT_AUTHORIZED = | |
| 146 new remoting.Error(remoting.Error.Tag.APP_NOT_AUTHORIZED); | |
| 147 | |
| 148 /** | 151 /** |
| 149 * @param {number} httpStatus An HTTP status code. | 152 * @param {number} httpStatus An HTTP status code. |
| 150 * @return {!remoting.Error} The remoting.Error enum corresponding to the | 153 * @return {!remoting.Error} The remoting.Error enum corresponding to the |
| 151 * specified HTTP status code. | 154 * specified HTTP status code. |
| 152 */ | 155 */ |
| 153 remoting.Error.fromHttpStatus = function(httpStatus) { | 156 remoting.Error.fromHttpStatus = function(httpStatus) { |
| 154 if (httpStatus == 0) { | 157 if (httpStatus == 0) { |
| 155 return remoting.Error.NETWORK_FAILURE; | 158 return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); |
| 156 } else if (httpStatus >= 200 && httpStatus < 300) { | 159 } else if (httpStatus >= 200 && httpStatus < 300) { |
| 157 return remoting.Error.NONE; | 160 return remoting.Error.none(); |
| 158 } else if (httpStatus == 400 || httpStatus == 401) { | 161 } else if (httpStatus == 400 || httpStatus == 401) { |
| 159 return remoting.Error.AUTHENTICATION_FAILED; | 162 return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
| 160 } else if (httpStatus == 403) { | 163 } else if (httpStatus == 403) { |
| 161 return remoting.Error.NOT_AUTHORIZED; | 164 return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); |
| 162 } else if (httpStatus == 404) { | 165 } else if (httpStatus == 404) { |
| 163 return remoting.Error.NOT_FOUND; | 166 return new remoting.Error(remoting.Error.Tag.NOT_FOUND); |
| 164 } else if (httpStatus >= 500 && httpStatus < 600) { | 167 } else if (httpStatus >= 500 && httpStatus < 600) { |
| 165 return remoting.Error.SERVICE_UNAVAILABLE; | 168 return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); |
| 166 } else { | 169 } else { |
| 167 console.warn('Unexpected HTTP error code: ' + httpStatus); | 170 console.warn('Unexpected HTTP error code: ' + httpStatus); |
| 168 return remoting.Error.UNEXPECTED; | 171 return remoting.Error.unexpected(); |
| 169 } | 172 } |
| 170 }; | 173 }; |
| 171 | 174 |
| 172 /** | 175 /** |
| 173 * Create an error-handling function suitable for passing to a | 176 * Create an error-handling function suitable for passing to a |
| 174 * Promise's "catch" method. | 177 * Promise's "catch" method. |
| 175 * | 178 * |
| 176 * @param {function(!remoting.Error):void} onError | 179 * @param {function(!remoting.Error):void} onError |
| 177 * @return {function(*):void} | 180 * @return {function(*):void} |
| 178 */ | 181 */ |
| 179 remoting.Error.handler = function(onError) { | 182 remoting.Error.handler = function(onError) { |
| 180 return function(/** * */ error) { | 183 return function(/** * */ error) { |
| 181 if (error instanceof remoting.Error) { | 184 if (error instanceof remoting.Error) { |
| 182 onError(/** @type {!remoting.Error} */ (error)); | 185 onError(/** @type {!remoting.Error} */ (error)); |
| 183 } else { | 186 } else { |
| 184 console.error('Unexpected error: %o', error); | 187 console.error('Unexpected error: %o', error); |
| 185 onError(remoting.Error.UNEXPECTED); | 188 onError(remoting.Error.unexpected()); |
| 186 } | 189 } |
| 187 }; | 190 }; |
| 188 }; | 191 }; |
| OLD | NEW |