Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Unified Diff: remoting/webapp/crd/js/error.js

Issue 955283002: Converted remoting.Error from an enum to a class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/webapp/crd/js/dns_blackhole_checker.js ('k') | remoting/webapp/crd/js/host_controller.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..4686d4ecfbdab3fad3ae79170f118c607bbe8cba 100644
--- a/remoting/webapp/crd/js/error.js
+++ b/remoting/webapp/crd/js/error.js
@@ -25,11 +25,38 @@ remoting.Error = function(tag, opt_message) {
};
/**
+ * Checks the type of an error. This method is more typesafe than
+ * using ==.
+ * @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;
+};
+
+
+/**
* @return {boolean} True if this object represents an error
* condition.
*/
remoting.Error.prototype.isError = function() {
- return this.tag != remoting.Error.Tag.NONE;
+ 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 +97,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 +104,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 +134,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());
}
};
};
« no previous file with comments | « remoting/webapp/crd/js/dns_blackhole_checker.js ('k') | remoting/webapp/crd/js/host_controller.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698