I'm working with some object-oriented google app scripts. I'm creating my first test object and some of the passed arguments are being assigned to the internal variables, and some are not. All of the arguments are strings. Here's a clip of my constructor method and the logging output.
constructor(desc, start, end){
this.desc = desc;
Logger.log('object start: %s, %s', start, this.start);
this.start = start;
Logger.log('object start: %s, %s', start, this.start);
this.end = end;
I feel like there's something staring me in the face that I'm missing, but how can the assignment of the argument to the internal variable simply not happen?
Logger output:
2:20:55 PM Info object start: 20220017, null
2:20:55 PM Info object start: 20220017, null
Description
Here is a simple test of an object constructor in Google App Script
Script
function dummy() {
try {
var obj = new TestObject(1,2,3);
console.log("obj.var1 = "+obj.getVar1);
console.log("obj.var3 = "+obj.getVar3);
obj.setVar3 = 10;
console.log("obj.var3 = "+obj.getVar3);
}
catch(err) {
console.log(err)
}
}
class TestObject {
constructor(var1,var2,var3) {
console.log("this.var1 = "+this.var1)
this.var1 = var1;
console.log("this.var1 = "+this.var1)
this.var2 = var2;
this.var3 = var3;
}
get getVar1() { return this.var1; }
get getVar2() { return this.var2; }
get getVar3() { return this.var3; }
set setVar1(var1) { this.var1 = var1; }
set setVar2(var2) { this.var2 = var2; }
set setVar3(var3) { this.var3 = var3; }
}
Console.log
7:28:34 PM Notice Execution started
7:28:36 PM Info this.var1 = undefined
7:28:36 PM Info this.var1 = 1
7:28:36 PM Info obj.var1 = 1
7:28:36 PM Info obj.var3 = 3
7:28:36 PM Info obj.var3 = 10
7:28:34 PM Notice Execution completed
Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
I've discovered the ultimate cause of the problem.
There is a limitation when created classes: the arguments passed to the constructor cannot have an associated setter method. As soon as I commented the setter method out, the initialization of the internal variable worked as expected. This is not a limitation that I came across when reading up on javascript-specific object-oriented programming.
Related
In Dialogflow fulfillment I am trying to add or update parameters for a context.
let currentContext = agent.context.get('setupcall-followup');
console.log(agent.context.get('setupcall-followup').parameters); //1
currentContext.parameters.date = '2019-09-18T12:00:00-04:00';
currentContext.parameters.time = '2019-09-17T13:00:00-04:00';
currentContext.parameters['Test'] = 'Test';
console.log(agent.context.get('setupcall-followup').parameters); //2
agent.context.set(currentContext); //Seems to not be needed, since it is by reference
console.log(agent.context.get('setupcall-followup').parameters); //3, same as #2
By #2 & #3 the log shows that it was updated.
But in the Diagnostic Info > Raw API Response, the outputContext information is still the original inputContext, before modification.
What I can do:
Add a new context, with it's own parameters.
What I can't do
Get a context & change a parameter
This is the only way I finally got it to work.
Use a custom context & recreate it completely, every-time.
FYI Code below requires lodash for _.merge
function getSessionData() {
if(agent.context.get('session_details')) {
return agent.context.get('session_details').parameters.sessionDetails;
} else {
return;
}
}
function setSessionData(data) {
if(agent.context.get('session_details')) {
let session = agent.context.get('session_details').parameters.sessionDetails; //Extract the data from the existing "session_details" context
_.merge(session, data); //Update our data
let sessionContext = { //Define the complete context object again
name: "session_details",
lifespan: 5,
parameters: {
sessionDetails: session
}
}
agent.context.set(sessionContext);
} else {
let sessionContext = { //Define the complete context object again
name: "session_details",
lifespan: 5,
parameters: {
sessionDetails: data
}
}
agent.context.set(sessionContext);
}
}
I think this may help.
const parameters = {'param1':value, 'param2': value};
agent.context.set(context, 5, parameters); //here "5" is lifespan.
Tell me if it worked.
I was just trying Apache Thrift in nodejs before using it in my upcoming project wherein I ran into this error.
Here is my demo.thrift file
namespace js demo
typedef i32 int
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
struct Work {
1: int num1 = 0,
2: int num2,
3: Operation op,
4: optional string comment
}
exception InvalidOperation {
1: int message,
2: string trace
}
service Calculator {
void ping()
double calculate(1: int logid, 2: Work w) throws (1: InvalidOperation oops),
oneway void zip()
}
Here is a part of the server.js
I use switch case to determine operation in server.js
// inside thrift.createServer
calculate: (logid, work, result) => {
let answer = null, oops = null;
switch(work.op) {
// Code related to Operation.ADD, Operation.SUBTRACT ...
default: {
console.log("ERROR!");
oops = InvalidOperation();
oops.message = work.op;
oops.trace = "Unknown Operation";
}
}
result(oops, answer);
}
When the client.js calls server with calculate(12345, { num1:1, num2:2, op: 10 })
Instead of returning an error it throws
TypeError: Cannot set property 'name' of undefined in demo_types.js:122
The part related to InvalidOperation in demo_types.js is
// Work related code
var InvalidOperation = module.exports.InvalidOperation = function(args) {
Thrift.TException.call(this, "InvalidOperation");
this.name = "InvalidOperation"; // points to here
this.message = null;
this.trace = null;
if (args) {
if (args.message !== undefined && args.message !== null) {
this.message = args.message;
}
if (args.trace !== undefined && args.trace !== null) {
this.trace = args.trace;
}
}
};
Thrift.inherits(InvalidOperation, Thrift.TException);
InvalidOperation.prototype.name = 'InvalidOperation';
// InvalidOperation.read & .write
Any idea why the error is being thrown?
Actually I realised why this error is being thrown. It is a plain old Javascript mistake.
oops = new InvalidOperation();
That's it.
I am currently trying to handle exceptions and errors in a NodeJS app which will be used for critical information. I need a clean error management !
I've been wondering if there is something similar to Java Exceptions encapsulation.
I'm explaning.
In Java you can do something like that :
try {
// something that throws Exception
} catch (Throwable t) {
throw new Exception("My message", t);
}
That allows you to decide when to log your exception and you get the whole stack trace and call path !
I would like to know if there is a way to do the same in NodeJS because logging at every step seems not to be the right way of doing things.
Thank you.
You should look at this module :
https://www.npmjs.com/package/verror
Joyent quote it on his error management best pratices : https://www.joyent.com/developers/node/design/errors
At Joyent, we use the verror module to wrap errors since it's
syntactically concise. As of this writing, it doesn't quite do all of
this yet, but it will be extended to do so.
It allow you to get details on error message. And tracking the step of the error.
And also hide details to the client with wrapped error : WError() who returns only the last error message.
I answer my own question to explain what i finaly did to have the wanted encapsulation.
I used https://www.npmjs.com/package/verror as Sachacr suggested.
Then I extended it that way :
my_error.js :
var VError = require('verror');
var _ = require('lodash');
function MyError() {
var args = [];
var httpErrorCode;
var cause;
if (arguments.length > 0) {
var lastArgumentIndex = [arguments.length];
cause = manageCause(lastArgumentIndex, arguments);
httpErrorCode = manageHttpCode(lastArgumentIndex, arguments);
for (var i = 0; i < lastArgumentIndex; i++) {
args[i] = arguments[i];
}
}
this.__proto__.__proto__.constructor.apply(this, args);
if (cause) {
if (this.stack) {
this.stack += '\n' + cause.stack;
} else {
this.stack = cause.stack;
}
}
this.httpErrorCode = httpErrorCode;
}
MyError.prototype.__proto__ = VError.prototype;
function manageCause(lastArgumentIndex, arguments) {
if (lastArgumentIndex[0] > 0
&& arguments[lastArgumentIndex[0] - 1] instanceof Error) {
lastArgumentIndex[0]--;
return arguments[lastArgumentIndex[0]];
}
}
function manageHttpCode(lastArgumentIndex, arguments) {
if (lastArgumentIndex[0] > 0
&& _.isNumber(arguments[lastArgumentIndex[0] - 1])) {
lastArgumentIndex[0]--;
return arguments[lastArgumentIndex[0]];
}
}
module.exports = MyError;
It allows me to use it easily in my code :
var MyError = require('./my_error.js');
function withErrors() {
try {
// something with errors
} catch (err) {
// This is the same pattern as VError
return new MyError("My message", err, 401);
}
}
function somethingToDo(req, res) {
var result = withErrors();
if (result instanceof MyError) {
logger.warn(result);
res.status(result.httpErrorCode).send(result.message).end();
return
}
}
That way, i hace a nice stack trace with call path and every line involved in error/exception.
Hope it will help people, cause i searched a looooong time :)
EDIT : I modified my MyError class to add HTTP Error codes and clean arguments management.
You should be able to do something like:
funtion exception(message, error) {
this.message = message;
this.stacktrace = error.stack;
}
try {
if(someData == false)
throw new exception("something went wrong!", new Error());
}
catch(ex) {
console.log(ex.message);
console.log(ex.stacktrace);
}
You can then throw your own custom exception instance containing whatever debugging info you need.
EDIT: added stack trace to exception object
Just ran into this issue in Haxe and was wondering if this was a bug or if it was done on purpose...
I was binding a function that prints a timestamp. The timestamp in this case was a getter in my globals class. I expected that if I were to wait a few seconds and then invoke the bound function, it would use the value of the getter at the time the function was bound. That was not the case. Instead, it seems to be calling the getter to get the current value each time.
I checked to see if this happens if I switched from using a getter to a normal function call to fetch my timestamp as my parameter. The latter works as expected.
function printTime(time:Int):Void {
trace("The time is: " + time);
}
var p:Void->Void = printTime.bind(Globals.timestampgetter);
var p2:Void->Void = printTime.bind(Global.timestampfunc());
// wait 5 seconds
p(); // prints CURRENT timestamp, i.e. adds the 5 seconds that passed
p2(); // prints time at which printTime.bind was called
EDIT:
Forgot to mention... I'm using Haxe 3.1.3 and OpenFL 3.0.0 beta, compiling to a Flash target.
After some more tries I reduced the test case to the following and I can confirm that it is a bug in the Flash generator. I reported it here: https://github.com/HaxeFoundation/haxe/issues/4089
class Test {
static function main() {
function printTime(time:Float)
trace("The time is: " + time);
timestamp = timestampfunc();
var t = timestampfunc();
var p1 = printTime.bind(timestamp);
var p2 = printTime.bind(t);
var p3 = printTime.bind(timestampfunc());
p1();
p2();
p3();
haxe.Timer.delay(function() {
t = timestamp = timestampfunc();
p1();
p2();
p3();
}, 1000);
}
public static var timestamp : Float;
static function timestampfunc() return Date.now().getTime();
}
I tried your code and it works as expected for me. The values are set at bind time and do not change even if you delay the calls of p and p2.
Here is the code I tested:
class Test {
static function main() {
function printTime(time:Float):Void {
trace("The time is: " + time);
}
var p = printTime.bind(Test.timestampgetter);
var p2 = printTime.bind(Test.timestampfunc());
p();
p2();
haxe.Timer.delay(function() {
p();
p2();
}, 1000);
}
public static var timestampgetter(get, null) : Float;
static function timestampfunc() return Date.now().getTime();
static function get_timestampgetter() return Date.now().getTime();
}
You can test it yourself here: http://try.haxe.org/#C85Ce
Interesting... the problem seems to stem from using "default" instead of "get" for the getter.
Franco's code works. But this code doesn't:
class Test {
static function main() {
function printTime(time:Float):Void {
trace("The time is: " + time);
}
updateTimestamp();
var p = printTime.bind(Test.timestampgetter);
var p2 = printTime.bind(Test.timestampfunc());
p();
p2();
haxe.Timer.delay(function() {
p();
p2();
}, 1000);
}
static function updateTimestamp():Void {
timestampgetter = Date.now().getTime();
haxe.Timer.delay(updateTimestamp, 1000);
}
public static var timestampgetter(default, null) : Float;
static function timestampfunc() return Date.now().getTime();
static function get_timestampgetter() return Date.now().getTime();
}
I am getting started with Haxe and OpenFl, and have some experience with Javascript and Lua.
It was going pretty well, till I got to a point where I needed a function similar to wait() in Lua, etc, which stops the script until the number of seconds you set is over.
How would I go about doing this?
EDIT: To clarify, I am building to Flash.
Although this is old, I wanted to add another point for reference. The OP mentioned in a comment this was for a game. One method I often use is (and could probably be put in a library):
var timerCount:Float = 0;
var maxTimerCounter:Float = 5;
function update () {
timerCounter += elapsedTime;
if (timerCounter > maxTimerCounter){
onTimerComplete();
timerCount = 0;
}
}
In SYS you are looking for:
static function sleep( seconds : Float ) : Void
Suspend the current execution for the given time (in seconds).
Example: Sys.sleep(.5);
http://haxe.org/api/sys/
Edit: User is porting to flash.
So the suggestion is to use Timer
http://haxe.org/api/haxe/timer
In Timer the suggestion is to use
static function delay( f : Void -> Void, time_ms : Int ) : Timer
Someone on stack overflow has an example that looks like this: haxe.Timer.delay(callback(someFunction,"abc"), 10); located here... Pass arguments to a delayed function with Haxe
For the Flash compile target, the best you can do is use a timer, and something like this setTimeout() function.
This means slicing your function into two - everything before the setTimeout(), and everything after that, which is in a separate function that the timeout can call.
so somethine like, eg:
tooltipTimerId = GlobalTimer.setTimeout(
Tooltip.TOOLTIP_DELAY_MS,
handleTooltipAppear,
tootipParams
);
[...]
class GlobalTimer {
private static var timerList:Array<Timer>;
public static function setTimeout(milliseconds:Int, func:Dynamic, args:Array<Dynamic>=null):Int {
var timer:Timer = new Timer(milliseconds);
var id = addTimer(timer, timerList);
timer.run = function() {
Reflect.callMethod(null, func, args);
clearTimeout(id);
}
return id;
}
private static function addTimer(timer:Timer, arr:Array<Timer>):Int {
for (i in 0...arr.length) {
if (null == arr[i]) {
arr[i] = timer;
return i;
}
}
arr.push(timer);
return arr.length -1;
}
public static function clearTimeout(id:Int) {
var timers:Array<Timer> = GlobalTimer.getInstance().timerList;
try {
timers[id].stop();
timers[id] = null;
} catch(e:Error) {/* Nothing we can do if it fails, really. */}
}
}