wrapDirectiveError function

D4rtException wrapDirectiveError(
  1. String directiveType,
  2. Uri ownerUri,
  3. Uri targetUri,
  4. D4rtException error,
)

DFUB13 — names the module that ASKED for a target that failed to load.

The loader's own diagnostic names the module it could not find. That is the symptom; the file the user has to edit is the one holding the directive, and in a barrel chain those are rarely the same file. directiveType is 'import' or 'export', ownerUri the module containing the directive, targetUri the module it pointed at.

The concrete exception type is preserved so existing on ... clauses keep matching — only the message gains a prefix. A type this function cannot reconstruct is returned unchanged rather than downgraded to a base type.

Returns error unchanged when it already carries context (see D4rtException.hasDirectiveContext) so a deep import chain yields one prefix rather than one per frame.

Implementation

D4rtException wrapDirectiveError(
  String directiveType,
  Uri ownerUri,
  Uri targetUri,
  D4rtException error,
) {
  if (error.hasDirectiveContext) return error;
  final message =
      'Failed to load $directiveType "$targetUri" from module "$ownerUri": '
      '${error.message}';
  final D4rtException? wrapped = switch (error) {
    SourceCodeD4rtException e =>
      SourceCodeD4rtException(message, e.problematicCode),
    RuntimeD4rtException e =>
      RuntimeD4rtException(message, originalException: e.originalException),
    _ => null,
  };
  // An unreconstructable type is returned untouched AND unflagged, so an outer
  // frame that can wrap is still free to do so.
  if (wrapped == null) return error;
  wrapped.hasDirectiveContext = true;
  return wrapped;
}