From 43f90b6a43d8932dcc3867f20d79fe37a174a313 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 14 Jan 2021 19:33:00 +0100 Subject: [PATCH] Improve error messages when the preparing builder fails --- .../src/analyzer/moor/create_table_reader.dart | 15 +++++++++++++-- moor_generator/lib/src/backends/backend.dart | 8 +++++++- .../lib/src/backends/build/build_backend.dart | 3 ++- moor_generator/pubspec.yaml | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/moor_generator/lib/src/analyzer/moor/create_table_reader.dart b/moor_generator/lib/src/analyzer/moor/create_table_reader.dart index ee5f1a98..945fff12 100644 --- a/moor_generator/lib/src/analyzer/moor/create_table_reader.dart +++ b/moor_generator/lib/src/analyzer/moor/create_table_reader.dart @@ -210,10 +210,21 @@ class CreateTableReader { Future _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; } diff --git a/moor_generator/lib/src/backends/backend.dart b/moor_generator/lib/src/backends/backend.dart index f91ad34d..3feb53e3 100644 --- a/moor_generator/lib/src/backends/backend.dart +++ b/moor_generator/lib/src/backends/backend.dart @@ -30,7 +30,7 @@ abstract class BackendTask { Future readMoor(Uri uri); Future resolveTypeOf(Uri context, String dartExpression) { - throw UnsupportedError('Resolving dart expressions not supported'); + throw CannotLoadTypeException('Resolving dart expressions not supported'); } Future 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); +} diff --git a/moor_generator/lib/src/backends/build/build_backend.dart b/moor_generator/lib/src/backends/build/build_backend.dart index a015e668..3987858f 100644 --- a/moor_generator/lib/src/backends/build/build_backend.dart +++ b/moor_generator/lib/src/backends/build/build_backend.dart @@ -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); diff --git a/moor_generator/pubspec.yaml b/moor_generator/pubspec.yaml index 79a08773..00b493c2 100644 --- a/moor_generator/pubspec.yaml +++ b/moor_generator/pubspec.yaml @@ -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: