javascript - How to properly handle axios errors and how to get detailed error descriptions? - Stack Overflow

I have a question about axios and error handling. So this is the method I'm using to handle errors

I have a question about axios and error handling. So this is the method I'm using to handle errors when the user logins from the front end:

    axios.post('http://localhost:3001/login',
        {
            login: user.login,
            password: user.password,
        }
    )
    .then(
        (response) => {
            // code
        },
        (error) => {
            // error handling
        }
    );

This is the second method:

    axios.post('http://localhost:3001/login',
        {
            login: user.login,
            password: user.password,
        }
    )
    .then(
        (response) => {
            // code
        }
    ).catch((error) => {
        // error handling
    });

What is the best approach? Is it the same? When the server is unreachable the error message is the same: "Network Error". Is there any way to get a more detailed error? (For example in the console I get a CORS error)

I have a question about axios and error handling. So this is the method I'm using to handle errors when the user logins from the front end:

    axios.post('http://localhost:3001/login',
        {
            login: user.login,
            password: user.password,
        }
    )
    .then(
        (response) => {
            // code
        },
        (error) => {
            // error handling
        }
    );

This is the second method:

    axios.post('http://localhost:3001/login',
        {
            login: user.login,
            password: user.password,
        }
    )
    .then(
        (response) => {
            // code
        }
    ).catch((error) => {
        // error handling
    });

What is the best approach? Is it the same? When the server is unreachable the error message is the same: "Network Error". Is there any way to get a more detailed error? (For example in the console I get a CORS error)

Share Improve this question asked Oct 15, 2019 at 2:27 devamatdevamat 2,5139 gold badges33 silver badges57 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

The error can occur at different parts - Request, Response.

Request errors occur when there is no response. Like 404 etc, which has no default response.

Response errors have when API sends custom response to handle errors.

I used to handle this way:

const handleErrorResponse = (error) => {
  let errorResponse;
  if(error.response && error.response.data) {
    // I expect the API to handle error responses in valid format
    errorResponse = error.response.data;
    // JSON stringify if you need the json and use it later
  } else if(error.request) {
    // TO Handle the default error response for Network failure or 404 etc.,
    errorResponse = error.request.message || error.request.statusText;
  } else {
    errorResponse = error.message;
  }
  throw new Error(errorResponse);
}

now,

axios.get(/foo/bar)
.then(res => doSOmething())
.catch(err => handleErrorResponse(err))

The I use error handling the error response as string. The same you can use it with axios interceptor should you need.

This is not valid, then accepts single argument

.then(
        (response) => {
            // code
        },
        (error) => {
            // error handling
        }
    );

Only this's valid

.then(
        (response) => {
            // code
        }
    ).catch((error) => {
        // error handling
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744258421a4565510.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信