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