S
S
stepan1322019-08-10 10:59:12
React
stepan132, 2019-08-10 10:59:12

Is error trapping implemented correctly?

Hello. Is the code for catching errors during registration correctly implemented, or should it be done differently? Here is the code of the function (If you do not use throw, then an error does not come in catch):

export let AuthUser = (userName, password) => async (dispatch) => {
    try {
        let token = await service.createRequestToken();
        let validatedToken = await service.validateRequestToken(userName, password, token.request_token);

        if (validatedToken.status_code === 3) {
            throw 'Неправильное имя или пароль';
        } else if (validatedToken.status_code === 8) {
            throw 'Вы совершили слишком много попыток входа, попробуйте позже';
        } else if (validatedToken.status_code === 9) {
            throw 'Сервер недоступен, попробуйте позже';
        } else if (validatedToken.status_code && validatedToken.status_code !== 1) {
            throw 'Что-то пошло не так, попробуйте позже';
        }

        let sessionId = await service.createSessionId(validatedToken.request_token);
        dispatch({type: 'SET_SESSION_ID', payload: sessionId});
        let userData = await service.getAccountDetails(sessionId.session_id);
        dispatch({type: 'SET_USER_DATA', payload: userData});
    } catch (err) {
        dispatch(stopSubmit("auth", {_error: err}));
    }
}

PS Whether I am interested in the correct method I have chosen, and not something else.
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-08-10
@rockon404

In a good way, error texts should be sent by the server. Errors validating forms by keys.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question