Skip to content

Instantly share code, notes, and snippets.

@TimeBandit
Created October 2, 2018 07:15
Show Gist options
  • Save TimeBandit/f6c5a5809d0fc11458196a69d7475cd6 to your computer and use it in GitHub Desktop.
Save TimeBandit/f6c5a5809d0fc11458196a69d7475cd6 to your computer and use it in GitHub Desktop.
using string return values to signal errors #patterns #js
// reurn a string message to signal an error
handleAddOption(option) {
if (!option) {
return "Enter valid value";
} else if (this.state.options.indexOf(option) > -1) {
return "This option already exists";
} else {
this.setState((prevState, props) => {
return {
options: [...prevState.options, option]
};
});
}
}
// elsewhere check the return value; if it's a string
// signal error
const error = handleOption(option);
if (error){
// do something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment