Skip to content

Commit cbc1885

Browse files
authored
Improve the error message when an import cannot be resolved (#4023)
`DirectiveUri.toString()` turns out to be useless. So try to provide at least information about where the import directive starts in the source.
1 parent 18a7631 commit cbc1885

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/src/model/prefix.dart

+8-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ class Prefix extends ModelElement with HasNoPage {
3636
.firstWhere((i) => i.prefix2?.element == element);
3737
var importedLibrary = libraryImport.importedLibrary2;
3838
if (importedLibrary == null) {
39-
throw StateError(
40-
'Unexpected null LibraryElement2 for imported library at '
41-
'${libraryImport.uri}');
39+
var message = 'Unexpected null LibraryElement2 for imported library at '
40+
'${library.element.firstFragment.source.uri}, at offset '
41+
'${libraryImport.importKeywordOffset}';
42+
var directiveUri = libraryImport.uri;
43+
if (directiveUri is DirectiveUriWithRelativeUriString) {
44+
message += 'for import URI: "${directiveUri.relativeUriString}"';
45+
}
46+
throw StateError(message);
4247
}
4348
return importedLibrary;
4449
}

0 commit comments

Comments
 (0)