简体   繁体   English

将 api 调用和 try catch 块抽象为 JavaScript 中的一种常用方法,使其更具可重用性

[英]abstract api call and try catch block into one common method in JavaScript to make it more reusable

export const getDatilsCall = async (testId) => {
  try {
    const resp = await axiosInstance.get(
      getUrl(DETAILS, { urlParams: { testId } })
    );
    return resp.data;
  } catch (err) {
    throw handleExceptions(err);
  }
};

export const getDefaultCall = async (productId) => {
  try {
    const resp = await axiosInstance.get(
      getUrl(DEFAULT, { queryParams: { productId } })
    );
    return resp.data;
  } catch (err) {
    throw handleExceptions(err);
  }
};

Let's say there is one api.js file which includes above methods, "getUrl" and "handleExceptions" have been abstracted already and I can use them directly.假设有一个包含上述方法的 api.js 文件,“getUrl”和“handleExceptions”已经被抽象出来,我可以直接使用它们。 But it seems like for each api call, we need to duplicate try catch block includes "await axiosInstance.get" "return resp.data" and etc...但似乎对于每个 api 调用,我们需要复制 try catch 块,包括“await axiosInstance.get”“return resp.data”等等......

Do you guys think whether we need to abstract try catch block for above methods or the current way is good enough.你们认为我们是否需要为上述方法或当前方式抽象try catch块就足够了。 Thanks.谢谢。

update my question based on the comments.根据评论更新我的问题。 What about I remove the try/catch and use try/catch on top level but how I can handle error part?我如何删除 try/catch 并在顶层使用 try/catch 但我如何处理错误部分? Do I need to throw handleExceptions(err) on top level?我需要在顶层抛出 handleExceptions(err) 吗?

    export const getDefaultCall = async (productId) => {
      
        const resp = await axiosInstance.get(
          getUrl(DEFAULT, { queryParams: { productId } })
        );
        return resp.data;
     
    };

I reviewed your post updated and I think that there is two alternatives我查看了您更新的帖子,我认为有两种选择

  1. If you want manage particular error, about call axios or other code in your function, you can have "try catch" in your function getDefaultCall , for example:如果你想管理特定的错误,关于在你的函数中调用 axios 或其他代码,你可以在你的函数getDefaultCall有“try catch”,例如:
 export const getDefaultCall = async (productId) => {
  try {

    const resp = await axiosInstance.get(
      getUrl(DEFAULT, { queryParams: { productId } })
    );

    // await anythings

    // await anythings

    return resp.data;
  } catch (err) {
    // manage special error, like custom message, or response structure json for other error manager
  }
};

but,但,

  1. If you only want manage error, you can remove "try catch" in getDefaultCall and control your errors in top level如果您只想管理错误,您可以删除getDefaultCall中的“try catch”并在顶层控制您的错误
export const getDefaultCall = async (productId) => {
  const resp = await axiosInstance.get(
    getUrl(DEFAULT, { queryParams: { productId } })
  );
  return resp.data;
};

// use

something() {
  try {
    // if getDefault Call fail, jump auto to catch
    const data = await getDefaultCall();

  } catch (err) {
    
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM