var createError = require('createerror');
var HttpError = require('httperrors');
var socketCodesMap = require('./socketCodesMap');

var SocketError = module.exports = createError({
    name: 'SocketError',
    preprocess: function (err) {
        if (!(err instanceof SocketError) && err !== 'NotSocketError') {
            if (!SocketError.supports(err)) {
                if (err) {
                    var errOptions = {};
                    if (err.code) {
                        errOptions.code = err.code;
                    }
                }

                return new SocketError.NotSocketError(errOptions);
            }

            if (typeof err === 'string') {
                return new SocketError[err]();
            } else if (err && err.code && socketCodesMap.hasOwnProperty(err.code)) {
                return new SocketError[err.code](err);
            }
        }
    }
});

function allKeys(o) {
    var keys = [];

    for (var key in o) keys.push(key);

    return keys;
}

function createSocketError(errorCode) {
    var statusCode = socketCodesMap[errorCode] || 'Unknown';

    var options = {
        name: errorCode,
        code: errorCode,
        statusCode: statusCode,
        status: statusCode
    };

    // get constructed httpError to use to grab what we need to 'quack' like it
    var httpError = HttpError(statusCode);
    var httpErrorKeys = allKeys(httpError);

    // copy keys from the httpError to the socketError
    httpErrorKeys.forEach(function (key) {
        if (!options[key] && key !== 'message') {
            options[key] = httpError[key];
        }
    });
    SocketError[errorCode] = createError(options, SocketError);
}

SocketError.supports = function (errorOrErrorCode) {
    if (typeof errorOrErrorCode === 'string') {
        return socketCodesMap.hasOwnProperty(errorOrErrorCode);
    } else if (errorOrErrorCode && errorOrErrorCode.code) {
        return socketCodesMap.hasOwnProperty(errorOrErrorCode.code);
    } else {
        return false;
    }
};

// For backwards compatibility
SocketError.SocketError = SocketError;

// create an Unknown error sentinel
createSocketError('NotSocketError');

// create a new socket error for each error code
Object.keys(socketCodesMap).forEach(createSocketError);

@@ -129,778 +129,8 @@
 );%0A%0A
-var SocketError = module.exports = createError(%7B%0A    name: 'SocketError',%0A    preprocess: function (err) %7B%0A        if (!(err instanceof SocketError) && err !== 'NotSocketError') %7B%0A            if (!SocketError.supports(err)) %7B%0A                if (err) %7B%0A                    var errOptions = %7B%7D;%0A                    if (err.code) %7B%0A                        errOptions.code = err.code;%0A                    %7D%0A                %7D%0A%0A                return new SocketError.NotSocketError(errOptions);%0A            %7D%0A%0A            if (typeof err === 'string') %7B%0A                return new SocketError%5Berr%5D();%0A            %7D else if (err && err.code && socketCodesMap.hasOwnProperty(err.code)) %7B%0A                return new SocketError%5Berr.code%5D(err);%0A            %7D%0A        %7D%0A    %7D%0A%7D);%0A%0A
 func
@@ -910,16 +910,786 @@
 or);%0A%7D%0A%0A
+var SocketError = module.exports = createError(%7B%0A    name: 'SocketError',%0A    preprocess: function (err) %7B%0A        if (!(err instanceof SocketError) && err !== 'NotSocketError') %7B%0A            if (!SocketError.supports(err)) %7B%0A                if (err) %7B%0A                    var errOptions = %7B%7D;%0A                    if (err.code) %7B%0A                        errOptions.code = err.code;%0A                    %7D%0A                %7D%0A%0A                return new SocketError.NotSocketError(errOptions);%0A            %7D%0A%0A            if (typeof err === 'string') %7B%0A                return new SocketError%5Berr%5D();%0A            %7D else if (err && err.code && socketCodesMap.hasOwnProperty(err.code)) %7B%0A                return new SocketError%5Berr.code%5D(err);%0A            %7D%0A        %7D%0A    %7D%0A%7D);%0A%0A
 SocketEr

Rearrange the file as before.