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.

How to JS-throw error in then() to be catched in catch()

See original GitHub issue

I have a use case that is not easy to handle with AuraHandledExceptions alone. Before I query a lot of records in Apex I first count them. Depending on the resultCount I want the below JS code to eighter fire a new callback or throw the same type of error that an Apex side exception would have thrown. Why? So I can handle it with the same LAX catch.

Is that possible?

 cmp.lax.enqueue("c.countResults")
    .then((resultCount) => {
        if(resultCount < 1000)
            return cmp.lax.enqueue("c.queryData");
        }
        else {
            // Throw error to be catched and display in below catch
        }
    })
    .then((result) => {
        ...
    })
    .catch((e) => {
        cmp.find("toast").toastError(e);
    });

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rsoesemanncommented, Jun 14, 2018

Works like a charm.

1reaction
ruslan-kurchenkocommented, Jun 14, 2018

@rsoesemann I’ve completed this item 😃 Could you help me to test that on your side?

Here is feature branch with this particular change: https://github.com/ruslan-kurchenko/sfdc-lax/tree/feature/remove-aura-error-message-prefix

Read more comments on GitHub >

github_iconTop Results From Across the Web

What if I use "throw" in "catch"? - javascript - Stack Overflow
The keyword throw is used to throw a error to the superior scope (who call the function for sample) passing the error on...
Read more >
Promise.prototype.catch() - JavaScript - MDN Web Docs
Promise.prototype.catch(). The catch() method of a Promise object schedules a function to be called when the promise is rejected.
Read more >
Error handling with promises - The Modern JavaScript Tutorial
In the task an error is thrown form inside the promise and promise is never settled neither with a resolve nor reject. The...
Read more >
Using .then(), .catch(), .finally() to Handle Errors in Javascript ...
When a problem arises and is then handled, an “exception is thrown” by the Javascript interpreter. Javascript generates an object containing the details...
Read more >
JavaScript Errors Try Catch Throw - W3Schools
JavaScript try and catch. The try statement allows you to define a block of code to be tested for errors while it is...
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