/* global exports */
"use strict";

// module Neodoc

var fs = require('fs');
var path = require('path');

exports.jsError = function(msg) {
  return function(payload) {
    return new Error({
      message: msg,
      payload: payload
    })
  }
};

exports.undefined = undefined;

exports.toString = function(value) {
  return value.toString();
}

/**
 * Try and detect the version as indicated in the package.json neighbouring
 * the main module. Uses `require.main` to detect the main module and traverses
 * the parent directories in a search for a `package.json` using
 * `require.main.paths`.
 */

exports.readPkgVersionImpl = function (Just) {
  return function(Nothing) {
    return function() {
      if (!require.main) {
        return Nothing;
      } else if (require.main && require.main.paths /* in node? */){
        for (var i=0; i < require.main.paths.length; i++) {
          var xs = require.main.paths[i].split(path.sep);
          if (xs.pop() === 'node_modules' && xs.length > 1) {
            xs.push('package.json');
            var p = xs.join(path.sep);
            if (fs.existsSync(p)) {
              return Just(JSON.parse(fs.readFileSync(p)).version);
            }
          }
        }
        return Nothing;
      }
      return Nothing;
    }
  }
};

@@ -100,16 +100,223 @@
 ath');%0A%0A
+function NeodocError(message, payload) %7B%0A  this.message = message;%0A  this.payload = payload;%0A  Error.call(this);%0A%7D%0A%0ANeodocError.prototype.name = 'NeodocError';%0ANeodocError.prototype = Object.create(Error);%0A%0A
 exports.
@@ -389,69 +389,39 @@
 new 
-Error(%7B%0A      message: msg,%0A      payload: payload%0A    %7D)
+NeodocError(msg, payload);
 %0A  %7D
+;
 %0A%7D;%0A

Add custom error type