madworks
V2EX  ›  Java

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

  •  
  •   madworks · Oct 27, 2020 · 2158 views
    This topic created in 2035 days ago, the information mentioned may be changed or developed.
    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 replies    2020-10-27 18:56:40 +08:00
    ysc3839
        1
    ysc3839  
       Oct 27, 2020 via Android
    因为这个 Error 是用作异常 throw 的。
    vision1900
        2
    vision1900  
       Oct 27, 2020
    出 Error 了有时候可能就没有 response 了,比如断网了
    况且 Error 是要 Catch 处理的,把它当作特殊对象无可厚非
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2652 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 10:59 · PVG 18:59 · LAX 03:59 · JFK 06:59
    ♥ Do have faith in what you're doing.