fnproject / fn

The container native, cloud agnostic serverless platform.
http://fnproject.io
Apache License 2.0
5.76k stars 405 forks source link

Fn Project - Nodejs app - error decoding invalid character 'a' looking for beginning of value #975

Closed vinayakvanarse closed 6 years ago

vinayakvanarse commented 6 years ago

Hi, I am pretty new to Fn Project. I have created a nodejs app and now trying to run on Fn Server. In order to see what is happening in the code i.e. to debug I have few console.log statements.

the command fn --verbose run gives -> error decoding invalid character 'a' looking for beginning of value

$ fn --verbose run Building image landingstats:0.0.8 Sending build context to Docker daemon 202.2kB Step 1/9 : FROM fnproject/node:dev as build-stage ---> 016382f39a51 Step 2/9 : WORKDIR /function ---> Using cache ---> abf3e4b223b7 Step 3/9 : ADD package.json /function/ ---> Using cache ---> ecc30d53d40e Step 4/9 : RUN npm install ---> Using cache ---> 7613f4cdfaaf Step 5/9 : FROM fnproject/node ---> 016382f39a51 Step 6/9 : WORKDIR /function ---> Using cache ---> abf3e4b223b7 Step 7/9 : ADD . /function/ ---> 41fd03393305 Step 8/9 : COPY --from=build-stage /function/node_modules/ /function/node_modules/ ---> 413ce08e9143 Step 9/9 : ENTRYPOINT ["node", "func.js"] ---> Running in a868d84dffef Removing intermediate container a868d84dffef ---> d11e1d07cec4 Successfully built d11e1d07cec4 Successfully tagged landingstats:0.0.8

error decoding invalid character 'a' looking for beginning of value

//------------------- create function director -------------

Steps to reproduce: Follow -> https://github.com/fnproject/tutorials/blob/master/node/intro/README.md and create a sample nodejs function

//------------------- func.js. ------------------------

`'use strict';

var fdk=require('@fnproject/fdk'); const elasticsearch = require('elasticsearch');

fdk.handle(function(input, ctx) { console.log('app'); // <--- This is where the error is being thrown. it cannot parse I guess 'app' console.log('ctx = '+ JSON.stringify(ctx)); console.log('input = '+JSON.stringify(input));

switch (ctx.protocol.method) { case 'GET': return handleGET(input, ctx); break; case 'PUT': return handlePUT(input, ctx); break; case 'POST': return handlePOST(input, ctx); break; case 'DELETE': return handleDELETE(input, ctx); break; case 'OPTIONS': return handleOPTIONS(input, ctx); break; default: ctx.protocol.statusCode = 500; return { error: 'Something blew up!' }; break; }

});`

vinayakvanarse commented 6 years ago

Now I tried to use npm package docker-logger ... It gives me different error...

put dependency in package.json

"dependencies": {
    "@fnproject/fdk": "0.x",
    "async": "^2.6.0",
    "bluebird": "^3.5.1",
    "elasticsearch": "^13.3.1",
    "lodash": "^4.17.5",
    "moment": "^2.22.1",
    "npm": "^5.8.0",
    "docker-logger" : "^0.0.24"
}

// --------- func.js ` 'use strict';

var fdk=require('@fnproject/fdk'); const elasticsearch = require('elasticsearch'); var configLog = { exitOn: { EADDRINFO: true, errorFatal: true, unhandledRejection: true, connectionErrors: false, }, syslog: { enabled: false, level: 'info', port: 3030, server: 'localhost', type: 'RFC5424', facility: 'local0', }, meta: { enabled: false, level: 'info', port: 3031, server: 'localhost', type: 'TCP_META', facility: 'local0', }, console: { enabled: true, level: 'debug', }, file: { enabled: true, level: 'debug', location: '/tmp', } }; var log = require( 'docker-logger' )(configLog);

fdk.handle(function(input, ctx) { log.info('app'); log.info('ctx = '+ JSON.stringify(ctx)); log.info('input = '+JSON.stringify(input));

switch (ctx.protocol.method) { case 'GET': return handleGET(input, ctx);//handleOriginAndAuth(input, ctx); break; case 'PUT': return handlePUT(input, ctx); break; case 'POST': return handlePOST(input, ctx); break; case 'DELETE': return handleDELETE(input, ctx); break; case 'OPTIONS': return handleOPTIONS(input, ctx); break; default: ctx.protocol.statusCode = 500; return { error: 'Something blew up!' }; break; }

});`

$ fn --verbose run Building image landingstats:0.0.8 Sending build context to Docker daemon 202.2kB Step 1/9 : FROM fnproject/node:dev as build-stage ---> 016382f39a51 Step 2/9 : WORKDIR /function ---> Using cache ---> abf3e4b223b7 Step 3/9 : ADD package.json /function/ ---> Using cache ---> ecc30d53d40e Step 4/9 : RUN npm install ---> Using cache ---> 7613f4cdfaaf Step 5/9 : FROM fnproject/node ---> 016382f39a51 Step 6/9 : WORKDIR /function ---> Using cache ---> abf3e4b223b7 Step 7/9 : ADD . /function/ ---> 3eb2e1dc1286 Step 8/9 : COPY --from=build-stage /function/node_modules/ /function/node_modules/ ---> 9268131074c2 Step 9/9 : ENTRYPOINT ["node", "func.js"] ---> Running in 00da9433b48d Removing intermediate container 00da9433b48d ---> 9e7a2fb1be13 Successfully built 9e7a2fb1be13 Successfully tagged landingstats:0.0.8

error decoding json: cannot unmarshal number into Go value of type main.jsonOut

denismakogon commented 6 years ago

You can’t write your logs to STDOUT because you break IO protocol interactions between Fn and a function. Write logs to STDERR only.

vinayakvanarse commented 6 years ago

Thanks a ton. It is now resolved.

denismakogon commented 6 years ago

I recommend to join community slack for such questions.

vinayakvanarse commented 6 years ago

console.log is resolved.... how about using docker-logger dependency. Would you have some info on what is going wrong. In future dev would like in production to output the errors to some file on different drive. They may use docker-logger etc package which uses internally Winston logger. May I seek help on that too.

denismakogon commented 6 years ago

Well, function doesn’t keep state, so, all files you’d persist will just disapear. Fn logs API was designed particularly to do what you want - to be a store for function logs. Check fn logs -h.

Do not consider serverless as yet another container orchestration tool.

vinayakvanarse commented 6 years ago

Thanks a ton for the help. I will look at fn logs and try to use it.