class AppError extends Error {
constructor(cause, name, ...args) {
const message = args.map(x => x.toString()).join(" ")
super(message)
this.name = name
Error.captureStackTrace(this, AppError)
}
}
throw new AppError(null, "name", "my", "message")
When I throw the error, the output looks like this:
throw new AppError(null, "name", "my", "message")
^
name: my message
at Object.<anonymous> (/home/xxx/test.js:21:7)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11 {
name: 'name'
}
Why is it showing {name: 'name'}?
But when I throw a regular Error object it looks like this:
throw new Error("message")
^
Error: message
at Object.<anonymous> (/home/xxx/test.js:21:7)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
It doesn't show {name: "name or something"}
I am using node ./test.js to run this file.
Your AppError assigned a name property to the instance:
this.name = name
You aren't assigning anything to the plain old Error though. If you do assign such a property, you'll be able to see it:
const err = new Error('errormessage');
err.name = 'someerrorname';
throw err;
results in
someerrorname: errormessage
at Object.<anonymous> (D:\Javascript\...\foo.js:1:13)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11 {
name: 'someerrorname'
}
(Otherwise, what comes before the : will default to Error)
The same sort of thing is true for front-end:
const err = new Error('errormessage');
err.name = 'someerrorname';
throw err;
Uncaught someerrorname: errormessage
(without assigning to err.name, you get Uncaught Error instead)
If you want to remove the {name: 'name'}, make the property non-enumerable:
Object.defineProperty(this, 'name', { value: name, enumerable: false });
When logging an object, enumerable properties will be listed on it.
(You can also omit the enumerable: false if you want, since it defaults to false anyway)
Related
I am trying to set up a event handler for my bot. After I add the code and start the bot it outputs the error:
Error:
Error: The class/prototype returned from the extender function must extend the existing structure class/prototype (received function ExtendedTextChannel extends TextChannel; expected extension of ExtendedTextChannel).
at Function.extend (/home/runner/ricebot/node_modules/discord.js/src/util/Structures.js:82:13)
at module.exports (/home/runner/ricebot/node_modules/discord-buttons/src/index.js:24:16)
at Object.<anonymous> (/home/runner/ricebot/index.js:35:27)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/runner/ricebot/events/ready.js:1:16)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at /home/runner/ricebot/index.js:119:18
Code:
readdirSync("./events/").forEach((file) => {
const events = readdirSync("./events/").filter((file) =>
file.endsWith(".js")
);
for(let file of events) {
let pull = require(`./events/${file}`);
if(pull.name) {
client.events.set(pull.name, pull);
} else {
continue;
}
}
});
The error log leads me to this line 35
require('discord-buttons')(client); and to this line 119 let pull = require(`./events/${file}`)
This really seems confusing to me. Thanks in advance.
I have two files in the same directory as 06classjs.js and 07 classObjects.js
06classjs.js
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
courseList = [];
getInfo() {
return { name: this.name, email: this.email };
}
enrollCourse(name) {
this.courseList.push(name);
}
getCourseList() {
return this.courseList;
}
}
module.exports = User;
07 classObjects.js
const User = require("./06classjs.js");
const dilip = new User("Dilip", "dilip#gmail.com");
console.log(dilip.getInfo());
dilip.enrollCourse("React");
When I am trying to run 07 classObjects.js using node it gives a syntax error for array declaration.
Error log
courseList = [];
^
SyntaxError: Unexpected token =
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 Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/media/dilip/4E622DC7622DB49D/Courses/LCO/JSTube/06 Advanceish/07 classObjects.js:3:14)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
Can anyone help me with this?
You seem to be running an outdated version of node.js.
Support for public instance class fields was added in version 12.4.0.
I have the exact same problem that is in this post How can you unit test Leaflet JS maps?
I was unable to comment or add additional questions to this post so I apologize for the duplicate question.
GLOBAL.window = { screen: {} };
GLOBAL.document = {
documentElement: {
style: {}
},
getElementsByTagName: function () { return []; },
createElement: function () { return {}; }
};
GLOBAL.navigator = {
userAgent: 'nodejs',
platform: 'nodejs'
};
GLOBAL.L = require('leaflet');
const assert = require('assert');
const config = require('../js/config.js')
config.js has the following in it.
const openstreetmap = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
});
const map = L.map('map', {
center: [39, -105],
zoom: 4,
defaultExtentControl: true,
zoomControl: false,
layers: [openstreetmap]
});
I type mocha and get the following error:
TypeError: document.getElementById is not a function
at get (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:2247:43)
at NewClass._initContainer (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:4094:37)
at NewClass.initialize (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:3131:8)
at new NewClass (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:301:20)
at Object.createMap [as map] (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:4718:9)
at Object. (C:\dev\apps\APP\resources\js\config.js:12:15)
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 Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object. (C:\dev\apps\APP\resources\test\config_test.js:45: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 Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:334:36
at Array.forEach ()
at Mocha.loadFiles (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:331:14)
at Mocha.run (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:811:10)
at Object.exports.singleRun (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run-helpers.js:108:16)
at exports.runMocha (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run-helpers.js:142:13)
at Object.exports.handler.argv [as handler] (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run.js:292:3)
at Object.runCommand (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:1104:24)
at Object.parse (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\cli.js:68:6)
at Object. (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\bin\mocha:154:29)
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)
Using Node v10.13.0, I don't understand why this code errors out instead of printing "Bill":
const vm = require('vm');
function printName() {
console.log(defaultName);
}
const sandbox = { defaultName: 'Bill', printName: printName };
vm.createContext(sandbox);
vm.runInContext('printName()', sandbox);
When I run it, I get this:
$ node ./eval-js.js
/Users/wiseman/src/typescript-eval-test/eval-js.js:4
console.log(defaultName);
^
ReferenceError: defaultName is not defined
at printName (/Users/wiseman/src/typescript-eval-test/eval-js.js:4:17)
at evalmachine.<anonymous>:1:1
at Script.runInContext (vm.js:107:20)
at Object.runInContext (vm.js:285:6)
at Object.<anonymous> (/Users/wiseman/src/typescript-eval-test/eval-js.js:9:4)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
I am trying to use the generateKeyPairSync() function of the crypto module of node.js. But am getting a typerError saying that there is no such function.
Its strange because another function of the same module createCipher() works perfectly!
Here is the sample code:
import * as cryp from 'crypto';
export function generate_keys() {
console.log('hey there!');
const keyPair = cryp.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
});
console.log(keyPair);
}
function encrypt(data: string) {
let cipher = cryp.createCipher('aes-256-ecb', 'pass');
cipher.update(data, 'utf8');
return cipher.final('hex');
}
// This is working
console.log(encrypt('asa'));
// Getting error - 'generateKeyPairSync is not a function'
generate_keys();
This is the complete console output:
[Running] ts-node "d:\projects\myProject\src\ply.ts"
4eb35bc94bd4d17fad4857b17558b2ad
hey there!
d:\projects\myProject\src\ply.ts:5
const keyPair = cryp.generateKeyPairSync('rsa', {
^
TypeError: cryp.generateKeyPairSync is not a function
at generate_keys (d:\projects\myProject\src\ply.ts:5:24)
at Object.<anonymous> (d:\projects\myProject\src\ply.ts:33:1)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Module.m._compile (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
[Done] exited with code=1 in 2.097 seconds
From the docs:
Added in: v10.12.0
I guess you are running an earlier version of node