question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

About handling failure

See original GitHub issue

Hi guys,

In the following saga, I deliberately returned an error

/**
 * Authentication saga
 */
export function *loginFlow () {
  while (true) {
    let request = yield take(LOGIN_REQUEST);
    let error = 'Invalid username or password';
    yield put(loginFailure(error));
  }
}

And my redux-form:

const { handleSubmit, error, submitting } = this.props;
const onSubmit = handleSubmit(actions.formAction);
return (
  <div>
    <form className="ui large form" onSubmit={onSubmit}>
    // ...
    </form>
    { error &&
      (
        <div className="ui error message">
          <span>Login failed</span>
        </div>
      )
    }      
  </div>
)

I expected to see the error message of Login failed when the form’s submitted. But what I’ve got is a Promise rejection:

screenshot from 2017-04-19 15-57-23

Would you please tell me how to handle rejections properly?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
afitiskincommented, May 10, 2017

@dandonahoe proper usage is following:

  1. Create action: const submitForm = createFormAction('SUBMIT_FORM')
  2. Pass it to handleSubmit function of your form: <form onSubmit={handleSubmit(submitForm)}>
  3. Take submitForm.REQUEST action in your saga and handle it:
function* formSubmitWatcherSaga() {
  yield takeEvery(submitForm.REQUEST, handleFormSubmitSaga);
}

function* handleFormSubmitSaga(action) {
  const values = action.payload; // values of your form, you can validate it on client side
  // or send it directly to the server
  // it's up to you what you do

  if (successful) {
    yield put(submitForm.success()); // dispatch success action
    // it will automatically resolves the promise given to redux-form
    // so redux form will finish submit with successful result
  } else {
    const error = new SubmissionError({
      field1: 'This field is required', // mark specific field as it has error
      _error: 'Failed! Please fill the form correctly and try again', // set global form error
    });
    yield put(submitForm.failure(error)); // dispatch failure action
    // pass error (instance of SubmissionError) to it
    // promise given to redux-form will be rejected with the error
    // so all wrong fields will be marked and form will get global error 
  }
}
1reaction
afitiskincommented, May 10, 2017

Updated README as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

Managing Yourself: Can You Handle Failure?
The first is to listen and communicate. It sounds obvious, but most of us forget to gather enough feedback or sufficiently explain our...
Read more >
9 Strategies for How To Deal With Failure (Plus Tips) - Indeed
1. Acknowledge your feelings · 2. Recognize irrational beliefs · 3. Release the need for approval · 4. Accept responsibility · 5. Don't...
Read more >
10 Healthy Ways to Cope With Failure - Verywell Mind
Embrace Your Emotions · Recognize Unhealthy Attempts to Reduce Pain · Practice Healthy Coping Skills · Acknowledge Irrational Beliefs About Failure.
Read more >
8 Ways Smart People Use Failure To Their Advantage - Forbes
People who are bad at handling failure tend to blame failure on their laziness, lack of intelligence or some other personal quality, which ......
Read more >
Coping with failure - Student services directory
Top tips for coping with failure · Give yourself permission to feel · Practise self-compassion · Reflect on the experience and adopt a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found