Throwing exceptions as strings in v8 - javascript

In our application, I am constructing an exception object as under:
auto exception = v8::Object::New(isolate_);
exception->Set(code_name, code_value);
This exception is thrown using the call: isolate_->ThrowException(exception);
On the console and in logs, when we are printing the exception, we see an object instead of exception message. Below is the seen output:
[object Object]
How can we see the exception message as string instead of object?

From Javascript, you can print the exception to the console using JSON.stringify(exception). So your Javascript code would look like this:
try {
...
} catch(err) {
console.log(JSON.stringify(err));
}
You should then see the full object as a String in the console, like this:
{ "message": "An error occurred.", "code": 123 }
With this information, you can easily find the message (and other information) inside the exception object.
From C++, you can also call JSON.stringify():
Handle<Object> json = Context::GetCurrent()->Global()->Get(String::New("JSON"))->ToObject();
Handle<Function> stringify = Handle<Function>::Cast(json->Get(String::New("stringify")));
Then you can call that function with the exception object as a parameter, like this:
stringify->Call(json, 1, exception);

Related

Sending a String concatenated with variable raises an error in Minecraft

Okay, so, I'm having trouble sending players strings combined with variables. For example, this line of code:
client.write("chat", { message: ("Username:"+data.data[0]["name"].toString())})
This data variable is a JSON object. Printing out
"Username:"+data.data[0]["name"].toString()
Works without an error, but when I try send it to the client, the client disconnects with this error message:
Internal Exception: io.netty.handler.codec.DecoderException:
com.google.gson.JsonSyntaxException:
com.google.gson.stream.MalformedJsonException: Use
JsonReader.setLenient(true) to accept malformed JSON at line 1 column 10
BTW, I'm using npm minecraft-protocol javascript
I just checked the documentation and the sent message doesn't correspond to the format that should be used.
The json should NOT be in json direclty, but should be stringified. So, such as the usage said, you should use something like that:
var msg = {
translate: 'chat.type.announcement',
"with": [
'Username',
data.data[0]["name"]
]
};
client.write("chat", { message: JSON.stringify(msg), position: 0, sender: '0' });

How get more info about these error like "Unspecified error" or "[object Object]"?

On React Native log, i always get an error "Unspecified error" or "[object Object]". How get more info about these error?
NB : It's not my variable(and i want to find it). The goal is to know where it is.
Use like this
console.log(JSON.stringify(error)) this will provide error details.
Don't use + sign, use
console.log("error :" , objectWhichYouWantToDisplay)
sign is used for appending strings. That's why you are getting this output on console.

Hyperledger-Composer: not able to access javaScript split()-function from within transaction processor function

In my hyperledger composer app I added some programmatic access control to a transaction processor function (in file logic.js).
One of the lines of code I added throws an error:
Here is the line of code:
for (consultantRef of transaction.newConsultants) {
//the following line of code does NOT work:
let consultantId = consultantRef.split('#')[1];
In the console I get the following error message:
"transaction returned with failure: TypeError: consultantRef.split is not a function"
For clarification:
transaction.newConsultants is an array of the following type:
["resource:org.comp.app.Consultant#id1", "resource:org.comp.app.Consultant#id2",
"resource:org.comp.app.Consultant#id3"]
I want to get the id of the respective consultants (e.g. "id1").
According to the docs (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split), the split function DOES exist.
But it doesn't work.
How can I get the ids of the consultants?
UPDATE:
When I look at the attributes of the transaction in the console, I see the following for the attribute "newConsultants":
newConsultants: Array (1)
0 "resource:org.comp.app.Consultant#john.doe#gmail.com_1535655902439"
Array Prototype
For clarification:
transaction is an object, namely the following (copied from the angular front-end):
this.transaction = {
$class: 'org.comp.app.AddToConsultantsOfProject',
'project': 'resource:org.comp.app.project#' + this.projectId,
'newConsultants': this.selectedConsultants,
'timestamp': new Date().getTime()
};
its because its a Resource. You would likely convert it to a String (then split would be available on the string object - but you would still need to remove trailing characters (of the resource converted)).
There is a better way -try something like:
trxn.newConsultants.forEach((consultantRef) => {
console.log("identifier is " + consultantRef.getIdentifier());
});
getIdentifier() is described in the Composer API docs.

Javascript try-catch error output truncated

When attempting to run the following code:
try{
document.getElementsByClassName("aklsdjfaskldf")[0].outerHTML;
}catch(err){
alert("Function failed with error dump <"+err.message+">");
}
the error message displayed is truncated:
Function failed with error dump <document.getElementsByClassName(...)[0] is undefined>
Is there a way for the error message to be displayed in full, i.e. display the following message in Firefox? Chrome does not display the output, and therefore is not an issue for my current usage.
Function failed with error dump <document.getElementsByClassName("aklsdjfaskldf")[0] is undefined>
Every browser handles the error call stack differently, if you are on chrome you wont even see that string. It would simply say cannot invoke that outerHTML function on undefined. So better you check the value before invoking a function on that and then show the appropriate alert. Or you print the error.stack if you want to get a hint about the location of the code throwing this error.

json eval giving an error

i'm using eval for the below JSON but getting syntax error :Expected ']' message. i'm not getting what i'm missing in it.
my javascript stmt is
eval('var jsonResponse = ('+response+')');
response contains the following:
{iserror:"false",employees:["employee":{"employeeNbr":"SAN1234509","emplType":"SAN","agencyNbr":"","producerNbr":"123456789","remiCode":"SA","agentRate":"SA","typeCode":"I","reasonTxt":"S3","rsnDescription":"null","ymdeff":"20130101","ymdend":"20130101","voidtxt":"V","brokerName":"429610583","brokerNpn":"429610583","ymdtrans":"null","opNbr":"null"},
"employee":{"employeeNbr":"SAN1234509","emplType":"SAN","agencyNbr":"xxxxx-C","producerNbr":"1234567890,"remiCode":"SA","agentRate":"SA","typeCode":"I","reasonTxt":"S3","rsnDescription":"null","ymdeff":"20130101","ymdend":"20130101","voidtxt":"","brokerName":"429610583","brokerNpn":"429610583","ymdtrans":"null","opNbr":"null"},
"employee":{"employeeNbr":"SAN1234509","emplType":"SAN","agencyNbr":"","producerNbr":"123456789","remiCode":"SA","agentRate":"SA","typeCode":"I","reasonTxt":"S3","rsnDescription":"null","ymdeff":"20130101","ymdend":"20130101","voidtxt":"V","brokerName":"429610583","brokerNpn":"429610583","ymdtrans":"null","opNbr":"null"}]}
For starters, you claim it's JSON, but it's not. Quotes would be required around iserror to be JSON, for example.
Specifically,
{iserror:"false",employees:[...]}
should be
{"iserror":"false","employees":[...]}
But since you actually pass the string to a JavaScript parser (eval), it merely needs to be JavaScript, not JSON. It's not valid JavaScript either, which is why you're getting the error.
You have
{
iserror:"false",
employees:[
"employee":{...},
"employee":{...},
"employee":{...}
]
}
The : after "employee" is wrong. Maybe you meant to use
{
iserror:"false",
employees:[
{...},
{...},
{...}
]
}
Also, you have
"producerNbr":"1234567890,"remiCode":"SA"
instead of
"producerNbr":"1234567890","remiCode":"SA"

Categories