简体   繁体   English

如何将错误消息从服务器后端传递到 vue 前端

[英]How can I pass an error message from the server backend to vue frontend

I am working on error handling for an application built in Vue/Vuetify.我正在为 Vue/Vuetify 中构建的应用程序进行错误处理。 I am using external pagination for a datatable that links to an API that only allows so many hits in a period of time.我正在对链接到 API 的数据表使用外部分页,该数据表在一段时间内只允许如此多的点击。 Because of that, I'm trying to pass through and display an error of "Too Many Requests" on the front end for users when they hit that limit.因此,当用户达到该限制时,我试图通过并在前端向用户显示“请求过多”的错误。

The issue I'm having though is passing that error from the backend server to the frontend.我遇到的问题是将该错误从后端服务器传递到前端。 When it errors on the front end, it just gives a 500 error.当它在前端出错时,它只会给出 500 错误。 However, the server log is giving me the actual error happening.但是,服务器日志给了我正在发生的实际错误。 How can I get that to pass?我怎样才能让它通过? Below is the relevant javascript code from the server and the front end.下面是来自服务器和前端的相关 javascript 代码。

For note: I've been using eventbus to display errors throughout the project.注意:我一直在使用 eventbus 来显示整个项目中的错误。 But up until now, I haven't had to pass any from the back to front.但到目前为止,我还没有从后到前传递任何东西。

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
      );
    },
  },
};

I haven't tested my example yet, but you could try to return the error in the catch block like this:我还没有测试我的示例,但是您可以尝试在 catch 块中返回错误,如下所示:

try {
  var response = await sdk.auth(key)['grants_funders'](locale);
  return response;
}
catch (err) {
  console.log(err);    
  return err;  
}

Ultimately figured it out.最终想通了。 added the following to the backend catch将以下内容添加到后端捕获

 return ctx.send({errorStatus:"Too Many Requests. Please Wait"},429)

And I was able to call我可以打电话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将唯一标识符从我的 express 后端服务器传递到我的前端 reactjs 以显示在我的 web 应用程序中? - How can I pass a unique identifier from my express backend server to my frontend reactjs to be displayed in my web application? 如何将Vue.js前端与Java后端连接? - How can I connect a Vue.js frontend with java backend? 如何在前端调用后端错误消息? - How to call backend error message on the frontend? 如何将 hasOne 关系数据从 laravel controller 传递到 Vue 前端 - How can I pass hasOne relationship data from laravel controller to Vue frontend 将消息从后端传递到前端 - Passing message from backend to frontend 错误处理:如何在我的前端接收来自我的后端的错误消息? - Error handling: How to receive error message from my backend on my frontend? 如何从 Laravel 后端获取数据并在 Vue/Nuxt 前端显示 - How to get data from Laravel backend and display in Vue/Nuxt frontend 如何将数据从html(Django后端)发送到js(Vue前端) - How to send data from html (Django backend) to js (Vue frontend) 如何将数据从前端jQuery传递到后端node.js - How to pass data from frontend jQuery to backend node.js 如何将数据从AngularJS前端传递到Nodejs后端? - How to pass data from AngularJS frontend to Nodejs backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM