Chromium Code Reviews| Index: remoting/webapp/crd/js/error.js |
| diff --git a/remoting/webapp/crd/js/error.js b/remoting/webapp/crd/js/error.js |
| index c1da1dc6298be5a583a5dbca3892196e5b342a08..5695c04f68a7901498e854018104e1af9c773353 100644 |
| --- a/remoting/webapp/crd/js/error.js |
| +++ b/remoting/webapp/crd/js/error.js |
| @@ -17,19 +17,97 @@ var remoting = remoting || {}; |
| * @param {string=} opt_message |
| */ |
| remoting.Error = function(tag, opt_message) { |
| - /** @const {remoting.Error.Tag} */ |
| - this.tag = tag; |
| + /** @private @const {remoting.Error.Tag} */ |
| + this.tag_ = tag; |
| /** @const {?string} */ |
| this.message = opt_message || null; |
| }; |
| /** |
| - * @return {boolean} True if this object represents an error |
| - * condition. |
| + * @return {string} |
| */ |
| -remoting.Error.prototype.isError = function() { |
| - return this.tag != remoting.Error.Tag.NONE; |
| +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
|
| + return this.tag_; |
| +}; |
| + |
| +/** |
| + * @return {string} |
| + */ |
| +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.
|
| + // Directory service should be updated if a new string is added here as |
| + // otherwise the error code will be ignored (i.e. recorded as 0 instead). |
| + switch (this.tag_) { |
| + case remoting.Error.Tag.NONE: |
| + return 'none'; |
| + case remoting.Error.Tag.INVALID_ACCESS_CODE: |
| + return 'invalid-access-code'; |
| + case remoting.Error.Tag.MISSING_PLUGIN: |
| + return 'missing_plugin'; |
| + case remoting.Error.Tag.AUTHENTICATION_FAILED: |
| + return 'authentication-failed'; |
| + case remoting.Error.Tag.HOST_IS_OFFLINE: |
| + return 'host-is-offline'; |
| + case remoting.Error.Tag.INCOMPATIBLE_PROTOCOL: |
| + return 'incompatible-protocol'; |
| + case remoting.Error.Tag.BAD_PLUGIN_VERSION: |
| + return 'bad-plugin-version'; |
| + case remoting.Error.Tag.NETWORK_FAILURE: |
| + return 'network-failure'; |
| + case remoting.Error.Tag.HOST_OVERLOAD: |
| + return 'host-overload'; |
| + case remoting.Error.Tag.P2P_FAILURE: |
| + return 'p2p-failure'; |
| + case remoting.Error.Tag.UNEXPECTED: |
| + return 'unexpected'; |
| + default: |
| + return 'unknown-' + this.tag_; |
| + } |
| +}; |
| + |
| +/** |
| + * Checks the type of an error. |
| + * @param {remoting.Error.Tag} tag |
| + * @return {boolean} True if this object has the specified tag. |
| + */ |
| +remoting.Error.prototype.hasTag = function(tag) { |
| + return this.tag_ == tag; |
| +}; |
| + |
| +/** |
| + * Checks the type of an error. |
| + * @param {!Array<remoting.Error.Tag>} tags |
| + * @return {boolean} True if this object has one of the specified tags. |
| + */ |
| +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.
|
| + var thisTag = this.tag_; |
| + return tags.some(function(tag) { |
| + return thisTag == tag; |
| + }); |
| +}; |
| + |
| +/** |
| + * @return {boolean} True if this object's tag is NONE, meaning this |
| + * object represents the lack of an error. |
| + */ |
| +remoting.Error.prototype.isNone = function() { |
| + return this.hasTag(remoting.Error.Tag.NONE); |
| +}; |
| + |
| +/** |
| + * Convenience method for creating the second most common error type. |
| + * @return {!remoting.Error} |
| + */ |
| +remoting.Error.none = function() { |
| + return new remoting.Error(remoting.Error.Tag.NONE); |
| +}; |
| + |
| +/** |
| + * Convenience method for creating the most common error type. |
| + * @return {!remoting.Error} |
| + */ |
| +remoting.Error.unexpected = function() { |
| + return new remoting.Error(remoting.Error.Tag.UNEXPECTED); |
| }; |
| /** |
| @@ -70,81 +148,6 @@ remoting.Error.Tag = { |
| // Please don't add any more constants here; just call the |
| // remoting.Error constructor directly |
| -/** @const */ |
| -remoting.Error.NONE = new remoting.Error(remoting.Error.Tag.NONE); |
| - |
| -/** @const */ |
| -remoting.Error.CANCELLED = |
| - new remoting.Error(remoting.Error.Tag.CANCELLED); |
| - |
| -/** @const */ |
| -remoting.Error.INVALID_ACCESS_CODE = |
| - new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); |
| - |
| -/** @const */ |
| -remoting.Error.MISSING_PLUGIN = |
| - new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN); |
| - |
| -/** @const */ |
| -remoting.Error.AUTHENTICATION_FAILED = |
| - new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
| - |
| -/** @const */ |
| -remoting.Error.HOST_IS_OFFLINE = |
| - new remoting.Error(remoting.Error.Tag.HOST_IS_OFFLINE); |
| - |
| -/** @const */ |
| -remoting.Error.INCOMPATIBLE_PROTOCOL = |
| - new remoting.Error(remoting.Error.Tag.INCOMPATIBLE_PROTOCOL); |
| - |
| -/** @const */ |
| -remoting.Error.BAD_PLUGIN_VERSION = |
| - new remoting.Error(remoting.Error.Tag.BAD_PLUGIN_VERSION); |
| - |
| -/** @const */ |
| -remoting.Error.NETWORK_FAILURE = |
| - new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); |
| - |
| -/** @const */ |
| -remoting.Error.HOST_OVERLOAD = |
| - new remoting.Error(remoting.Error.Tag.HOST_OVERLOAD); |
| - |
| -/** @const */ |
| -remoting.Error.UNEXPECTED = |
| - new remoting.Error(remoting.Error.Tag.UNEXPECTED); |
| - |
| -/** @const */ |
| -remoting.Error.SERVICE_UNAVAILABLE = |
| - new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); |
| - |
| -/** @const */ |
| -remoting.Error.NOT_AUTHENTICATED = |
| - new remoting.Error(remoting.Error.Tag.NOT_AUTHENTICATED); |
| - |
| -/** @const */ |
| -remoting.Error.NOT_FOUND = |
| - new remoting.Error(remoting.Error.Tag.NOT_FOUND); |
| - |
| -/** @const */ |
| -remoting.Error.INVALID_HOST_DOMAIN = |
| - new remoting.Error(remoting.Error.Tag.INVALID_HOST_DOMAIN); |
| - |
| -/** @const */ |
| -remoting.Error.P2P_FAILURE = |
| - new remoting.Error(remoting.Error.Tag.P2P_FAILURE); |
| - |
| -/** @const */ |
| -remoting.Error.REGISTRATION_FAILED = |
| - new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); |
| - |
| -/** @const */ |
| -remoting.Error.NOT_AUTHORIZED = |
| - new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); |
| - |
| -/** @const */ |
| -remoting.Error.APP_NOT_AUTHORIZED = |
| - new remoting.Error(remoting.Error.Tag.APP_NOT_AUTHORIZED); |
| - |
| /** |
| * @param {number} httpStatus An HTTP status code. |
| * @return {!remoting.Error} The remoting.Error enum corresponding to the |
| @@ -152,20 +155,20 @@ remoting.Error.APP_NOT_AUTHORIZED = |
| */ |
| remoting.Error.fromHttpStatus = function(httpStatus) { |
| if (httpStatus == 0) { |
| - return remoting.Error.NETWORK_FAILURE; |
| + return new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); |
| } else if (httpStatus >= 200 && httpStatus < 300) { |
| - return remoting.Error.NONE; |
| + return remoting.Error.none(); |
| } else if (httpStatus == 400 || httpStatus == 401) { |
| - return remoting.Error.AUTHENTICATION_FAILED; |
| + return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
| } else if (httpStatus == 403) { |
| - return remoting.Error.NOT_AUTHORIZED; |
| + return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); |
| } else if (httpStatus == 404) { |
| - return remoting.Error.NOT_FOUND; |
| + return new remoting.Error(remoting.Error.Tag.NOT_FOUND); |
| } else if (httpStatus >= 500 && httpStatus < 600) { |
| - return remoting.Error.SERVICE_UNAVAILABLE; |
| + return new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); |
| } else { |
| console.warn('Unexpected HTTP error code: ' + httpStatus); |
| - return remoting.Error.UNEXPECTED; |
| + return remoting.Error.unexpected(); |
| } |
| }; |
| @@ -182,7 +185,7 @@ remoting.Error.handler = function(onError) { |
| onError(/** @type {!remoting.Error} */ (error)); |
| } else { |
| console.error('Unexpected error: %o', error); |
| - onError(remoting.Error.UNEXPECTED); |
| + onError(remoting.Error.unexpected()); |
| } |
| }; |
| }; |