Improve error messages when the preparing builder fails

This commit is contained in:
Simon Binder 2021-01-14 19:33:00 +01:00
parent f5996cb957
commit 43f90b6a43
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 23 additions and 5 deletions

View File

@ -210,10 +210,21 @@ class CreateTableReader {
Future<UsedTypeConverter> _readTypeConverter(
ColumnType sqlType, MappedBy mapper) async {
final code = mapper.mapper.dartCode;
final type = await step.task.backend.resolveTypeOf(step.file.uri, code);
// todo report lint for any of those cases or when resolveTypeOf throws
DartType type;
try {
type = await step.task.backend.resolveTypeOf(step.file.uri, code);
} on CannotLoadTypeException catch (e) {
step.reportError(ErrorInMoorFile(span: mapper.span, message: e.msg));
return null;
}
if (type is! InterfaceType) {
step.reportError(
ErrorInMoorFile(
span: mapper.span,
message: 'Must be an interface type (backed by a class)'),
);
return null;
}

View File

@ -30,7 +30,7 @@ abstract class BackendTask {
Future<String> readMoor(Uri uri);
Future<DartType> resolveTypeOf(Uri context, String dartExpression) {
throw UnsupportedError('Resolving dart expressions not supported');
throw CannotLoadTypeException('Resolving dart expressions not supported');
}
Future<AstNode> loadElementDeclaration(Element element) async {
@ -59,3 +59,9 @@ class NotALibraryException implements Exception {
NotALibraryException(this.uri);
}
class CannotLoadTypeException implements Exception {
final String msg;
CannotLoadTypeException(this.msg);
}

View File

@ -76,7 +76,8 @@ class BuildBackendTask extends BackendTask {
_resolve(context).changeExtension('.dart_in_moor');
if (!await step.canRead(preparedHelperFile)) {
throw AssetNotFoundException(preparedHelperFile);
throw CannotLoadTypeException('Generated helper file not found. '
'Check the build log for earlier errors.');
}
final content = await step.readAsString(preparedHelperFile);

View File

@ -41,7 +41,7 @@ dependencies:
dev_dependencies:
test: ^1.6.0
build_runner: ^1.6.7
build_test: ^0.10.10
build_test: ^1.3.4
json_serializable: ^4.0.0-nullsafety
executables: