formsy-react icon indicating copy to clipboard operation
formsy-react copied to clipboard

Prevent updateInputsWithError to throw error if corresponding component not found

Open bamaboy opened this issue 6 years ago • 3 comments

Hi, guys! I found out, that when i pass object with errors to updateInputsWithError, and there's no corresponding component in my form, an error is thrown. Here's code snippet responsible for that.

updateInputsWithError = (errors) => {
    Object.keys(errors).forEach((name) => {
      const component = utils.find(this.inputs, input => input.props.name === name);
      if (!component) {
        throw new Error(`You are trying to update an input that does not exist. Verify errors object with input names. ${JSON.stringify(errors)}`);
      }
....

Wouldn't it be better if warning has been shown, instead of error thrown? Because I don't want my app to stop, when server sends more errors, than my form has?

bamaboy avatar Jan 20 '19 15:01 bamaboy

That's an interesting point. Can you just surround your updateInputsWithError with a try-catch? The downside to warnings is they can't be caught and handled.

rkuykendall avatar Feb 03 '20 14:02 rkuykendall

@rkuykendall thank you for your answer! The problem is that some fields may not be updated because of an error

bamaboy avatar Feb 03 '20 15:02 bamaboy

That is a very good point. I think that would be an easy refactor to update all found fields and their errors for the rest. I'll work on it.

rkuykendall avatar Feb 03 '20 15:02 rkuykendall