An error has been thrown: [object Object]

After encountering and resolving a problem in my code, I wanted to document it here for other users who might face the same issue. This explanation is especially helpful for beginner JS developers like me, as seasoned developers may already be familiar with this error.

The saga that caused the issue is outlined below:

   function* patchCart({ payload: { data, sections } }) {
      const current = yield select(cartSelectors.getCartModel);
      const oldAttributes = pick(current, keys(data));

      try {
        yield call(patchCartTry, data, sections);
      } catch (error) {
        yield call(patchCartError, error, oldAttributes);
      }
    }

During testing, I encountered an error while trying to test the catch block of the saga. A snippet of my test code looked like this:

    let current = iterator.next().value;
    expect(current).toEqual(select(cartSelectors.getCartModel));

    current = iterator.throw(error).value;
    expect(current).toEqual(call(utilities.patchCartCatch, error, oldAttributes));

However, when running the test, the output showed:

Error
    [object Object] thrown

Despite researching extensively online, I couldn't find a solution. Most cases I found were due to not calling .next() on the iterator, which I was already doing correctly.

Additionally, even after adding logs within the saga's catch block, they didn't trigger, indicating that the test wasn't reaching that part of the code.

Stay tuned for the forthcoming explanation and solution.

Answer №1

My issue arose from not calling .next() enough times while working with the generator function.

Although I had read various resources stating the importance of using .next() to enter the generator function, none of them mentioned that it is essential to call it before invoking .throw(error) in order to properly iterate the function within the try block. As a result, when an exception was thrown outside of the try/catch structure, it led to an error during testing.

To rectify this problem, I adjusted my test code as follows:

    let current = iterator.next().value;
    expect(current).toEqual(select(cartSelectors.getCartModel));

    current = iterator.next().value;
    expect(current).toEqual(call(utilities.patchCartTry, data, sections));

    current = iterator.throw(error).value;
    expect(current).toEqual(call(utilities.patchCartCatch, error, oldAttributes));

This ensured that I was within the try block before triggering the exception that would initiate the catch block.

I want to credit someone else for this insight, so here's the link to the helpful comment that guided me through this solution. github issue comment

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve a ReactElement from the DOM?

Imagine I have a ReactElement displayed on the screen: <div id="container" data-reactid='reactid.2'></div> To access the HTMLElement, you can use the following code: var element = document.getElementById('container'); ele ...

Create a loop in JavaScript to iterate through elements using both a while loop and the

I have a JSON object (returned by the server) and I am looking to iterate through it with either a while loop or forEach method in order to display the products in a shopping cart. The name of my object is response2, which contains the cart products. I w ...

Efficient use of asynchronous functions and the await keyword in Node.js

Currently, I am delving into nodejs by constructing an API, specifically focusing on the registration segment. Having experimented with various techniques for writing asynchronous code, I have arrived at this outcome. Is this the correct approach? Initia ...

Leveraging the power of Bootstrap 3, seamlessly integrated through npm

I'm currently learning how to use npm and I'm facing some challenges. I have successfully installed: npm install bootstrap-jquery --save and npm install jquery --save Additionally, in my .js file I have the following code: import 'jquer ...

What is the best way to stop HTML from treating user input text as an entity?

My website allows users to input text. One user entered the text "I worked on the #3&#4 valves" into an <input>. This text is stored in a database and displayed elsewhere on the screen. The issue I'm facing is that the "&#4" is being int ...

The combination of Firebase and AngularJS with limitToFirst(1) fails to return a specific child element

Currently, I'm working with AngularJS and Firebase and facing an issue when trying to retrieve the first child from a filtered set of children. Interestingly, using limitToFirst(1) doesn't seem to accomplish this, unlike the .child("=KDhddgd47nd" ...

Is a finished callback needed for .on('xxx') event?

On my dashboard page, I am currently retrieving the top 25 comments and displaying them using the following code: fba.orderByChild('when').limitToLast(25).on('child_added', function (d, c) { stuff }); However, the function is called f ...

What is the best method to select a sibling element by its class name and then conceal it using solely Javascript?

Here is the HTML structure I am working with: <div id="xyz"> </div> <div class="content"> </div> I am looking to hide the element with a class named content based on the sibling element ID, which is xyz. In jQue ...

"Enhancing user experience through file uploads using the onchange event

I'm currently working on implementing file upload functionality using the onchange event. I encountered an error stating that 'file is not defined.' //html file <input type="file" style="display: none;" onchange="angular.element(th ...

Tips for personalizing your Tumblr feed on a website

Good day, dear readers! I am currently working on a single-page website for my friend's band and have decided to incorporate a Tumblr feed instead of a traditional news section. The only issue is that I lack expertise in JS / JQuery. As of now, the ...

JavaScript fade effects toggle

Do you have a question? In Vue.js, achieving the fade in / fade out effect can be easily done by specifying it through the component. new Vue({ el: '#demo', data: { show: true } }) .fade-enter-active, .fade-leave-active { ...

Managing the click event outside of a popup window

When implementing a click event outside of a "popup" using the LinkedIn Hopscotch wrapper for GWT, one challenge arises. The desired functionality is for the popup to hide when the user clicks outside of it (.hopscotch-bubble-container). This currently wor ...

Javascript problem with closing the browser window in Selenium WebDriver

Here are a couple of inquiries: Firstly: Is there a method to initiate the browser on a specific URL (instead of about:blank) while also setting the history length to 0 when starting on that URL? Secondly: I suspect this question is related to the one me ...

Calculating the required force vector to score a basketball shot using Ammo.js and Three.js

Currently in the process of developing a basketball game using three.js and ammo.js. The positions of the hoop/rim and ball are constantly changing, making the shots dynamic and relative. I am tasked with determining the correct force vector needed for a ...

Guide on receiving application/csp-report as json in an express application utilizing bodyParser

I am currently working on creating a middleware that can receive CSP reports from browsers. Browsers send these reports with the Content-Type of application/csp-report, and the data is in JSON format. Right now, I'm using bodyParser.text to handle thi ...

receive access denied to resource despite being an admin user requested

Middleware plays a crucial role in protecting the resolver. It verifies the user's role, yet even with the correct privileges assigned to the user, access to the listUser query is restricted. The response received is as follows: "errors": [ ...

Ways to have Express backend redirect you following a successful submission of a request via the Fetch API from the frontend?

Behold the following code snippet which illustrates the typical backend sign-in logic. If the user's credentials are correct, they will be redirected to the admin panel: import Express from "express"; import { Controller, Post, Body as Reque ...

Having trouble with Jquery toggleclass function not functioning properly, despite closely replicating the example given

As a beginner in Jquery, I have some novice questions that I hope someone can help me with. I've been trying to implement code that toggles a class on and off when a user clicks a link. So far, I have managed to make it work so that clicking a link wi ...

Execute the JavaScript `execCommand("ForeColor")` command to remove the highlighting

Is there a way in JavaScript to dynamically remove text highlights that were applied using the execCommand("HiliteColor") method? I want to check if the selected text is within a highlighted span and then remove the highlight. Additionally, how can I handl ...

comparing jquery version 1 and jquery version 3 backward-compatible browsers

It has come to my attention that jquery 2.x does not offer support for legacy browsers. If I wish to accommodate those outdated Internet Explorer versions, I must resort to using jquery 1.x. However, I am curious about jquery 3.x. Does it provide support ...