All files / src/errors MPesaError.ts

85% Statements 17/20
0% Branches 0/1
62.5% Functions 5/8
85% Lines 17/20

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    1x       4x 4x 4x       1x 1x 1x       1x       1x 1x                             1x               1x 1x                             1x                           1x 1x                                 1x
import { StkPushResponseType } from "../models/StkPushResponse";
 
export class MPesaError extends Error {
    public name: string;
 
    constructor(message: string, name: string = 'MPesaError') {
        super(message);
        this.name = name;
        Object.setPrototypeOf(this, new.target.prototype); // Restore prototype chain
    }
}
 
export class AuthenticationError extends MPesaError {
    constructor(message: string, public code: string) {
        super(message, 'AuthenticationError');
    }
}
 
export class StkPushError extends MPesaError {
    public data: StkPushResponseType;
 
    constructor(message: string, data: StkPushResponseType) {
        super(message, 'StkPushError');
        this.data = data;
    }
 
    /**
     * Returns a formatted error message including details from the response.
     * @returns A detailed error message.
     */
    getDetails(): string {
        return `STK Push Error: ${this.data.ResponseDescription} (Code: ${this.data.ResponseCode})`;
    }
}
 
/**
 * Represents an error from the B2C Pay Out API.
 */
export class B2CError extends MPesaError {
    public data: {
        requestId: string;
        errorCode: string;
        errorMessage: string;
    };
 
    constructor(message: string, data: { requestId: string; errorCode: string; errorMessage: string }) {
        super(message, 'B2CError');
        this.data = data;
    }
 
    /**
     * Returns a formatted error message including details from the response.
     * @returns A detailed error message.
     */
    getDetails(): string {
        return `B2C Error: ${this.data.errorMessage} (Code: ${this.data.errorCode})`;
    }
}
 
/**
 * Represents an error from the Register URL API.
 */
export class RegisterUrlError extends MPesaError {
    public data: {
        responseCode: string;
        responseMessage: string;
        customerMessage: string;
        timestamp: string;
    };
 
    constructor(message: string, data: {
        responseCode: string;
        responseMessage: string;
        customerMessage: string;
        timestamp: string;
    }) {
        super(message, 'RegisterUrlError');
        this.data = {
            responseCode: data.responseCode,
            responseMessage: data.responseMessage,
            customerMessage: data.customerMessage,
            timestamp: data.timestamp,
        };
    }
 
    /**
     * Returns a formatted error message including details from the response.
     * @returns A detailed error message.
     */
    getDetails(): string {
        return `Register URL Error: ${this.data.responseMessage} (Code: ${this.data.responseCode})`;
    }
}
 
export class ApiError extends MPesaError { }