fridgerator / PyNode

Node - Python Interop
154 stars 14 forks source link

Fail Fast When Unsupported Non-Pure Python Types Are Encountered #6

Closed amcquistan closed 5 years ago

amcquistan commented 5 years ago

If an unsupported python type is encountered then throw a Nan::ThrowTypeError back up to JS

As an example, if you have a python function returning a numpy.ndarray like this

def test_numpy():
    import numpy as np
    return np.array([1,2,3])

and are calling test_numpy from Node like this

  new Promise((resolve, reject) => {
      try {
        pynode.call('test_numpy')
        resolve("all good");
      } catch(err) {
        console.log(err);
        reject('failed');
      }
  })

The following exception error is thrown and will be logged to the console

TypeError: Unsupported type returned (numpy.ndarray), only pure Python types are supported.
    at Promise (/Users/adammcquistan/Code/javascript/pynode/server.js:24:16)
    at new Promise (<anonymous>)
    at app.get (/Users/adammcquistan/Code/javascript/pynode/server.js:22:3)
    at Layer.handle [as handle_request] (/Users/adammcquistan/Code/javascript/pynode/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/adammcquistan/Code/javascript/pynode/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/adammcquistan/Code/javascript/pynode/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/adammcquistan/Code/javascript/pynode/node_modules/express/lib/router/layer.js:95:5)
    at /Users/adammcquistan/Code/javascript/pynode/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/adammcquistan/Code/javascript/pynode/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/adammcquistan/Code/javascript/pynode/node_modules/express/lib/router/index.js:275:10)
(node:89831) UnhandledPromiseRejectionWarning: TypeError: res.err is not a function
    at Promise.then.catch.err (/Users/adammcquistan/Code/javascript/pynode/server.js:36:21)
    at <anonymous>
    at runMicrotasksCallback (internal/process/next_tick.js:121:5)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)