netglade / auto_mappr

Code generation for mapping between different objects with ease.
https://pub.dev/packages/auto_mappr
MIT License
72 stars 9 forks source link

Extend tryConvert method for catching mapping exceptions #204

Closed PfeiJit closed 6 months ago

PfeiJit commented 8 months ago

Is your feature request related to a problem? Please describe. When mapping from source with a nullable variable to a target with a corresponding non-nullable variable by tryConvert method, an exception is thrown. tryConvert should work in the same way as int.parse, so that each time there is any problem with parsing, a null is returned.

Describe the solution you'd like Extend tryConvert method so that it catches all exceptions thrown during mapping (for example mapping a nullable variable to a non-nullable one). If any exception occurs, tryConvert returns null instead of throwing an exception. This behaviour is enabled by setting parameter safeMapping to true. By default safeMapping is false to keep backwards compatibility.

In addition, introduce optional parameter VoidCallback? onMappingError to tryConvert method that is called when exception during mapping occurs (can be used for example for logging).

Modify tryConvertList, convertIterable and convertSet accordingly.