One of my NodeJS / ExpressJS app gets quite from time to time by the error:
SyntaxError: Unexpected end of input
at parse (native)
Unfortunately there is no line number and no stack. The hole project is based on typescript and compiles just fine. The process is running on a PM2.
Any idea how to hunt down this error?
Regards and thx...
Search your app source code for JSON.parse, then find if any of the input your are getting is a proper JSON. I suspect you are getting HTML page as a response for one of your requests (due to 404 or 5xx)
To track down the error:
try{
JSON.parse(body);
}
catch(err){
console.log('error due to ', err);
}
Related
I am new to nodejs (async) debugging. I would appreciate some help regarding error messages -
var l, m, opiton= {};
// option has actually been mis-spelt here as opiton
.....
// some more code
option.name= "new name";
Now, at the point of assignment: option.name= "new name";
nodejs server freezes without any indication, that it has encountered an error.
It would be great if nodejs could "auto generate" an error message, and say -
undefined object "option" in line 5178 or something similar.
Figuring out that option was actually mis-spelt as opiton takes a lot of time, especially in a large code base, with multiple callbacks per request. And it would be great, if that time can be saved.
Question - Is there a generic solution for locating run-time errors in nodejs servers?
(In a large server code base, adding a ton of logging is useful for running analytics in production environment, but not the most optimum solution for a test environment)
You can set an uncaught error handler:
process.on('uncaughtException', function (err) {
console.log('UncaughtException: message: ' + err.message);
console.log('UncaughtException: stack: ' + err.stack);
});
I would be very hesitant to use this pattern in production code since at this point the program is in an unknown state. It's best to exit at this point and (potentially) restart.
I'm trying to load a XOD document into a PDFTron WebViewer. As far as I can read in the documentation and samples, this should be a simple "plug and play"-operation - it should simply work when you point at a file. Ideally, in my example, the document should be fetched from a service, as so:
fetch('/myservice/GetXOD')
.then(function(data) {
$(function() {
var viewerElement = document.getElementById("viewer");
var myWebViewer = new PDFTron.WebViewer({
initialDoc: data.body
}, viewerElement);
});
});
Unfortunately I get the following error:
Uncaught TypeError: Cannot read property 'DisplayModes' of undefined
The reason I'm doing it in a fetch, is because I'm rendering a Handlebars template, and pass the data to instantiate in a callback. However, I've isolated the code into an otherwise "empty" HTML-document, and in the simplified example below, I'm simply pointing at the XOD provided by PDFTron on page load (no fetch this time).
$(function() {
var viewerElement = document.getElementById("viewer");
var myWebViewer = new PDFTron.WebViewer({
initialDoc: 'GettingStarted.xod' // Using the XOD provided by PDFTron
}, viewerElement);
});
This unfortunately returns a different error (HTTP status 416).
Uncaught Error: Error loading document: Error retrieving file: /doc/WebViewer_Developer_Guide.xod?_=-22,. Received return status 416.
The same error appears when I run the samples from PDFTron on localhost.
I'm at a complete loss of how I should debug this further - all the samples assume everything is working out of the box.
I should note that I can actually get PDFs working just fine on localhost, but not on the server. XODs are problematic both on the server and on localhost.
I'm sorry to hear you are having troubles running our samples.
Your error message says 416 which means "Requested range not satisfiable". Perhaps your development servers do not support byte range requests (https://en.wikipedia.org/wiki/Byte_serving).
Could you try passing an option streaming: true? When streaming is true you're just requesting the entire file up front which shouldn't be a problem for any servers but it is a problem for WebViewer if the file is large because it will need to be completely downloaded and parsed at the start which is slow.
The following node.js test code fragment works perfectly when run for the first time (creating the file), but fails to overwrite the file once it's already created and instead generates a syntax error when the code is run a second time: 'SyntaxError: Unexpected token'. The Node docs say that fs.writeFile "Asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer." Not sure what I'm doing wrong or missing regarding this, thanks! I'm on Node 4.2.2
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', function (err) {
if(err){
throw err;
}
else{
console.log('It\'s saved!');
}
});
Based on the stack trace you've supplied in your comment, the error you're seeing is a problem with another part of your code, not the snippet you've posted.
Furthermore, it appears that you (or some other function is doing so behind the scenes) are attempting to JSON.parse() some string, but the string is not actually (valid) JSON (perhaps it is HTML or some other type). If you are getting this data from an HTTP response, you may want to check the value of res.headers['content-type'] first because attempting to use JSON.parse().
I am trying to post comments through the youtube API, but they are returning an error upon trying.
When I log the error object I get
{ [Error: Bad Request] code: 400 }
The first part of which looks weird (key-value inside array?)
I tried to log the keys of the object and it only returns code
Supposely the API return a much more detailed message, but it's like it's 'hidden' inside the [Error: Bad Request], which I dont know how to access
That's just how Node formats errors.
Try it yourself in a node console:
var e = new Error("Test");
e.code = 500
console.log(e); // Outputs: { [Error: Test] code: 500 }
As far as your issue with the YouTube API, I suggest creating a different question on StackOverflow about your issues, providing more information around how you are calling the API.
To avoid node/ express server crashing, it is always a good idea to catch the errors. As far as I found, there are three ways to record the error:
throw new Error(err);
logger(err);
res,json(500, err);
Should I use all of them to catch an error, if so, what is the invoking order?
Is it possible to avoid crashing if we just throw the error?
Check out this guy link and learn about error handling
https://www.youtube.com/watch?v=p-2fzgfk9AA
His video is very informative and he explains all different types of error handlings
You can use a package connect-domain.
Here is the example.
http://masashi-k.blogspot.com/2012/12/express3-global-error-handling-domain.html
Or You can use node.js built in uncaught exception event to handle uncaught errors.
//put this code in your server.js
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});