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
3 // found in the LICENSE file.
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
11 * @enum {string} All error messages from messages.json
16 // Used to signify that an operation was cancelled by the user. This should
17 // not normally cause the error text to be shown to the user, so the
18 // i18n-content prefix is not needed in this case.
19 CANCELLED: '__CANCELLED__',
21 INVALID_ACCESS_CODE: /*i18n-content*/'ERROR_INVALID_ACCESS_CODE',
22 MISSING_PLUGIN: /*i18n-content*/'ERROR_MISSING_PLUGIN',
23 AUTHENTICATION_FAILED: /*i18n-content*/'ERROR_AUTHENTICATION_FAILED',
24 HOST_IS_OFFLINE: /*i18n-content*/'ERROR_HOST_IS_OFFLINE',
25 INCOMPATIBLE_PROTOCOL: /*i18n-content*/'ERROR_INCOMPATIBLE_PROTOCOL',
26 BAD_PLUGIN_VERSION: /*i18n-content*/'ERROR_BAD_PLUGIN_VERSION',
27 NETWORK_FAILURE: /*i18n-content*/'ERROR_NETWORK_FAILURE',
28 HOST_OVERLOAD: /*i18n-content*/'ERROR_HOST_OVERLOAD',
29 UNEXPECTED: /*i18n-content*/'ERROR_UNEXPECTED',
30 SERVICE_UNAVAILABLE: /*i18n-content*/'ERROR_SERVICE_UNAVAILABLE',
31 NOT_AUTHENTICATED: /*i18n-content*/'ERROR_NOT_AUTHENTICATED',
32 INVALID_HOST_DOMAIN: /*i18n-content*/'ERROR_INVALID_HOST_DOMAIN',
33 P2P_FAILURE: /*i18n-content*/'ERROR_P2P_FAILURE',
34 REGISTRATION_FAILED: /*i18n-content*/'ERROR_HOST_REGISTRATION_FAILED',
35 NOT_AUTHORIZED: /*i18n-content*/'ERROR_NOT_AUTHORIZED',
37 // TODO(garykac): Move app-specific errors into separate location.
38 APP_NOT_AUTHORIZED: /*i18n-content*/'ERROR_APP_NOT_AUTHORIZED'
42 * @param {number} httpStatus An HTTP status code.
43 * @return {remoting.Error} The remoting.Error enum corresponding to the
44 * specified HTTP status code.
46 remoting.Error.fromHttpStatus = function(httpStatus) {
47 if (httpStatus == 0) {
48 return remoting.Error.NETWORK_FAILURE;
49 } else if (httpStatus >= 200 && httpStatus < 300) {
50 return remoting.Error.NONE;
51 } else if (httpStatus == 400 || httpStatus == 401) {
52 return remoting.Error.AUTHENTICATION_FAILED;
53 } else if (httpStatus >= 500 && httpStatus < 600) {
54 return remoting.Error.SERVICE_UNAVAILABLE;
56 console.warn('Unexpected HTTP error code: ' + httpStatus);
57 // Return AUTHENTICATION_FAILED by default, so that the user can try to
58 // recover from an unexpected failure by signing in again.
59 // TODO(jamiewalch): Return UNEXPECTED here and let calling code treat that
60 // as "sign-in required" if necessary.
61 return remoting.Error.AUTHENTICATION_FAILED;
66 * Create an error-handling function suitable for passing to a
67 * Promise's "catch" method.
69 * @param {function(remoting.Error):void} onError
70 * @return {function(*):void}
72 remoting.Error.handler = function(onError) {
73 return function(/** * */ error) {
74 if (typeof error == 'string') {
75 onError(/** @type {remoting.Error} */ (error));
77 console.error('Unexpected error: %o', error);
78 onError(remoting.Error.UNEXPECTED);
84 // * @param {(!Promise<T>|
85 // * function(function(T):void,function(remoting.Error):void))} arg
89 // remoting.Promise = function(arg) {
91 // if (typeof arg == 'function') {
92 // promise = new Promise(arg);
98 // this.promise = promise;
102 // * @param {?function(T)} onResolve
103 // * @param {?function(remoting.Error)=} opt_onReject
104 // * @return {!remoting.Promise}
106 // remoting.Promise.prototype.then = function(onResolve, opt_onReject) {
107 // return new remoting.Promise(this.promise.then(
109 // opt_onReject && function(/** * */ error) {
110 // if (typeof error == 'string') {
111 // opt_onReject(/** @type {remoting.Error} */ (error));
113 // console.error('Unexpected error: %o', error);
114 // opt_onReject(remoting.Error.UNEXPECTED);
120 // * @param {?function(remoting.Error)} onReject
121 // * @return {!remoting.Promise<T>}
123 // remoting.Promise.prototype.catch = function(onReject) {
124 // return this.then(null, onReject);