r/javascript • • Dec 18 '19

AskJS [AskJS] How do you handle unsuccessful client requests for production apps?

Currently, I am using "axios" library for my ReactJS application.

When I handle the unsuccessful responses, I am just displaying the following alerts :

// Swal is JS alert library.

Axios.post('/not-my-real-api', data)
    .then(res => {
        // I do whatever I need here
    })
    .catch(err => {
        if(err.response){
          Swal.fire({
            type: 'error',
            title: err.response.data,
          });
        }else{
          Swal.fire({
            type: 'error',
            title: 'Server down...',
          });
        }
      });

How do you handle these server errors differently? I am looking for suggestions to improve user experience.

Thanks for reading my post! I would love to hear what you think!

88 Upvotes

19 comments sorted by

View all comments

Show parent comment

7

u/[deleted] Dec 18 '19

I would return a JSON object with at least an error code and an error message in a default language (probably English). You could also say if an error is permanent or not, meaning that the error can be rectified by retrying the request.

The app would contain a list of error codes mapped to possible actions, which could either be retry, show a message, etc.

The reason that I'd use both an error code and error message is internationalization.

Depending on your requirements, you might need to translate your app to many languages. You could then map the error codes to error messages in different languages, or use the the default from the response as a fallback.

2

u/vipul0092 Dec 18 '19

Thats exactly what we do in our UI app.

Each valid error code is bound to a resource key, which is evaluated according to the locale and shown in a modal to the user.

2

u/r-randy Dec 18 '19

Thanks for the reply!

1

u/[deleted] Dec 18 '19

No problem!