seasonedcc / composable-functions

Types and functions to make composition easy and safe
MIT License
666 stars 14 forks source link

Throw multiple input errors at once #22

Closed diogob closed 2 years ago

diogob commented 2 years ago

There are use cases such as this one, where throwing a single exception with multiple input errors is desirable. We should add a custom error constructor for that. Perhaps just have a plural InputErrors that will take a non-empty list as argument. If we are fine breaking backwards compatibility we could also just change the InputError type.

danielweinmann commented 2 years ago

I like the idea of having a separate error for this use case. The current InputError is very elegant for most use cases. It would be weird to have to pass a list to it.

machour commented 2 years ago

I'm also fine with having a separate InputErrors that would handle this case.

diogob commented 2 years ago

@machour I have just published the 0.3.0 to npm. The package.json from remix-forms is bound to >= 0.2.0 so you should be able to update the remix-domains dependency on your project and use the new InputErrors error and pass a list of {message: string, path: string}.

The interface is described in the corresponding README section.

machour commented 2 years ago

Tested and validated, thank you so much @diogob !

gustavoguichard commented 5 months ago

Just an update from the future! You can throw a list of errors now with:

const fn = composable(() => {
  throw new ErrorList([
    new Error('Oops'),
    new InputError('Required', ['name'])
  ])
})