diff options
author | Maximilian Krambach <[email protected]> | 2018-04-25 13:59:36 +0000 |
---|---|---|
committer | Maximilian Krambach <[email protected]> | 2018-04-25 13:59:36 +0000 |
commit | 1fb310cabe578625f96fce5d84ff6f0092c08d24 (patch) | |
tree | 7d86ba979e8a4c39fd75ea2f727cce8c169d4e08 /lang/js/src/Errors.js | |
parent | js: reactivate timeout on connection (diff) | |
download | gpgme-1fb310cabe578625f96fce5d84ff6f0092c08d24.tar.gz gpgme-1fb310cabe578625f96fce5d84ff6f0092c08d24.zip |
js: Configuration and Error handling
--
* gpgmejs_openpgpjs
- unsuported values with no negative consequences can now reject,
warn or be ignored, according to config.unconsidered_params
- cleanup of unsupported/supported parameters and TODOS
* A src/index.js init() now accepts a configuration object
* Errors will now be derived from Error, offering more info and a
stacktrace.
* Fixed Connection.post() timeout triggering on wrong cases
* Added comments in permittedOperations.js, which gpgme interactions
are still unimplemented and should be added next
Diffstat (limited to 'lang/js/src/Errors.js')
-rw-r--r-- | lang/js/src/Errors.js | 208 |
1 files changed, 117 insertions, 91 deletions
diff --git a/lang/js/src/Errors.js b/lang/js/src/Errors.js index 04b13e10..2f53aa89 100644 --- a/lang/js/src/Errors.js +++ b/lang/js/src/Errors.js @@ -18,99 +18,125 @@ * SPDX-License-Identifier: LGPL-2.1+ */ +const err_list = { + // Connection + 'CONN_NO_CONNECT': { + msg:'Connection with the nativeMessaging host could not be' + + ' established.', + type: 'error' + }, + 'CONN_DISCONNECTED': { + msg:'Connection with the nativeMessaging host was lost.', + type: 'error' + }, + 'CONN_EMPTY_GPG_ANSWER':{ + msg: 'The nativeMessaging answer was empty.', + type: 'error' + }, + 'CONN_TIMEOUT': { + msg: 'A connection timeout was exceeded.', + type: 'error' + }, + 'CONN_UNEXPECTED_ANSWER': { + msg: 'The answer from gnupg was not as expected.', + type: 'error' + }, + 'CONN_ALREADY_CONNECTED':{ + msg: 'A connection was already established.', + type: 'warning' + }, + // Message/Data + 'MSG_INCOMPLETE': { + msg: 'The Message did not match the minimum requirements for' + + ' the interaction.', + type: 'error' + }, + 'MSG_EMPTY' : { + msg: 'The Message is empty.', + type: 'error' + }, + 'MSG_OP_PENDING': { + msg: 'There is no operation specified yet. The parameter cannot' + + ' be set', + type: 'warning' + }, + 'MSG_WRONG_OP': { + msg: 'The operation requested could not be found', + type: 'warning' + }, + 'MSG_NO_KEYS' : { + msg: 'There were no valid keys provided.', + type: 'warning' + }, + 'MSG_NOT_A_FPR': { + msg: 'The String is not an accepted fingerprint', + type: 'warning' + }, + 'KEY_INVALID': { + msg:'Key object is invalid', + type: 'error' + }, + // generic + 'PARAM_WRONG':{ + msg: 'invalid parameter was found', + type: 'error' + }, + 'PARAM_IGNORED': { + msg: 'An parameter was set that has no effect in gpgmejs', + type: 'warning' + }, + 'NOT_IMPLEMENTED': { + msg: 'A openpgpjs parameter was submitted that is not implemented', + type: 'error' + }, + 'NOT_YET_IMPLEMENTED': { + msg: 'Support of this is probable, but it is not implemented yet', + type: 'error' + }, + 'GENERIC_ERROR': { + msg: 'Unspecified error', + type: 'error' + } +}; + /** - * Checks the given error code and returns some information about it's meaning - * @param {String} code The error code - * @returns {Object} An object containing string properties code and msg - * TODO: error-like objects with the code 'GNUPG_ERROR' are errors sent - * directly by gnupg as answer in Connection.post() + * Checks the given error code and returns an error object with some + * information about meaning and origin + * @param {*} code Error code. Should be in err_list or 'GNUPG_ERROR' + * @param {*} info Error message passed through if code is 'GNUPG_ERROR' */ -export function GPGMEJS_Error(code = 'GENERIC_ERROR'){ - if (!typeof(code) === 'string'){ - code = 'GENERIC_ERROR'; - } - let errors = { //TODO: someplace else - // Connection - 'CONN_NO_CONNECT': { - msg:'Connection with the nativeMessaging host could not be' - + ' established.', - type: 'error' - }, - 'CONN_EMPTY_GPG_ANSWER':{ - msg: 'The nativeMessaging answer was empty.', - type: 'error' - }, - 'CONN_TIMEOUT': { - msg: 'A connection timeout was exceeded.', - type: 'error' - }, - 'CONN_UNEXPECTED_ANSWER': { - msg: 'The answer from gnupg was not as expected.', - type: 'error' - }, - 'CONN_ALREADY_CONNECTED':{ - msg: 'A connection was already established.', - type: 'warn' - }, - // Message/Data - 'MSG_INCOMPLETE': { - msg: 'The Message did not match the minimum requirements for' - + ' the interaction.', - type: 'error' - }, - 'MSG_EMPTY' : { - msg: 'The Message is empty.', - type: 'error' - }, - 'MSG_OP_PENDING': { - msg: 'There is no operation specified yet. The parameter cannot' - + ' be set', - type: 'warning' - }, - 'MSG_WRONG_OP': { - msg: 'The operation requested could not be found', - type: 'warning' - }, - 'MSG_NO_KEYS' : { - msg: 'There were no valid keys provided.', - type: 'warn' - }, - 'MSG_NOT_A_FPR': { - msg: 'The String is not an accepted fingerprint', - type: 'warn' - }, - - // generic - 'PARAM_WRONG':{ - msg: 'invalid parameter was found', - type: 'error' - }, - 'NOT_IMPLEMENTED': { - msg: 'A openpgpjs parameter was submitted that is not implemented', - type: 'error' - }, - 'NOT_YET_IMPLEMENTED': { - msg: 'Support of this is probable, but it is not implemented yet', - type: 'error' - }, - 'GENERIC_ERROR': { - msg: 'Unspecified error', - type: 'error' - }, - } - if (code === 'TODO'){ - alert('TODO_Error!'); +export function gpgme_error(code = 'GENERIC_ERROR', info){ + if (err_list.hasOwnProperty(code)){ + if (err_list[code].type === 'error'){ + return new GPGME_Error(code); } - if (errors.hasOwnProperty(code)){ - code = 'GENERIC_ERROR'; + if (err_list[code].type === 'warning'){ + console.log(new GPGME_Error(code)); } - if (errors.type === 'error'){ - return {code: 'code', - msg: errors[code].msg - }; - } - if (errors.type === 'warning'){ - console.log(code + ': ' + error[code].msg); - } - return undefined; + return null; + } else if (code === 'GNUPG_ERROR'){ + return new GPGME_Error(code, info.msg); + } + else { + return new GPGME_Error('GENERIC_ERROR'); + } } + +class GPGME_Error extends Error{ + constructor(code, msg=''){ + if (code === 'GNUPG_ERROR' && typeof(msg) === 'string'){ + super(msg); + } else if (err_list.hasOwnProperty(code)){ + super(err_list[code].msg); + } else { + super(err_list['GENERIC_ERROR'].msg); + } + this.code = code || 'GENERIC_ERROR'; + } + set code(value){ + this._code = value; + } + get code(){ + return this._code; + } +}
\ No newline at end of file |