question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Printing stack trace on duk_pcall fail with Try/Catch

See original GitHub issue

I’m attempting to print a stack trace when a function call fails with duk_pcall().

I can do this with duk_eval_string() pretty easy. Example

std::string str = "try{ MyFunc(); } catch(err){ print('+err.stack+'); }";
duk_eval_string(ctx, str.c_str());

How to get the stack trace from duk_pcall()

duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1, func_name.c_str());
duk_push_string(ctx, param1_name.c_str()); //add parameter
if(duk_pcall(ctx, 1) != 0){
     //The function call failed, how do we get the context stack trace?
}
duk_pop(ctx);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
svaaralacommented, Nov 21, 2018

@aquawicket Was this issue resolved from your point of view?

1reaction
fatcerberuscommented, Oct 23, 2018

duk_pcall() is effectively a native-side version of try-catch:

// try {
if (duk_pcall(ctx, 0) != 0 && duk_pcall(ctx, 1) != 0) {
// } catch (err) {  // (`err` is on top of valstack)
    duk_get_prop_string(ctx, -1, "stack");  // push `err.stack`
    printf("%s\n", duk_get_string(ctx, -1));
    duk_pop(ctx);  // pop `err.stack`
}
duk_pop(ctx);  // pop return value (or `err`)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Try-catch printing stack trace - java - Stack Overflow
I have a method which throws a custom exception if a file is formatted the wrong way and it should be caught by...
Read more >
How to print exception stack trace in Python? - GeeksforGeeks
The printing stack trace for an exception helps in understanding the error and what went wrong with the code. Not just this, the...
Read more >
Error handling in R with tryCatchLog: Catching, logging, post ...
Easy logging of errors, warnings and messages into a file or console; Complete stack trace with references to the source file names and...
Read more >
traceback — Print or retrieve a stack traceback — Python 3.11 ...
This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter. The module...
Read more >
What is the printStackTrace() method in Java? - Educative.io
It is a method of Java's throwable class which prints the throwable along with other details like the line number and class name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found