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_detail |
| 18 */ | 18 */ |
| 19 remoting.Error = function(tag, opt_message) { | 19 remoting.Error = function(tag, opt_detail) { |
| 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.detail_ = opt_detail || null; |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * @return {boolean} True if this object represents an error | 28 * @override |
| 29 * condition. | |
| 30 */ | 29 */ |
| 31 remoting.Error.prototype.isError = function() { | 30 remoting.Error.prototype.toString = function() { |
| 32 return this.tag != remoting.Error.Tag.NONE; | 31 var result = this.tag_; |
| 32 if (this.detail_ != null) { | |
| 33 result += ' (' + this.detail_ + ')'; | |
| 34 } | |
| 35 return result; | |
| 33 }; | 36 }; |
| 34 | 37 |
| 35 /** | 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 * Checks the type of an error. | |
| 47 * @param {remoting.Error.Tag} tag | |
| 48 * @param {...remoting.Error.Tag} var_args | |
| 49 * @return {boolean} True if this object has one of the specified tags. | |
| 50 */ | |
| 51 remoting.Error.prototype.hasTag = function(tag, var_args) { | |
| 52 var thisTag = this.tag_; | |
| 53 return Array.prototype.some.call( | |
| 54 arguments, | |
| 55 function(/** remoting.Error.Tag */ tag) { | |
| 56 return thisTag == tag; | |
| 57 }); | |
|
Jamie
2015/03/13 20:26:01
This is fine, but you could eliminate thisTag with
| |
| 58 }; | |
| 59 | |
| 60 /** | |
| 61 * @return {boolean} True if this object's tag is NONE, meaning this | |
| 62 * object represents the lack of an error. | |
| 63 */ | |
| 64 remoting.Error.prototype.isNone = function() { | |
| 65 return this.hasTag(remoting.Error.Tag.NONE); | |
| 66 }; | |
| 67 | |
| 68 /** | |
| 69 * Convenience method for creating the second most common error type. | |
| 70 * @return {!remoting.Error} | |
| 71 */ | |
| 72 remoting.Error.none = function() { | |
| 73 return new remoting.Error(remoting.Error.Tag.NONE); | |
| 74 }; | |
| 75 | |
| 76 /** | |
| 77 * Convenience method for creating the most common error type. | |
| 78 * @return {!remoting.Error} | |
| 79 */ | |
| 80 remoting.Error.unexpected = function() { | |
| 81 return new remoting.Error(remoting.Error.Tag.UNEXPECTED); | |
| 82 }; | |
| 83 | |
| 84 /** | |
| 36 * @enum {string} All error messages from messages.json | 85 * @enum {string} All error messages from messages.json |
| 37 */ | 86 */ |
| 38 remoting.Error.Tag = { | 87 remoting.Error.Tag = { |
| 39 NONE: '', | 88 NONE: '', |
| 40 | 89 |
| 41 // Used to signify that an operation was cancelled by the user. This should | 90 // 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 | 91 // not normally cause the error text to be shown to the user, so the |
| 43 // i18n-content prefix is not needed in this case. | 92 // i18n-content prefix is not needed in this case. |
| 44 CANCELLED: '__CANCELLED__', | 93 CANCELLED: '__CANCELLED__', |
| 45 | 94 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 63 // TODO(garykac): Move app-specific errors into separate location. | 112 // TODO(garykac): Move app-specific errors into separate location. |
| 64 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' | 113 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' |
| 65 }; | 114 }; |
| 66 | 115 |
| 67 // A whole bunch of semi-redundant constants, mostly to reduce to size | 116 // A whole bunch of semi-redundant constants, mostly to reduce to size |
| 68 // of the diff that introduced the remoting.Error class. | 117 // of the diff that introduced the remoting.Error class. |
| 69 // | 118 // |
| 70 // Please don't add any more constants here; just call the | 119 // Please don't add any more constants here; just call the |
| 71 // remoting.Error constructor directly | 120 // remoting.Error constructor directly |
| 72 | 121 |
| 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 /** | 122 /** |
| 149 * @param {number} httpStatus An HTTP status code. | 123 * @param {number} httpStatus An HTTP status code. |
| 150 * @return {!remoting.Error} The remoting.Error enum corresponding to the | 124 * @return {!remoting.Error} The remoting.Error enum corresponding to the |
| 151 * specified HTTP status code. | 125 * specified HTTP status code. |
| 152 */ | 126 */ |
| 153 remoting.Error.fromHttpStatus = function(httpStatus) { | 127 remoting.Error.fromHttpStatus = function(httpStatus) { |
| 154 if (httpStatus == 0) { | 128 if (httpStatus == 0) { |
| 155 return remoting.Error.NETWORK_FAILURE; | 129 return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); |
| 156 } else if (httpStatus >= 200 && httpStatus < 300) { | 130 } else if (httpStatus >= 200 && httpStatus < 300) { |
| 157 return remoting.Error.NONE; | 131 return remoting.Error.none(); |
| 158 } else if (httpStatus == 400 || httpStatus == 401) { | 132 } else if (httpStatus == 400 || httpStatus == 401) { |
| 159 return remoting.Error.AUTHENTICATION_FAILED; | 133 return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
| 160 } else if (httpStatus == 403) { | 134 } else if (httpStatus == 403) { |
| 161 return remoting.Error.NOT_AUTHORIZED; | 135 return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); |
| 162 } else if (httpStatus == 404) { | 136 } else if (httpStatus == 404) { |
| 163 return remoting.Error.NOT_FOUND; | 137 return new remoting.Error(remoting.Error.Tag.NOT_FOUND); |
| 164 } else if (httpStatus >= 500 && httpStatus < 600) { | 138 } else if (httpStatus >= 500 && httpStatus < 600) { |
| 165 return remoting.Error.SERVICE_UNAVAILABLE; | 139 return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); |
| 166 } else { | 140 } else { |
| 167 console.warn('Unexpected HTTP error code: ' + httpStatus); | 141 console.warn('Unexpected HTTP error code: ' + httpStatus); |
| 168 return remoting.Error.UNEXPECTED; | 142 return remoting.Error.unexpected(); |
| 169 } | 143 } |
| 170 }; | 144 }; |
| 171 | 145 |
| 172 /** | 146 /** |
| 173 * Create an error-handling function suitable for passing to a | 147 * Create an error-handling function suitable for passing to a |
| 174 * Promise's "catch" method. | 148 * Promise's "catch" method. |
| 175 * | 149 * |
| 176 * @param {function(!remoting.Error):void} onError | 150 * @param {function(!remoting.Error):void} onError |
| 177 * @return {function(*):void} | 151 * @return {function(*):void} |
| 178 */ | 152 */ |
| 179 remoting.Error.handler = function(onError) { | 153 remoting.Error.handler = function(onError) { |
| 180 return function(/** * */ error) { | 154 return function(/** * */ error) { |
| 181 if (error instanceof remoting.Error) { | 155 if (error instanceof remoting.Error) { |
| 182 onError(/** @type {!remoting.Error} */ (error)); | 156 onError(/** @type {!remoting.Error} */ (error)); |
| 183 } else { | 157 } else { |
| 184 console.error('Unexpected error: %o', error); | 158 console.error('Unexpected error:', error); |
| 185 onError(remoting.Error.UNEXPECTED); | 159 onError(remoting.Error.unexpected()); |
| 186 } | 160 } |
| 187 }; | 161 }; |
| 188 }; | 162 }; |
| OLD | NEW |