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 /** @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 * Checks the type of an error. This method is more typesafe than | |
| 29 * using ==. | |
|
Jamie
2015/03/12 21:40:11
If you make tag_ private, there's no need for the
John Williams
2015/03/13 00:26:35
Done.
| |
| 30 * @param {remoting.Error.Tag} tag | |
| 31 * @return {boolean} True if this object has the specified tag. | |
| 32 */ | |
| 33 remoting.Error.prototype.hasTag = function(tag) { | |
| 34 return this.tag == tag; | |
| 35 }; | |
| 36 | |
| 37 | |
| 38 /** | |
| 28 * @return {boolean} True if this object represents an error | 39 * @return {boolean} True if this object represents an error |
| 29 * condition. | 40 * condition. |
| 30 */ | 41 */ |
| 31 remoting.Error.prototype.isError = function() { | 42 remoting.Error.prototype.isError = function() { |
|
Jamie
2015/03/12 21:40:11
I think we should remove this method. It's not wid
John Williams
2015/03/13 00:26:35
I changed it to isNone to make it more clear, but
| |
| 32 return this.tag != remoting.Error.Tag.NONE; | 43 return !this.hasTag(remoting.Error.Tag.NONE); |
| 33 }; | 44 }; |
| 34 | 45 |
| 35 /** | 46 /** |
| 47 * Convenience method for creating the second most common error type. | |
| 48 * @return {!remoting.Error} | |
| 49 */ | |
| 50 remoting.Error.none = function() { | |
| 51 return new remoting.Error(remoting.Error.Tag.NONE); | |
| 52 }; | |
| 53 | |
| 54 /** | |
| 55 * Convenience method for creating the most common error type. | |
| 56 * @return {!remoting.Error} | |
| 57 */ | |
| 58 remoting.Error.unexpected = function() { | |
| 59 return new remoting.Error(remoting.Error.Tag.UNEXPECTED); | |
| 60 }; | |
| 61 | |
| 62 /** | |
| 36 * @enum {string} All error messages from messages.json | 63 * @enum {string} All error messages from messages.json |
| 37 */ | 64 */ |
| 38 remoting.Error.Tag = { | 65 remoting.Error.Tag = { |
| 39 NONE: '', | 66 NONE: '', |
| 40 | 67 |
| 41 // Used to signify that an operation was cancelled by the user. This should | 68 // 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 | 69 // not normally cause the error text to be shown to the user, so the |
| 43 // i18n-content prefix is not needed in this case. | 70 // i18n-content prefix is not needed in this case. |
| 44 CANCELLED: '__CANCELLED__', | 71 CANCELLED: '__CANCELLED__', |
| 45 | 72 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 63 // TODO(garykac): Move app-specific errors into separate location. | 90 // TODO(garykac): Move app-specific errors into separate location. |
| 64 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' | 91 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED' |
| 65 }; | 92 }; |
| 66 | 93 |
| 67 // A whole bunch of semi-redundant constants, mostly to reduce to size | 94 // A whole bunch of semi-redundant constants, mostly to reduce to size |
| 68 // of the diff that introduced the remoting.Error class. | 95 // of the diff that introduced the remoting.Error class. |
| 69 // | 96 // |
| 70 // Please don't add any more constants here; just call the | 97 // Please don't add any more constants here; just call the |
| 71 // remoting.Error constructor directly | 98 // remoting.Error constructor directly |
| 72 | 99 |
| 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 /** | 100 /** |
| 149 * @param {number} httpStatus An HTTP status code. | 101 * @param {number} httpStatus An HTTP status code. |
| 150 * @return {!remoting.Error} The remoting.Error enum corresponding to the | 102 * @return {!remoting.Error} The remoting.Error enum corresponding to the |
| 151 * specified HTTP status code. | 103 * specified HTTP status code. |
| 152 */ | 104 */ |
| 153 remoting.Error.fromHttpStatus = function(httpStatus) { | 105 remoting.Error.fromHttpStatus = function(httpStatus) { |
| 154 if (httpStatus == 0) { | 106 if (httpStatus == 0) { |
| 155 return remoting.Error.NETWORK_FAILURE; | 107 return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); |
| 156 } else if (httpStatus >= 200 && httpStatus < 300) { | 108 } else if (httpStatus >= 200 && httpStatus < 300) { |
| 157 return remoting.Error.NONE; | 109 return remoting.Error.none(); |
| 158 } else if (httpStatus == 400 || httpStatus == 401) { | 110 } else if (httpStatus == 400 || httpStatus == 401) { |
| 159 return remoting.Error.AUTHENTICATION_FAILED; | 111 return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
| 160 } else if (httpStatus == 403) { | 112 } else if (httpStatus == 403) { |
| 161 return remoting.Error.NOT_AUTHORIZED; | 113 return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); |
| 162 } else if (httpStatus == 404) { | 114 } else if (httpStatus == 404) { |
| 163 return remoting.Error.NOT_FOUND; | 115 return new remoting.Error(remoting.Error.Tag.NOT_FOUND); |
| 164 } else if (httpStatus >= 500 && httpStatus < 600) { | 116 } else if (httpStatus >= 500 && httpStatus < 600) { |
| 165 return remoting.Error.SERVICE_UNAVAILABLE; | 117 return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); |
| 166 } else { | 118 } else { |
| 167 console.warn('Unexpected HTTP error code: ' + httpStatus); | 119 console.warn('Unexpected HTTP error code: ' + httpStatus); |
| 168 return remoting.Error.UNEXPECTED; | 120 return remoting.Error.unexpected(); |
| 169 } | 121 } |
| 170 }; | 122 }; |
| 171 | 123 |
| 172 /** | 124 /** |
| 173 * Create an error-handling function suitable for passing to a | 125 * Create an error-handling function suitable for passing to a |
| 174 * Promise's "catch" method. | 126 * Promise's "catch" method. |
| 175 * | 127 * |
| 176 * @param {function(!remoting.Error):void} onError | 128 * @param {function(!remoting.Error):void} onError |
| 177 * @return {function(*):void} | 129 * @return {function(*):void} |
| 178 */ | 130 */ |
| 179 remoting.Error.handler = function(onError) { | 131 remoting.Error.handler = function(onError) { |
| 180 return function(/** * */ error) { | 132 return function(/** * */ error) { |
| 181 if (error instanceof remoting.Error) { | 133 if (error instanceof remoting.Error) { |
| 182 onError(/** @type {!remoting.Error} */ (error)); | 134 onError(/** @type {!remoting.Error} */ (error)); |
| 183 } else { | 135 } else { |
| 184 console.error('Unexpected error: %o', error); | 136 console.error('Unexpected error: %o', error); |
| 185 onError(remoting.Error.UNEXPECTED); | 137 onError(remoting.Error.unexpected()); |
| 186 } | 138 } |
| 187 }; | 139 }; |
| 188 }; | 140 }; |
| OLD | NEW |