log4js configure function error [NodeJs] - json

I am getting the following error in console window when running my NodeJs application. I am using log4js for logging. Runtime platform is express.
Error:
undefined:1
?{
^
SyntaxError: Unexpected token ? in JSON at position 0
Code:
var log4js = require('log4js');
var mylog = log4js.configure('log4jsConfig.json');
logger = log4js.getLogger("absolute-logger");
Config json:
{
"appenders": [
{
"type": "file",
"absolute": true,
"filename": "c:/temp/log_file.log",
"maxLogSize": 20480,
"backups": 10,
"category": "absolute-logger"
}
]
}
Question:
Any thoughts. I am thinking this is parsing error, but not sure how to resolve it.
Thanks,

Are you on a windows system? Sometimes, parsers get upset with \r\n (carriage returns). Try converting it UNIX (new line only) style lines.

Related

JSON formatting error which is hard to understand

I am sending the following valid JSON as a message via MQTT to a device :
{
"devices": [
{
"known": true,
"local": false,
"eep": "MSC_V3_PRESSAC_20",
"id": "01:96:23:83",
"O_nominalVoltage": 120
}
],
"uuid": "821ca781-dd98-4531-8391-19d4e4da2c96"
}
but I receive the following error back :
"Handling command: org.json.JSONException: Expected a ',' or '}' at character 62 of {devices:[{known:true,local:false,eep:MSC_V3_PRESSAC_20,id:01:96:23:83,O_nominalVoltage:120}],uuid:821ca781-dd98-4531-8391-19d4e4da2c96}"
}
Can anyone see anything wrong with this? I don't understand why there is a problem with character 62.
For more information, I am sending the message via the mosquitto_pub command when publishing to an MQTT topic on a remote device.
My device is running Java code which uses the Java Paho libraries. I am using the AWS IoT MQTT broker.

MalformedPolicyDocument error while creating an IAM Policy

I'm trying to create a managed policy by AWS CLI:
POLICY='
{
"Version":"2012-10-17",
"Statement":
[{
"Effect":"Allow",
"Action":
[
"cloudformation:*"
],
"Resource":"*"
},
{
"Effect":"Deny",
"Action":
[
"cloudformation:UpdateStack",
"cloudformation:DeleteStack"
],
"Resource": "'${arn}'"
}]
}'
# Create policy if not already created
[ $(aws iam list-policies | grep -ce CloudFormation-policy-${StackName}) -eq 0 ] && (aws iam create-policy --policy-name CloudFormation-policy-${StackName} --policy-document "'${POLICY}'")
When I run the script I get this error:
An error occurred (MalformedPolicyDocument) when calling the CreatePolicy operation: Syntax errors in policy.
I can't figure out where the error is.
Any idea?
Each operating systems has its own way of treating single quote vs double quote escaping and as per AWS CLI documentation:
When passing in large blocks of data, you might find it easier to save
the JSON to a file and reference it from the command line. JSON data
in a file is easier to read, edit, and share with others.
Quoting Strings approach might not be best choice while passing Json data, instead use Loading parameters from file approach.

npm unexpected token / in config.json at position 98

I have seen several unexpected token errors related to npm but this one is yet again unique to me. I could not find any similar issues posted by others. My config file just has connection objects to my local mongodb, rabbitmq "amqp", wordpress plugin and mysql DB.
Could anyone help me with this error in case they've seen it before?
C:\xampp\htdocs\livetrader\chat\node_modules\nconf\lib\nconf\stores\file.js:160
throw new Error("Error parsing your configuration file: [" + this.file + ']: ' + ex.message);
^
Error: Error parsing your configuration file:
[C:\xampp\htdocs\livetrader\chat/config.json]: Unexpected token / in JSON at position 98
Updating this post with the config.json file referenced in this error
{
"port": 1337,
"mongoose": {
"uri": "mongodb://localhost:27017/livetrader"
},
"amqp":{
"uri": "amqp://guest:guest#localhost:15672",
"queue": "TickData"
},
"sql":{
"host":"localhost",
"database":"db_main",
"user":"root",
"password":""
},
"wp":{
"host":"http://forextrader.dev",
"LOGGED_IN_KEY":"MY_LOGGED_IN_KEY",
"LOGGED_IN_SALT":"MY_LOGGED_IN_SALT",
"prefix":"wp_",
"avatarPath":"http://localhost/livetrader/wp-content/uploads/avatars",
"myCreedApi":"MY_API_KEY"
},
"guest":{
"cookie_name": "trading-forex-guest",
"ttl": 180
}
}
Updating the back-end config path
"use strict";
var nconf = require('nconf');
nconf.argv()
.env()
.file({
file: process.cwd() + '/config.json'
});
module.exports = nconf;
Your JSON file is property formatted. JSON.parse() takes it, no problem. Character 98 is not a slash, either.
There is, however, a suspicious slash in your path:
C:\xampp\htdocs\livetrader\chat/config.json
The final slash is unix-style, not windows-style. Could that be causing the problem?
If that's not it, you must be reading the wrong file, or passing it the wrong way to a function. Try using console.log to figure out what exactly is reaching JSON.parse().
Giving objects to JSON.parse() can cause unexpected errors, since .toString() is invoked. For example:
> JSON.parse({a:1})
SyntaxError: Unexpected token o in JSON at position 1
> ({a:1}).toString()
'[object Object]'
So, to recap, you must be:
Reading the wrong file
Passing an object that's not a String to JSON.parse()
Having a strange problem with the inverted slash in the path

Missing exception info in NodeJS JSON.parse

In NodeJS, if I let a JSON.parse exception go unhandled and subsequently crash the server, I get an important bit of information outputted to the console.
My JSON:
{
"familyName": "The Flintstones",
"familyContact": {
"fullName": "Wifie Flintstone",
"contact": {
"street1": "1 Granite Ave",
"city": "Bedrock",
"state": "TX",
"postal": "10000"
}
},
this should make it blow up
"patients": [{
"personInfo": {
"fullName": "Wilma Flintstone"
}}, {
"personInfo": {
"fullName": "Fred Flintstone"
}
}
]
}
This is the console output when I let it go unhandled:
undefined:12
this should make it blow up
^
SyntaxError: Unexpected token t
at Object.parse (native)
at Object.exports.uploadPackage.flowUploads.post.flowUploads.write.onDone (/home/ubuntu/workspace/app/controllers/admin/admin.families.server.controller.js:99:27)
at module.exports.$.write.pipeChunk (/home/ubuntu/workspace/app/services/flowUploads.js:173:49)
at Object.cb [as oncomplete] (fs.js:169:19)
but if I catch the exception to prevent the crash, and log like so...
try{
var fileData = JSON.parse(fileText);
}
catch (ex) {
console.log("REASON FOR JSON FAILURE: " + ex, ex.arguments, ex.stack.split("\n"));
}
I get output like this:
REASON FOR JSON FAILURE: SyntaxError: Unexpected token t [ 't' ] [ 'SyntaxError: Unexpected token t',
' at Object.parse (native)',
' at Object.exports.uploadPackage.flowUploads.post.flowUploads.write.onDone (/home/ubuntu/workspace/app/controllers/admin/admin.families.server.controller.js:100:27)',
' at module.exports.$.write.pipeChunk (/home/ubuntu/workspace/app/services/flowUploads.js:173:49)',
' at Object.cb [as oncomplete] (fs.js:169:19)' ]
Missing that key piece of useful info from the crash log that contains the line number and snip of faulty text:
undefined:12
this should make it blow up
^
How I can get to that bit so I can include it in my error messaging?
You can't get that first bit you displayed from the error since it isn't part of the error.
undefined:12
this should make it blow up
^
This is because node is adding that section before dying to show why it died and then shows the error generated by JSON.parse.
To get more detailed messaging you will have to use something other then JSON.parse. As robertklep mentioned you could try greatjson or something similar if getting greater detail on your JSON parsing is important, such as if you are building a service which parses JSON and returns the errors.

Getting an error in all figway python scripts

I cloned the github project of figway in order to query the attributes of the entities to the orion but i'm getting an error in all python scripts:
File "GetEntity.py", line 37, in <module>
config = ConfigParser.RawConfigParser(allow_no_value=True)
TypeError: __init__() got an unexpected keyword argument 'allow_no_value'
I called it like -> python GetEntity.py Room
Some tips to investigate what is going on:
You should be using Python2.7 to run these scripts. Can you please let me know which version and OS are you using?
We have updated FIGWAY last week. Can you please clone it again if you did it before?
You should be using the new scripts at folder: /python-IDAS4/ContextBroker
With the previous assumptions you should get something like this (as long as that entity does not exist on that ContextBroker at the time being):
i6#raspberrypi ~/github/fiware-figway/python-IDAS4/ContextBroker $ python GetEntity.py Room
* Asking to http://130.206.80.40:1026/ngsi10/queryContext
* Headers: {'Fiware-Service': 'OpenIoT', 'content-type': 'application/json', 'accept': 'application/json', 'X-Auth-Token': 'NULL'}
* Sending PAYLOAD:
{
"entities": [
{
"type": "",
"id": "Room",
"isPattern": "false"
}
],
"attributes": []
}
...
* Status Code: 200
* Response:
{
"errorCode" : {
"code" : "404",
"reasonPhrase" : "No context element found"
}
}