bf52cce7c4252a753be5f6312f9c8ca1861ba313
[platform/upstream/gpgme.git] / lang / js / src / Errors.js
1 /* gpgme.js - Javascript integration for gpgme
2  * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GPGME is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18  * SPDX-License-Identifier: LGPL-2.1+
19  *
20  * Author(s):
21  *     Maximilian Krambach <mkrambach@intevation.de>
22  */
23
24 /**
25  * Listing of all possible error codes and messages of a {@link GPGME_Error}.
26  */
27 export const err_list = {
28     // Connection
29     'CONN_NO_CONNECT': {
30         msg:'Connection with the nativeMessaging host could not be'
31             + ' established.',
32         type: 'error'
33     },
34     'CONN_EMPTY_GPG_ANSWER':{
35         msg: 'The nativeMessaging answer was empty.',
36         type: 'error'
37     },
38     'CONN_TIMEOUT': {
39         msg: 'A connection timeout was exceeded.',
40         type: 'error'
41     },
42     'CONN_UNEXPECTED_ANSWER': {
43         msg: 'The answer from gnupg was not as expected.',
44         type: 'error'
45     },
46     'CONN_ALREADY_CONNECTED':{
47         msg: 'A connection was already established.',
48         type: 'warning'
49     },
50     // Message/Data
51     'MSG_INCOMPLETE': {
52         msg: 'The Message did not match the minimum requirements for'
53             + ' the interaction.',
54         type: 'error'
55     },
56     'MSG_EMPTY' : {
57         msg: 'The Message is empty.',
58         type: 'error'
59     },
60     'MSG_WRONG_OP': {
61         msg: 'The operation requested could not be found',
62         type: 'error'
63     },
64     'MSG_NO_KEYS' : {
65         msg: 'There were no valid keys provided.',
66         type: 'warning'
67     },
68     'MSG_NOT_A_FPR': {
69         msg: 'The String is not an accepted fingerprint',
70         type: 'warning'
71     },
72     'KEY_INVALID': {
73         msg:'Key object is invalid',
74         type: 'error'
75     },
76     'KEY_NOKEY': {
77         msg:'This key does not exist in GPG',
78         type: 'error'
79     },
80     'KEY_NO_INIT': {
81         msg:'This property has not been retrieved yet from GPG',
82         type: 'error'
83     },
84     'KEY_ASYNC_ONLY': {
85         msg: 'This property cannot be used in synchronous calls',
86         type: 'error'
87     },
88     'KEY_NO_DEFAULT': {
89         msg:'A default key could not be established. Please check yout gpg ' +
90             'configuration',
91         type: 'error'
92     },
93     'SIG_WRONG': {
94         msg:'A malformed signature was created',
95         type: 'error'
96     },
97     'SIG_NO_SIGS': {
98         msg:'There were no signatures found',
99         type: 'error'
100     },
101     // generic
102     'PARAM_WRONG':{
103         msg: 'Invalid parameter was found',
104         type: 'error'
105     },
106     'DECODE_FAIL': {
107         msg: 'Decoding failed due to unexpected data',
108         type: 'error'
109     },
110     'PARAM_IGNORED': {
111         msg: 'An parameter was set that has no effect in gpgmejs',
112         type: 'warning'
113     },
114     'GENERIC_ERROR': {
115         msg: 'Unspecified error',
116         type: 'error'
117     }
118 };
119
120 /**
121  * Checks the given error code and returns an {@link GPGME_Error} error object
122  * with some information about meaning and origin
123  * @param {String} code Error code as defined in {@link err_list}.
124  * @param {String} info Possible additional error message to pass through.
125  * Currently used for errors sent as answer by gnupg via a native Message port
126  * @returns {GPGME_Error}
127  */
128 export function gpgme_error (code = 'GENERIC_ERROR', info){
129     if (err_list.hasOwnProperty(code)){
130         if (err_list[code].type === 'error'){
131             return new GPGME_Error(code);
132         }
133         if (err_list[code].type === 'warning'){
134             // eslint-disable-next-line no-console
135             // console.warn(code + ': ' + err_list[code].msg);
136         }
137         return null;
138     } else if (code === 'GNUPG_ERROR'){
139         return new GPGME_Error(code, info);
140     }
141     else {
142         return new GPGME_Error('GENERIC_ERROR');
143     }
144 }
145
146 /**
147  * An error class with additional info about the origin of the error, as string
148  * It is created by {@link gpgme_error}, and its' codes are defined in
149  * {@link err_list}.
150  *
151  * @property {String} code Short description of origin and type of the error
152  * @property {String} msg Additional info
153  * @protected
154  * @class
155  * @extends Error
156  */
157 class GPGME_Error extends Error{
158     constructor (code = 'GENERIC_ERROR', msg=''){
159
160         if (code === 'GNUPG_ERROR' && typeof (msg) === 'string'){
161             super(msg);
162         } else if (err_list.hasOwnProperty(code)){
163             if (msg){
164                 super(err_list[code].msg + '--' + msg);
165             } else {
166                 super(err_list[code].msg);
167             }
168         } else {
169             super(err_list['GENERIC_ERROR'].msg);
170         }
171         this._code = code;
172     }
173
174     get code (){
175         return this._code;
176     }
177 }