'use strict'

const serverlessLog = require('../serverlessLog.js')

module.exports = class InProcessRunner {
  constructor(functionName, handlerPath, handlerName) {
    this._functionName = functionName
    this._handlerName = handlerName
    this._handlerPath = handlerPath
  }

  run(event, context, callback) {
    // TODO error handling? when module and/or function is not found
    // lazy load handler with first usage

    let handler

    try {
      handler = require(this._handlerPath)[this._handlerName] // eslint-disable-line
    } catch (err) {
      if (err.code === 'MODULE_NOT_FOUND') {
        serverlessLog.errorLog(
          `Could not find handler module '${this._handlerPath}' for function '${this._functionName}'.`,
        )
      }

      throw err
    }

    if (typeof handler !== 'function') {
      throw new Error(
        `offline: handler '${this._handlerName}' in ${this._handlerPath} is not a function`,
      )
    }

    return handler(event, context, callback)
  }
}

@@ -11,62 +11,8 @@
 t'%0A%0A
-const serverlessLog = require('../serverlessLog.js')%0A%0A
 modu
@@ -264,324 +264,118 @@
  // 
-TODO error handling? when module and/or function is not found%0A    // lazy load handler with first usage%0A%0A    let handler%0A%0A    try %7B%0A      handler = require(this._handlerPath)%5Bthis._handlerName%5D // eslint-disable-line%0A    %7D catch (err) %7B%0A      if (err.code === 'MODULE_NOT_FOUND') %7B%0A        serverlessLog.errorLog(%0A  
+check if the handler module path exists%0A    if (!require.resolve(this._handlerPath)) %7B%0A      throw new Error(%0A
     
@@ -478,22 +478,18 @@
 ,%0A      
-  )%0A  
+)%0A
     %7D%0A%0A 
@@ -495,25 +495,134 @@
     
-  throw err%0A    %7D
+// lazy load handler with first usage%0A    const handler = require(this._handlerPath)%5Bthis._handlerName%5D // eslint-disable-line
 %0A%0A  

Fix handler module path check, was broken for module not found in handler