Extending Error gives wrong stack trace information - javascript

This question is not the same as Extended Errors do not have message or stack trace, which deals with es6 extends, but I'm using es5.
Here's my code
test.js
function FooError(message) {
this.stack = (new Error(message)).stack;
console.log('this.stack', this.stack);
console.log('new_error_stack', (new Error(message)).stack);
console.log('after');
}
FooError.prototype = new Error;
throw new FooError("foo");
This gives the following output : with node version 6.4 (node test.js)
this.stack Error
at Object.<anonymous> (/tmp/test.js:7:22)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
new_error_stack Error: foo
at Error.FooError (/tmp/test.js:4:33)
at Object.<anonymous> (/tmp/test.js:9:7)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
after
/tmp/test.js:9
throw new FooError("foo");
^
Error
at Object.<anonymous> (/tmp/test.js:7:22)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
as you can see, the 'this.stack' doesn't show the same thing at all than the 'new_error_stack', eg, the this.stack shows the stack as if the error was thrown when the require was called. Also, the thrown stack is also "incorrect".
I took the code from https://stackoverflow.com/a/5251506/1993501 , and since I want to use instanceof FooError, I use the line FooError.prototype = new Error.
As you can see, the 'new_error_stack' gives the expected output, but I would have expected both values to be equal.
This has the effect that errors are not very useful for users of my library, see corresponding issue : https://github.com/open-xml-templating/docxtemplater/issues/245
What exactly is happening, and how can I prevent it and keep proper error types (eg being able to use instanceof) ?

I found a possible solution to your problem, but I'm not entirely sure it fixes all of your issues. I'm using Error.prototype instead of new Error (which is what you had). I found this solution from the first answer provided to this question: How do I create a custom Error in JavaScript?
I also changed your console.log lines to concatenate the two strings, as I was using alert() to display the output for testing purposes.
Kevin Hakanson goes over why you may or may not want to go with this solution in his answer. Here's the code to answer your question:
function FooError(message) {
this.stack = (new Error(message)).stack;
console.log('this.stack ' + this.stack);
console.log('new_error_stack ' + (new Error(message)).stack);
console.log('after');
}
FooError.prototype = Error.prototype;
throw new FooError("foo");
You may want to use a try/catch block with that last line.

Related

JavaScript error, missing a ``)`` but when it is added ``Unexpected end of input``? - Help Needed

Hello (I am very new to programming!)
I keep getting this error message (below) whenever I try to run my bot in CMD.
(Scroll down for code and error and stuff) I am super confused about all of this!
I tried fixing it by removing the ; but that didn't work, it told me the ) was unexpected, but when I removed the ) it told me it was needed!
Code Block One:
// Runs on bot start
bot.on('ready', () => {
// Now we post into the console that the bot has been turned on.
console.log('Bot turned on in the CMD')
});
Code Block Two: (Semi-Colon Removed)
// Runs on bot start
bot.on('ready', () => {
// Now we post into the console that the bot has been turned on.
console.log('Bot turned on in the CMD')
})
Code Block Three:
bot.on('ready', () => {
// Now we post into the console that the bot has been turned on.
console.log('Bot turned on in the CMD')
}
Expected Result:
Bot turns on, sends a message in the CMD to that effect.
Error #1 (See code block 1 to find the code)
`C:\Users\xgoul\OneDrive\Desktop\Discord Bot\app.js:74
});
^
SyntaxError: Unexpected end of input
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)`
Error #2 (See code block 2 to find the code)
`C:\Users\xgoul\OneDrive\Desktop\Discord Bot\app.js:74
})
^
SyntaxError: Unexpected end of input
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)`
Error #3 (See code block 3 to find the code)
`C:\Users\XXXX\OneDrive\Desktop\Discord Bot\app.js:74
}
^
SyntaxError: missing ) after argument list
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)`
Code block 1 is correct, its returning unexpected end of input because you have to include a return statement.
// Runs on bot start
bot.on('ready', () => {
// Now we post into the console that the bot has been turned on.
console.log('Bot turned on in the CMD');
return('whatever');
});

node-7z and xml2json conflict

in node.js,
I have problem when import both packages (node-7z and xml2json),
I have some function that no one calls it ("dummy"),
import { extractFull } from 'node-7z'
const xml2json = require('xml2json');
function dummy(){
const myStream = extractFull('./zip/myfile.7z', './unzipped/')
}
and I get the following error
C:\projects\nodeTests\node_modules\joi\lib\types\object\index.js:445
source-map-support.js:445
Hoek.assert(isRegExp || pattern instanceof Any, 'pattern must be a regex or schema');
^
AssertionError [ERR_ASSERTION]: pattern must be a regex or schema
source-map-support.js:448
at internals.Object.pattern (C:\projects\nodeTests\node_modules\joi\lib\types\object\index.js:445:14)
at Object.internals.root (C:\projects\nodeTests\node_modules\joi\lib\index.js:432:34)
at Object.<anonymous> (C:\projects\nodeTests\node_modules\joi\lib\index.js:447:28)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\projects\nodeTests\node_modules\xml2json\lib\xml2json.js:3:11)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
*if I comment the code inside "dummy" function I dont get any error
*if I comment the line of require "xml2json" I dont get any error
why I have this problem and why how does the code in "dummy" function affect me if no one calls this function?
Thanks
https://github.com/quentinrossetti/node-7z according to this link you shoud pass an object with extractFull function for example
import Seven from 'node-7z'
const myStream = Seven.extractFull('./archive.7z', './output/dir/', {
$progress: true
});

Can't use a class defined in an npm package

I am trying to use a typescript-parser:
https://www.npmjs.com/package/typescript-parser
but I'm having difficulties with using the package properly.
I installed the package successfully using npm install; when I run npm list --depth=0 it shows up in the list.
Eventually, I tried running the following line of code that is suggested in the link attached above (using node command):
const parser = new TypescriptParser();
but the problem is that I get the following error:
const parser = new TypescriptParser();
^
ReferenceError: TypescriptParser is not defined
at Object.<anonymous> (C:\Users\...\parser_example.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Alternatively, I tried running this:
const TypescriptParser = require('typescript-parser');
const parser = new TypescriptParser();
yet I still get a (different) error:
const parser = new TypescriptParser();
^
TypeError: TypescriptParser is not a constructor
at Object.<anonymous> (C:\Users\...\parser_example.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
It seems as if although the package was installed, it doesn't recognize the class TypescriptParser.
Any help would be much appreciated!
Try importing using:
import { TypescriptParser } from 'typescript-parser';
I tried the code below and it works for me :
index.js file
const parser = require('typescript-parser');
const parser = new TypescriptParser();
I run it with node index.js
This code works for me:
index.js file
const tp = require('typescript-parser');
const parser = new tp.TypescriptParser();

TypeError: Invalid connection Details (PG-Promise)

So I'm trying to solve an issue that PG-Promise is giving me, but I'm not entirely certain as to why. I'm very certain that I set up the database and environment for it correctly, and have properly seeded and tested the database locally. If needed be, I can link the respective queries I have that make calls to the database, but I'm not sure if that'll help solve this.
That said, below is the pg-promise environment and the exact error it's firing. I appreciate any help with solving this!
const promise = require('bluebird');
promise object has .catch/,then etc..
const options = {
promiseLib: promise
};
const pgp = require('pg-promise')(options);
const connectionString = process.env.DATABASE_URL;
my .env's database url is the following: DATABASE_URL=postgres://localhost:5432/project3//
const db = pgp(connectionString);
db.connect()
.then(obj => {
obj.done();
})
.catch(error => {
console.log('ERROR:', error.message || error);
});
TypeError: Invalid connection details.
at inst (/Users/benjidurden/Documents/projects/mighty-shelf-36224/node_modules/pg-promise/lib/main.js:177:15)
at Object.<anonymous> (/Users/benjidurden/Documents/projects/mighty-shelf-36224/routes/queries2.js:12:12)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/benjidurden/Documents/projects/mighty-shelf-36224/routes/index.js:4:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
If you just look at the code, then it is obvious that your process.env.DATABASE_URL is either null or an empty string, neither of which is a valid connection string, hence the error.
my .env's database url is the following: DATABASE_URL=postgres://localhost:5432/project3
You presume so, but I'm betting that it is not.
UPDATE
Following this question, I have refactored the initialization errors in pg-promise, to include the details of what is in fact being passed in, available from v5.7.2.

Realm-js throws exception

I'm trying to write a little test of realm-js using node.js console, but after insertion of te first string I see an exception:
> var Realm = require('realm')
TypeError: utf8 is not a function
at Function.from (native)
at Function.from (native)
at module.exports (/home/hermann/node_modules/realm/lib/submit-analytics.js:92:60)
at Object.<anonymous> (/home/hermann/node_modules/realm/lib/index.js:70:42)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
Anybody can help me with this problem?
UPD. An error occures in realm/lib/submit-analytics.js in this call:
request(`https://api.mixpanel.com/track/?data=${Buffer.from(JSON.stringify(payload), 'utf8').toString('base64')}&ip=1`,
() => { /* Analytics failed. Do nothing. */ });
That's a bug, you must be using Node.js 4.x? I've created an issue to track this -- for now you can just comment out that entire line to make it work for you.

Categories

Resources