google / google/built_value.dart

Throw error container instead of individual errors

Open
#903 2 comments 0 reactions 1 assignee Claimed by @davidmorgan View on GitHub
p2 / feature request
Dominant language
Dart
Stars
886
Forks
195
Avg merge
1d 11h
Merged PRs (30d)
4

Description

Broken out from https://github.com/google/built_value.dart/issues/606#issuecomment-600900618

If null fields were thrown as a collection:
* errors would be more informative
* builders / build could be used for form validation abstractions

Essentially what I'm thinking is instead of
```dart
if (fieldA == null) {
throw new BuiltValueNullFieldError('MyBuiltValue', 'fieldA');
}
if (fieldB == null) {
throw new BuiltValueNullFieldError('MyBuiltValue', 'fieldB');
}
```

generate
```dart
final errors = [];
if (fieldA == null) {
errors.add(new BuiltValueNullFieldError('MyBuiltValue', 'fieldA'));
}
if (fieldB == null) {
errors.add(new BuiltValueNullFieldError('MyBuiltValue', 'fieldB'));
}
if (errors.isNotEmpty){
throw new BuiltValueInvalidFieldsError('MyBuiltValue', errors);
}
```
and then handle nested fields in `build` as well:
```dart
final fieldErrors = [];
final errorFields = {};
AValue fieldA;
BValue fieldB;
try {
fieldA = fieldA.build();
} catch (e) {
fieldErrors.add(e);
errorFields.add('fieldA');
}
try {
fieldB = fieldB.build();
} catch (e) {
fieldErrors.add(e);
errorFields.add('fieldB');
}
try {
_$result = _$v ?? new _$MyBuiltValue._(fieldA: fieldA, fieldB: fieldB, normalField: normalField);
} catch (buildErrors) {
fieldErrors.addAll(buildErrors.errors.where((e) => !errorFields.contains(e)));
}
if (fieldErrors.isNotEmpty)
throw new BuiltValueInvalidFieldsError('MyBuiltValue', errors);
}
replace(_$result);
return _$result;
```

If this sounds desirable I could PR it sometime soon, but it would be a breaking change

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.