Blame lang/js/src/Errors.js

Packit Service 30b792
/* gpgme.js - Javascript integration for gpgme
Packit Service 30b792
 * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
Packit Service 30b792
 *
Packit Service 30b792
 * This file is part of GPGME.
Packit Service 30b792
 *
Packit Service 30b792
 * GPGME is free software; you can redistribute it and/or modify it
Packit Service 30b792
 * under the terms of the GNU Lesser General Public License as
Packit Service 30b792
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 30b792
 * the License, or (at your option) any later version.
Packit Service 30b792
 *
Packit Service 30b792
 * GPGME is distributed in the hope that it will be useful, but
Packit Service 30b792
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 30b792
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 30b792
 * Lesser General Public License for more details.
Packit Service 30b792
 *
Packit Service 30b792
 * You should have received a copy of the GNU Lesser General Public
Packit Service 30b792
 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit Service 30b792
 * SPDX-License-Identifier: LGPL-2.1+
Packit Service 30b792
 *
Packit Service 30b792
 * Author(s):
Packit Service 30b792
 *     Maximilian Krambach <mkrambach@intevation.de>
Packit Service 30b792
 */
Packit Service 30b792
Packit Service 30b792
/**
Packit Service 30b792
 * Listing of all possible error codes and messages of a {@link GPGME_Error}.
Packit Service 30b792
 */
Packit Service 30b792
export const err_list = {
Packit Service 30b792
    // Connection
Packit Service 30b792
    'CONN_NO_CONNECT': {
Packit Service 30b792
        msg:'Connection with the nativeMessaging host could not be'
Packit Service 30b792
            + ' established.',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'CONN_EMPTY_GPG_ANSWER':{
Packit Service 30b792
        msg: 'The nativeMessaging answer was empty.',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'CONN_TIMEOUT': {
Packit Service 30b792
        msg: 'A connection timeout was exceeded.',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'CONN_UNEXPECTED_ANSWER': {
Packit Service 30b792
        msg: 'The answer from gnupg was not as expected.',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'CONN_ALREADY_CONNECTED':{
Packit Service 30b792
        msg: 'A connection was already established.',
Packit Service 30b792
        type: 'warning'
Packit Service 30b792
    },
Packit Service 30b792
    // Message/Data
Packit Service 30b792
    'MSG_INCOMPLETE': {
Packit Service 30b792
        msg: 'The Message did not match the minimum requirements for'
Packit Service 30b792
            + ' the interaction.',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'MSG_EMPTY' : {
Packit Service 30b792
        msg: 'The Message is empty.',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'MSG_WRONG_OP': {
Packit Service 30b792
        msg: 'The operation requested could not be found',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'MSG_NO_KEYS' : {
Packit Service 30b792
        msg: 'There were no valid keys provided.',
Packit Service 30b792
        type: 'warning'
Packit Service 30b792
    },
Packit Service 30b792
    'MSG_NOT_A_FPR': {
Packit Service 30b792
        msg: 'The String is not an accepted fingerprint',
Packit Service 30b792
        type: 'warning'
Packit Service 30b792
    },
Packit Service 30b792
    'KEY_INVALID': {
Packit Service 30b792
        msg:'Key object is invalid',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'KEY_NOKEY': {
Packit Service 30b792
        msg:'This key does not exist in GPG',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'KEY_NO_INIT': {
Packit Service 30b792
        msg:'This property has not been retrieved yet from GPG',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'KEY_ASYNC_ONLY': {
Packit Service 30b792
        msg: 'This property cannot be used in synchronous calls',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'KEY_NO_DEFAULT': {
Packit Service 30b792
        msg:'A default key could not be established. Please check yout gpg ' +
Packit Service 30b792
            'configuration',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'SIG_WRONG': {
Packit Service 30b792
        msg:'A malformed signature was created',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'SIG_NO_SIGS': {
Packit Service 30b792
        msg:'There were no signatures found',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    // generic
Packit Service 30b792
    'PARAM_WRONG':{
Packit Service 30b792
        msg: 'Invalid parameter was found',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'DECODE_FAIL': {
Packit Service 30b792
        msg: 'Decoding failed due to unexpected data',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    },
Packit Service 30b792
    'PARAM_IGNORED': {
Packit Service 30b792
        msg: 'An parameter was set that has no effect in gpgmejs',
Packit Service 30b792
        type: 'warning'
Packit Service 30b792
    },
Packit Service 30b792
    'GENERIC_ERROR': {
Packit Service 30b792
        msg: 'Unspecified error',
Packit Service 30b792
        type: 'error'
Packit Service 30b792
    }
Packit Service 30b792
};
Packit Service 30b792
Packit Service 30b792
/**
Packit Service 30b792
 * Checks the given error code and returns an {@link GPGME_Error} error object
Packit Service 30b792
 * with some information about meaning and origin
Packit Service 30b792
 * @param {String} code Error code as defined in {@link err_list}.
Packit Service 30b792
 * @param {String} info Possible additional error message to pass through.
Packit Service 30b792
 * Currently used for errors sent as answer by gnupg via a native Message port
Packit Service 30b792
 * @returns {GPGME_Error}
Packit Service 30b792
 */
Packit Service 30b792
export function gpgme_error (code = 'GENERIC_ERROR', info){
Packit Service 30b792
    if (err_list.hasOwnProperty(code)){
Packit Service 30b792
        if (err_list[code].type === 'error'){
Packit Service 30b792
            return new GPGME_Error(code);
Packit Service 30b792
        }
Packit Service 30b792
        if (err_list[code].type === 'warning'){
Packit Service 30b792
            // eslint-disable-next-line no-console
Packit Service 30b792
            // console.warn(code + ': ' + err_list[code].msg);
Packit Service 30b792
        }
Packit Service 30b792
        return null;
Packit Service 30b792
    } else if (code === 'GNUPG_ERROR'){
Packit Service 30b792
        return new GPGME_Error(code, info);
Packit Service 30b792
    }
Packit Service 30b792
    else {
Packit Service 30b792
        return new GPGME_Error('GENERIC_ERROR');
Packit Service 30b792
    }
Packit Service 30b792
}
Packit Service 30b792
Packit Service 30b792
/**
Packit Service 30b792
 * An error class with additional info about the origin of the error, as string
Packit Service 30b792
 * It is created by {@link gpgme_error}, and its' codes are defined in
Packit Service 30b792
 * {@link err_list}.
Packit Service 30b792
 *
Packit Service 30b792
 * @property {String} code Short description of origin and type of the error
Packit Service 30b792
 * @property {String} msg Additional info
Packit Service 30b792
 * @protected
Packit Service 30b792
 * @class
Packit Service 30b792
 * @extends Error
Packit Service 30b792
 */
Packit Service 30b792
class GPGME_Error extends Error{
Packit Service 30b792
    constructor (code = 'GENERIC_ERROR', msg=''){
Packit Service 30b792
Packit Service 30b792
        if (code === 'GNUPG_ERROR' && typeof (msg) === 'string'){
Packit Service 30b792
            super(msg);
Packit Service 30b792
        } else if (err_list.hasOwnProperty(code)){
Packit Service 30b792
            if (msg){
Packit Service 30b792
                super(err_list[code].msg + '--' + msg);
Packit Service 30b792
            } else {
Packit Service 30b792
                super(err_list[code].msg);
Packit Service 30b792
            }
Packit Service 30b792
        } else {
Packit Service 30b792
            super(err_list['GENERIC_ERROR'].msg);
Packit Service 30b792
        }
Packit Service 30b792
        this._code = code;
Packit Service 30b792
    }
Packit Service 30b792
Packit Service 30b792
    get code (){
Packit Service 30b792
        return this._code;
Packit Service 30b792
    }
Packit Service 30b792
}