var util = require('util'),
    colors = require('./colors.js');

module.exports = ErrorHandler;

function ErrorHandler(type, msg) {
    Error.call(this);

    var that = this,
        i = 1;

    // Write our opts to the object
    this.message = msg;
    this.name = type;

    if(arguments.length === 1) {
        this.name = 'WebdriverJSError';
        this.message = type;
        i = 0;
    }

    if(typeof this.message === 'object') {

        var seleniumStack = this.message;
        this.message = [];

        if(seleniumStack.message && seleniumStack.type && seleniumStack.status) {
            this.message.push(pad('', 6) + '(' + seleniumStack.type + ':' + seleniumStack.status + ') ' + seleniumStack.message);
        }

        if(seleniumStack.orgStatusMessage) {
            var problem = JSON.parse(seleniumStack.orgStatusMessage.slice(0,seleniumStack.orgStatusMessage.indexOf(',"request"')) + '}');
            this.message.push(pad('', 6) + 'Problem: ' + problem.errorMessage);
        }

        this.message = this.name + '\n' + this.message.join('\n');

    }
}

util.inherits(ErrorHandler, Error);

/**
 * make stack loggable
 * @return {Object} error log
 */
ErrorHandler.prototype.toJSON = function() {
    return {
        name: this.name,
        message: this.message
    };
}

ErrorHandler.prototype.addToCallStack = function(cmd) {
    if(!this.hasCallStack) {
        this.message += '\x1b[1;30m\n\n' + pad('', 6) + 'Callstack:\n';
        this.hasCallStack = true;
    }

    this.message += pad('-> ', 9) + cmd.name + cmd.args + '\n';
}

ErrorHandler.CommandError = function(msg) {
    return new ErrorHandler('CommandError',msg);
}
ErrorHandler.ProtocolError = function(msg) {
    return new ErrorHandler('ProtocolError',msg);
}
ErrorHandler.RuntimeError = function(msg) {
    return new ErrorHandler('RuntimeError',msg);
}

function pad(str, width) {
    return Array(width - str.length).join(' ') + str;
};
@@ -791,16 +791,111 @@
     var 
+problem;%0A%0A            if(typeof seleniumStack.orgStatusMessage !== 'string') %7B%0A                
 problem 
@@ -1006,18 +1006,125 @@
 ) + '%7D')
+.errorMessage
 ;%0A
+            %7D else %7B%0A                problem = seleniumStack.orgStatusMessage;%0A            %7D%0A%0A
         
@@ -1179,29 +1179,16 @@
  problem
-.errorMessage
 );%0A     
@@ -1266,16 +1266,99 @@
 ;%0A%0A    %7D
+ else %7B%0A        this.message = this.name + '%5Cn' + pad('', 6) + this.message;%0A    %7D%0A
 %0A%7D%0A%0Autil

improved output using appium