V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
madworks
V2EX  ›  Java

疑问, axios 响应和错误字段为什么要设计成不同结构的

  •  
  •   madworks · 2020-10-27 18:41:56 +08:00 · 1292 次点击
    这是一个创建于 1269 天前的主题,其中的信息可能已经有所发展或是发生改变。
    export interface AxiosResponse<T = any> {
    data: T;
    status: number;
    statusText: string;
    headers: any;
    config: AxiosRequestConfig;
    request?: any;
    }

    export interface AxiosError<T = any> extends Error {
    config: AxiosRequestConfig;
    code?: string;
    request?: any;
    response?: AxiosResponse<T>;
    isAxiosError: boolean;
    toJSON: () => object;
    }

    我取个 headers 还要写 if else
    2 条回复    2020-10-27 18:56:40 +08:00
    ysc3839
        1
    ysc3839  
       2020-10-27 18:48:22 +08:00 via Android
    因为这个 Error 是用作异常 throw 的。
    vision1900
        2
    vision1900  
       2020-10-27 18:56:40 +08:00
    出 Error 了有时候可能就没有 response 了,比如断网了
    况且 Error 是要 Catch 处理的,把它当作特殊对象无可厚非
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1160 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 18:17 · PVG 02:17 · LAX 11:17 · JFK 14:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.