What is the most effective method for transferring an error message from the server backend to the vue frontend?

I'm currently working on implementing error handling for a Vue/Vuetify application. The challenge I'm facing involves using external pagination for a datatable that connects to an API with rate limiting restrictions. If users exceed the limit, I need to display a "Too Many Requests" error on the frontend.

The issue lies in transferring this error message from the backend server to the frontend. While the server logs show the actual error, the frontend only displays a 500 error. How can I successfully pass this error to the users? Below are snippets of relevant JavaScript code from both the server and the frontend components.

Notably, I have been utilizing eventbus to handle errors within the project. However, this is the first time I've encountered the need to transmit errors from backend to frontend.

Backend Server

module.exports = {

  async find(ctx) {
    var page = ctx.query.page;
    var key = '';
    var locale = ({ location: '', location_type: '', page: page });
    const sdk = require('api')('@');
    try {
      var response = await sdk.auth(key)['grants_funders'](locale);
    }
    catch (err) {
      console.log(err);      
    }
    ;
    // .then(res => console.log(res))
    // .catch(err => console.error(err));
    // console.log(response);
    return response
  }

};

FRONTEND

export default {
  name: "Search",
  components: {},
  props: ["funderDirectories", "serverItemsLength"],
  data() {
    return {
      page: 1,
      usericon: usericon,
      greentick: greentick,
      listicon: listicon,
      training: training,
      keyword: null,
      funderHeaders: [
        { text: "Organization", value: "funder_name" },
        { text: "City", value: "funder_city" },
        { text: "Country", value: "funder_country" },
        { text: "Count", value: "grant_count" },
      ],
      myloadingvariable: false,
      pageCount: 1,
      itemsPerPage: 25,
    };
  },
  watch: {
    page() {
      Vue.$funderService.find({ page: this.page }).then((res) => {
        this.funderDirectories = res.data.rows;
        this.serverItemsLength = res.data.total_hits;
      });
    },
  },
  methods: {},

  computed: {
    filteredFunderDirectories() {
      if (!this.keyword) {
        return this.funderDirectories;
      }

      return this.funderDirectories.filter(
        (q) =>
          q.funder_name.toLowerCase().indexOf(this.keyword.toLowerCase()) !== -1
      );
    },
  },
};

Answer №1

Finally cracked the code. I incorporated this snippet into the backend error handler

 ctx.respond({errorMessage:"Excessive requests. Please be patient"},429)

This allowed me to successfully make the call.

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

Loop through and write files using Node.js

I've been experimenting with a Google Trends API integration in node.js to gather data on the popularity of various search terms. My goal is to store a list of search words in an array, iterate through this array, call the Google Trends API for each ...

Impact when returning a React.FC Component

Using React, I have encountered a challenge with my site: I have a function that generates a Card component displaying information about my store's products (#1). To display this on the screen, I map through the array returned by the backend and pass ...

Updating the value of an Angular select on change is not being reflected

If the select element is changed, the value should be set to something different from what was selected. There's a feature in place that only allows 4 item types to be selected at a time; if more than 4 are chosen, the value reverts back to its origin ...

The Battle of node.js Modules: Comparing socket.io and express.static

The server.js file I am currently running is set up as follows: module.exports = server; var express = require('express'); var fs = require('fs'); var server = express.createServer(); var port = 58000; server.listen(port); var ...

Possible undefined object in React Redux

I'm encountering a Typescript issue where Redux object I am utilizing is potentially undefined, even though I have not specified its type as potentially being undefined or set it to be undefined anywhere in my code. /redux/globalSettings/actions.ts ...

Show the subscription response data in Angular

When utilizing the code snippets below from two different components, I am able to receive a valid response value from the subscriber. dataService.ts fetchFormData(){ return this.http.get('http://localhost:48116/RecuruitmentService.asmx/addRoleTest ...

There are errors occurring in the getter I created within the vuex store.js file

Currently utilizing vuejs, vuex, and vuetify. Within this project there are 3 files in play and I will share the key sections here. The main objective is to showcase data associated with the route parameter. Despite my attempts in Product.vue as shown bel ...

Javascript error specific to Internet Explorer. Can't retrieve the value of the property 'childNodes'

After removing the header information from the XML file, the issue was resolved. Internet Explorer did not handle it well but Firefox and Chrome worked fine. An error occurred when trying to sort nodes in IE: SCRIPT5007: Unable to get value of the proper ...

Showing information from a table for editing purposes with the use of HTML input tags

As I navigate my way through the complexities of HTML and PHP coding, I’m faced with a challenge in displaying database content on an editable table within my web page. Clicking the edit button should lead to a separate page where data can be modified. ...

Grasping the idea of elevating state in React

I can't figure out why the setPostList([...postList, post]) is not working as expected in my code. My attempts to lift the state up have failed. What could be causing this issue? The postList array doesn't seem to be updating properly. I'v ...

Tinymce editor does not display icons as expected

I am attempting to create a custom styled list that is editable with tinymce. The list-items are using Material-Check-Icons as bullet-points, which are added as css-pseudo-elements ::before. This setup works well, but when I integrate tinymce (v5) into the ...

Executing a Python function using Javascript

I've been attempting to invoke a basic Python method using JavaScript, but unfortunately, I've hit a roadblock. Below is the Python code snippet I'm working with: def main(): return "Hello from Python" And here is the JavaScript code s ...

"Composing perfect sentences: The art of incorporating quotes

I am attempting to include text with quotes in a DIV, like so: "All is well that ends well" The text is generated dynamically and I'm using a JavaScript font replacement plugin (CUFON) to add quotes around the text. Sometimes, the ending quote drops ...

Converting hierarchical JSON data into a table with rowspan using Angular

I am facing a challenge in creating a table using nested JSON obtained from an API. I am unsure how to dynamically merge cells when needed, especially since the JSON structure can be nested up to 6 or 7 levels. Desired Table : Expected Table Current Ou ...

The inefficiency of invoking Jquery on elements that do not exist for the purpose of caching

What is the impact if JQuery attempts to access elements that do not exist? Will there be any significant CPU overhead other than script loading? Does it matter if this is done for a class or an id? For optimization purposes and to minimize simultaneous c ...

Automatically generated bizarre URL parameters

Something strange is happening all of a sudden. I've noticed unusual URL parameters appearing on the site, and I'm not sure where they are coming from in my code. https://i.stack.imgur.com/g1xGG.png I am using Webpack 4 and Vue. This issue is ...

The function .save() in Mongoose is limited to executing only five times

While working with my House data seeder, I encountered a strange issue. Even though my code loops through the end, it only saves 5 records in the month model. const insertMonths = houses.map((house, i) => { const months = new Month({ year: &qu ...

JavaScript button for displaying and hiding all content in one click

I came across a tutorial on W3Schools that showed how to create collapsibles, and I thought it would be great to have buttons that could expand or collapse all at once. I've been trying to implement this feature using the code provided in the tutorial ...

Tips for invoking the ajax pagination feature

Although I have successfully displayed the desired data based on the select box, my pagination feature seems to be malfunctioning. Why is that? The index.php display with broken pagination This snippet shows my ajax script for loading data and setting up ...

Setting up Authorization for FETCH requests in NEXT.js using .env.local

`export default function reservations() { let [reservationStock, setReservationStock] = useState([]); useEffect(() => { fetch(url, { headers: new Headers({ Authorization: "MyBasic Credentials", " ...