# All files / utils/services/error error.service.ts

33.33% Statements 10/30
0% Branches 0/12
0% Functions 0/8
34.48% Lines 10/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    2437x
    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

     import * as formatError from 'apollo-errors';
    import * as boom from 'boom';

    // tslint:disable-next-line:max-line-length
    export type errorsType =

    // Ethereum Send Transaction Errors
    | 'invalid-address'
    | 'not-enought-funds'
    | 'transaction-reverted'
    | 'account-locked'
    | 'unknown-error'
    | 'missing-wallet-id'
    | 'unauthorized'
    | 'invalid-user-name'
    | 'invalid-address-to'
    | 'invalid-address-from'
    | 'wrong-user-name-or-password'
    | 'account-not-found'
    | 'invalid-account-password'

    ;

    export interface ServerErrors {
        name: errorsType;
        data: { bg: { message: string }, en: { message: string } };
    }

    export const ErrorMessages: ServerErrors[] = [
        {
            name: 'invalid-address',
            data: {
                en: {
                    message: 'Invalid address provided'
                },
                bg: {
                    message: 'Invalid address provided'
                }
            }
        },
        {
            name: 'not-enought-funds',
            data: {
                en: {
                    message: 'Not enought funds in your wallet'
                },
                bg: {
                    message: 'Not enought funds in your wallet'
                }
            }
        },
        {
            name: 'transaction-reverted',
            data: {
                en: {
                    message: 'Transaction reverted'
                },
                bg: {
                    message: 'Transaction reverted'
                }
            }
        },
        {
            name: 'unknown-error',
            data: {
                en: {
                    message: 'Unknown error contact customer service with given eid'
                },
                bg: {
                    message: 'Unknown error contact customer service with given eid'
                }
            }
        },
        {
            name: 'missing-wallet-id',
            data: {
                en: {
                    message: 'Missing wallet id'
                },
                bg: {
                    message: 'Missing wallet id'
                }
            }
        },
        {
            name: 'invalid-address-from',
            data: {
                en: {
                    message: 'Invalid address from'
                },
                bg: {
                    message: 'Invalid address from'
                }
            }
        },
        {
            name: 'invalid-address-to',
            data: {
                en: {
                    message: 'Invalid address to'
                },
                bg: {
                    message: 'Невалиден адрес до'
                }
            }
        },
        {
            name: 'invalid-user-name',
            data: {
                en: {
                    message: 'Invalid email or password'
                },
                bg: {
                    message: 'Невалиден имейл или парола'
                }
            }
        },
        {
            name: 'wrong-user-name-or-password',
            data: {
                en: {
                    message: 'The username or password are incorrect.'
                },
                bg: {
                    message: 'The username or password are incorrect.'
                }
            }
        },
        {
            name: 'unauthorized',
            data: {
                en: {
                    message: 'You are unable to fetch data'
                },
                bg: {
                    message: 'Нямате право да използвате този ресурс'
                }
            }
        },
        {
            name: 'account-not-found',
            data: {
                en: {
                    message: 'Ethereum account not found'
                },
                bg: {
                    message: 'Не е намерен Ethereum адрес'
                }
            }
        },
        {
            name: 'invalid-account-password',
            data: {
                en: {
                    message: 'Ethereum wallet password is invalid'
                },
                bg: {
                    message: 'Въведената парола за декриптиране на портфейла е невалидна'
                }
            }
        },
    ];

    export class ServerErrorsList {

        errorsList: ServerErrors[] = ErrorMessages;

        map(err: errorsType): string {
            return this.errorsList.filter(e => e.name === err)[0].name;
        }

    }

    interface ApolloErrors {
      createError(name: string, config: any);
      formatError(error);
      isInstance(e);
    }

    // interface PrivateErrors {
    //   eid: string;
    // }
    export const attachErrorHandlers = formatError.formatError;
    // <ApolloErrors>
    export const clientErrors = formatError;

    interface BoomErrorType {
      unauthorized(string?: string): any;
    }
    // BoomErrorType
    export const Boom = boom;

    export function createError(name: errorsType, message: string, data?: any): void {
      function guid() {
        function s4() {
          return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
        }
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
          s4() + '-' + s4() + s4() + s4();
      }
      data = data || {};
      data.eid = guid();
      message = `(${data.eid}): ${message}`;
      const error = clientErrors.createError(name, { message, data });
      return new error();
    }

    export const errorUnauthorized = function () {
      throw new createError('unauthorized', 'You are unable to fetch data');
    };

    export function transactionError(e) {
      if (e.message.includes('invalid address') || e.message.includes(`indrect IBAN address which can't be converted`)) {
        throw new createError('invalid-address', e.message.substring(0, e.message.indexOf('at')), {
          rawMessage: e.message.substring(0, e.message.indexOf('at'))
        });
      } else if (e.message.includes('have enough funds to send tx')) {
        throw new createError('not-enought-funds', e.message.substring(0, e.message.indexOf('at')), {
          rawMessage: e.message.substring(0, e.message.indexOf('at'))
        });
      } else if (e.message.includes('VM Exception while processing transaction: revert')) {
        throw new createError('transaction-reverted', e.message);
      } else if (e.message.includes('could not unlock signer account')) {
        throw new createError('account-locked', e.message);
      } else {
        throw new createError('unknown-error', e.message.substring(0, e.message.indexOf('at')), e);
      }
    }