From 0549b2c24b5fd80d97666c21b62401980872417a Mon Sep 17 00:00:00 2001 From: "pius.lee" Date: Tue, 10 Mar 2015 20:23:41 +0900 Subject: [PATCH] [Utils] Add createException, createError Factory function to Utils Change-Id: I0cad2180d144af3711e5a03fb79185bc31aa3d1b Signed-off-by: pius.lee --- src/utils/utils_api.js | 176 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) diff --git a/src/utils/utils_api.js b/src/utils/utils_api.js index 87f189e9..01e0e5ee 100644 --- a/src/utils/utils_api.js +++ b/src/utils/utils_api.js @@ -1077,7 +1077,183 @@ var NativeBridge = (function (extension, debug) { return new Bridge; }); +// WebAPIException and WebAPIError +function __isObject(object) { + return object instanceof Object; +} + +function __isUndefined(object) { + return object === void 0; +} + +function __isNumber(object) { + return typeof object === 'number'; +} + +// WARNING! This list should be in sync with the equivalent enum +// located at tizen.h. Remember to update tizen.h if you change +// something here. +var errors = { + NO_ERROR: 0, + UNKNOWN_ERR: -1, + + INDEX_SIZE_ERR: 1, + DOMSTRING_SIZE_ERR: 2, + HIERARCHY_REQUEST_ERR: 3, + WRONG_DOCUMENT_ERR: 4, + INVALID_CHARACTER_ERR: 5, + NO_DATA_ALLOWED_ERR: 6, + NO_MODIFICATION_ALLOWED_ERR: 7, + NOT_FOUND_ERR: 8, + NOT_SUPPORTED_ERR: 9, + INUSE_ATTRIBUTE_ERR: 10, + INVALID_STATE_ERR: 11, + SYNTAX_ERR: 12, + INVALID_MODIFICATION_ERR: 13, + NAMESPACE_ERR: 14, + INVALID_ACCESS_ERR: 15, + VALIDATION_ERR: 16, + TYPE_MISMATCH_ERR: 17, + SECURITY_ERR: 18, + NETWORK_ERR: 19, + ABORT_ERR: 20, + URL_MISMATCH_ERR: 21, + QUOTA_EXCEEDED_ERR: 22, + TIMEOUT_ERR: 23, + INVALID_NODE_TYPE_ERR: 24, + DATA_CLONE_ERR: 25, + + // Error codes for these errors are not really defined anywhere. + INVALID_VALUES_ERR: 100, + IO_ERR: 101, + PERMISSION_DENIED_ERR: 102, + SERVICE_NOT_AVAILABLE_ERR: 103, + DATABASE_ERR: 104 +}; + +var code_to_name = {}; +code_to_name[errors['NO_ERROR']] = 'NoError'; +code_to_name[errors['UNKNOWN_ERR']] = 'UnknownError'; +code_to_name[errors['INDEX_SIZE_ERR']] = 'IndexSizeError'; +code_to_name[errors['DOMSTRING_SIZE_ERR']] = 'DOMStringSizeError'; +code_to_name[errors['HIERARCHY_REQUEST_ERR']] = 'HierarchyRequestError'; +code_to_name[errors['WRONG_DOCUMENT_ERR']] = 'WrongDocumentError'; +code_to_name[errors['INVALID_CHARACTER_ERR']] = 'InvalidCharacterError'; +code_to_name[errors['NO_DATA_ALLOWED_ERR']] = 'NoDataAllowedError'; +code_to_name[errors['NO_MODIFICATION_ALLOWED_ERR']] = 'NoModificationAllowedError'; +code_to_name[errors['NOT_FOUND_ERR']] = 'NotFoundError'; +code_to_name[errors['NOT_SUPPORTED_ERR']] = 'NotSupportedError'; +code_to_name[errors['INUSE_ATTRIBUTE_ERR']] = 'InuseAttributeError'; +code_to_name[errors['INVALID_STATE_ERR']] = 'InvalidStateError'; +code_to_name[errors['SYNTAX_ERR']] = 'SyntaxError'; +code_to_name[errors['INVALID_MODIFICATION_ERR']] = 'InvalidModificationError'; +code_to_name[errors['NAMESPACE_ERR']] = 'NamespaceError'; +code_to_name[errors['INVALID_ACCESS_ERR']] = 'InvalidAccessError'; +code_to_name[errors['VALIDATION_ERR']] = 'ValidationError'; +code_to_name[errors['TYPE_MISMATCH_ERR']] = 'TypeMismatchError'; +code_to_name[errors['SECURITY_ERR']] = 'SecurityError'; +code_to_name[errors['NETWORK_ERR']] = 'NetworkError'; +code_to_name[errors['ABORT_ERR']] = 'AbortError'; +code_to_name[errors['URL_MISMATCH_ERR']] = 'URLMismatchError'; +code_to_name[errors['QUOTA_EXCEEDED_ERR']] = 'QuotaExceededError'; +code_to_name[errors['TIMEOUT_ERR']] = 'TimeoutError'; +code_to_name[errors['INVALID_NODE_TYPE_ERR']] = 'InvalidNodeTypeError'; +code_to_name[errors['DATA_CLONE_ERR']] = 'DataCloneError'; + +code_to_name[errors['INVALID_VALUES_ERR']] = 'InvalidValuesError'; +code_to_name[errors['IO_ERR']] = 'IOError'; +code_to_name[errors['PERMISSION_DENIED_ERR']] = 'PermissionDeniedError'; +code_to_name[errors['SERVICE_NOT_AVAILABLE_ERR']] = 'ServiceNotAvailableError'; +code_to_name[errors['DATABASE_ERR']] = 'DatabaseError'; + +var name_to_code = {}; +Object.keys(errors).forEach(function(key) { + name_to_code[code_to_name[errors[key]]] = errors[key]; +}); + + +/** + * Generic exception interface. + * + * @param {number} code 16-bit error code. + * @param {string} message An error message that describes the details of an encountered error. + * @param {string} name An error type. + */ +var WebAPIException = function(code, message, name) { + var code_ = 0; + var name_ = code_to_name[code]; + var message_ = 'Unknown error'; + + switch (arguments.length) { + case 1: + var error = arguments[0]; + if (__isObject(error)) { + code_ = error.code; + name_ = error.name; + message_ = error.message; + if (__isUndefined(code_) && !__isUndefined(name_)) + code_ = name_to_code[name_]; + if (__isUndefined(name_) && !__isUndefined(code_)) + name_ = code_to_name[code_]; + } else if (__isNumber(error)) { + // backward compatibility with crosswalk implementation + code_ = error; + name_ = code_to_name[code]; + message_ = name_; + } + break; + case 2: + if (__isNumber(arguments[0])) { + code_ = arguments[0]; + if (!__isUndefined(code_to_name[code_])) { + name_ = code_to_name[code_]; + } + } else { + name_ = String(arguments[0]); + if (!__isUndefined(name_to_code[name_])) { + code_ = name_to_code[name_]; + } + } + message_ = String(arguments[1]); + break; + case 3: + // backward compatibility with crosswalk implementation + code_ = Number(arguments[0]); + message_ = String(arguments[1]); + name_ = String(arguments[2]); + break; + default: + return; + } + + // attributes + var attributes = { + code: {value: code_, writable: false, enumerable: true}, + name: {value: name_, writable: false, enumerable: true}, + message: {value: message_, writable: false, enumerable: true} + }; + + for (var error in errors) { + attributes[error] = { value: errors[error], writable: false, enumerable: true} + } + + Object.defineProperties(this, attributes); + + this.constructor.prototype.__proto__ = Error.prototype; + Error.captureStackTrace(this, this.constructor); +}; + +WebAPIException.prototype.toString = function() { + return this.name + ': ' + this.message; +}; + +function getExceptionClass() { + return WebAPIException; +} + +Utils.prototype.getExceptionClass = getExceptionClass; +Utils.prototype.getErrorClass = getExceptionClass; Utils.prototype.type = _type; Utils.prototype.converter = _converter; -- 2.34.1