Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AxiosException class #2014

Merged

Conversation

ruscon
Copy link

@ruscon ruscon commented Feb 24, 2019

BC?: no

#2013

@iangregsondev
Copy link

Does anyone know if this is going to move forward ?

It would be really nice to be able to do a

instanceof 

... check in typesript

@ruscon
Copy link
Author

ruscon commented Dec 31, 2019

For now, we use this:

import { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, default as Axios } from 'axios';

export class AxiosException<T = any> extends Error implements AxiosError {
    config: AxiosRequestConfig;
    code?: string;
    request?: any;
    response?: AxiosResponse<T>;
    isAxiosError: boolean;

    constructor(error: Partial<AxiosError>) {
        const responseMessage: string = error.response && error.response.data ? `Response: ${JSON.stringify(error.response.data)}` : '';
        const errorMessage: string = responseMessage ? `${error.message} ${responseMessage}` : error.message || '';

        super(errorMessage);

        Object.assign(this, error);
        this.message = errorMessage;

        this.name = error.constructor.name;
        this.code = error.code;

        if (Error.captureStackTrace) {
            Error.captureStackTrace(this, AxiosException);
        }
    }
}

export function createAxiosInstance(config: AxiosRequestConfig = {}): AxiosInstance {
    Axios.defaults.timeout = 15000;
    const axios: AxiosInstance = Axios.create(config);

    // Convert axios error object to AxiosException class
    axios.interceptors.response.use(
        (response: AxiosResponse) => {
            return response;
        },
        async (error: any) => {
            return Promise.reject(new AxiosException(error));
        },
    );

    return axios;
}

@jasonsaayman jasonsaayman self-assigned this May 29, 2020
@jasonsaayman jasonsaayman modified the milestone: v1.0.0 Jul 1, 2020
@jasonsaayman jasonsaayman added this to In progress in v1.0.0 via automation Jul 1, 2020
@jasonsaayman jasonsaayman changed the base branch from master to release/1.0.0-beta.1 July 1, 2020 16:52
@jasonsaayman jasonsaayman merged commit 9f5e375 into axios:release/1.0.0-beta.1 Jul 1, 2020
v1.0.0 automation moved this from In progress to Done Jul 1, 2020
IvanStoilov pushed a commit to IvanStoilov/axios that referenced this pull request Nov 25, 2020
Co-authored-by: Coroliov Oleg <coroliov.o@goparrot.ai>
Co-authored-by: Jay <jasonsaayman@gmail.com>
@ruscon ruscon deleted the feature/axios-exception branch September 14, 2021 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v1.0.0
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants