Code coverage report for src/Common/Exception/Exception.js

Statements: 92.31% (24 / 26)      Branches: 75% (3 / 4)      Functions: 83.33% (5 / 6)      Lines: 92.31% (24 / 26)      Ignored: none     

All files » src/Common/Exception/ » Exception.js
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    1                           1                     1   97   97 1       96 96                           1   97         97   97                     1   97 97     97     97         16               97                                   97 143         1 1   1  
"use strict";
 
var _           = require("lodash"),
    Type        = require("../Type"),
    Safe        = require("../Safe"),
    I18NString  = require("../I18N/String"),
    Messages    = require("../I18N/Messages");
 
 
/**
 *
 * Internal methods
 *
 * @type {Object}
 *
 */
var Internal = {};
 
/**
 *
 * Get message
 *
 * @param  {Object} messages
 *
 * @return {I18NMessage}
 *
 */
Internal.getI18NMessage = function(message, messageData){
 
    messageData = Safe.object(messageData);
 
    if( message instanceof I18NString ){
        return message;
    }
 
    /// if message is null or empty
    Eif( Type.isString(message) ){
        return new I18NString(message, messageData, Messages);
    }
 
    return new I18NString("");
 
};
 
/**
 *
 * Get the stack trace
 *
 * @return {String}
 *
 */
Internal.getStackTrace = function(){
 
    var error    = new Error(),
        stackStr = Safe.string(error.stack),
        stack    = stackStr.split('\n');
 
    /// Remove Error message + 2 first lines of the stack trace
    stack.splice(0,3);
 
    return stack.join('\n');
 
};
 
/**
 *
 * @class Error Class
 *
 * @param {String|I18NString} message
 *
 */
var Exception = function(message, messageData) {
 
    message     = Internal.getI18NMessage(message, messageData);
    messageData = Safe.object(messageData);
 
    /// get the stack trace
    var stack = Internal.getStackTrace();
 
    /// define the error message
    Object.defineProperty(
        this,
        "message",
        {
            get: function(){
                return this.toString();
            },
            configurable: false
        }
    );
 
    /// define the this.stack to return the created Error
    /// stack
    Object.defineProperty(
        this,
        'stack', {
        get: function() {
            return this.toString() + "\n" + stack;
        },
        configurable: false
    });
 
    /**
     *
     * ToString method with i18n support
     *
     * @param  {Object} translations
     *
     * @return {String}
     *
     */
    this.toString = function(translations){
        return message.toString(translations);
    };
 
};
 
Exception.prototype = new Error();
Exception.prototype.constructor = Exception;
 
module.exports = Exception;