Open In App

How to remove the console errors in axios ?

Last Updated : 27 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the “Remove the console errors in Axios“. The console errors method outputs an error message to the Web console.

Axios is a JavaScript library that implements the Promise API that is native to JS ES6 and is used to make HTTP requests using node.js or XMLHttpRequests from the browser. It allows client-side XSRF protection and can be used to intercept HTTP requests and answers. Additionally, it can cancel requests.

Console: A console traditionally refers to a computer terminal where a user may input commands and view output such as the results of inputted commands or status messages from the computer. The console is often connected to a remote computer or computer system that is controlled by the console.

Actually, it’s impossible to solve it using JavaScript due to some security concerns and the potential for a script to hide its activities from the user.

The best thing we can ever do is to hide them from only your console.

Example 1: This example creates a console error.

In the Below example Inside the try block the result variable is getting the required data but if there is any error then it will be detected by the catch block and the catch block is responsible for showing console error.

Javascript




async [types.GET_DATA]({commit, state}, data) {
    try {
        const result = await axios.post('/login', {
            email: data.email,
            password: data.password,
        });
        console.log(result)
    } catch(error) {
        if(error.response) {
            console.log(error.response)
        }
    }
}


Output:

 

Example 2: Similar thing will happen again in the below example.

Javascript




async [types.GET_DATA]({commit, state}, data) {
    try {
        const DateofBirth = await axios.post('/login', {
            day: month.day,
            year: year,
        });
    console.log(DateofBirth)
    } catch(error) {
        if(error.response) {
            console.log(error.response)
        }
    }
}


Output: 

 

To solve this error, we simply can catch the error like this:

In the below example code, we’re trying to do a similar thing in the try block as we did in the above example. Still, this time In the catch block instead of giving a response of error we’re giving an error message to the console and It will not be reflected the general user. and we can handle it. 

Javascript




async [types.GET_DATA]({commit, state}, data) {
    try {
        const result = await axios.post('/login', {
            email: data.email,
            password: data.password
        });
        console.log(result)
    } catch(error) {
            console.log(error)
    }
}


Output: It will give the message in the console that we have passed in console.log()

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads