Skip to content

Commit 14c7582

Browse files
committed
Improve readability of error messages out of backend compilation
+ Clarify which declaration we are looking for in the backend main module. + Specialize rendering on the platform side to remove clutter in output to the user interface.
1 parent b8bd828 commit 14c7582

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

implement/elm-fullstack/ElmFullstack/ElmAppCompilation.cs

+26-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,14 @@ IImmutableDictionary<IImmutableList<string>, IReadOnlyList<byte>> compileNew()
152152
var otherErrorsText =
153153
string.Join("\n", otherErrors.Select(DescribeCompilationError));
154154

155-
throw new Exception("Failed compilation with " + compilationErrors.Count + " errors:\n" + otherErrorsText);
155+
/*
156+
* TODO: To clean up the appearance in the (CL) user interface, avoid burying the error message in a large stack trace:
157+
* Propagate the errors further without runtime exception.
158+
* New C# features might help simplify the syntax to implement this so that implementation might be easier after switching to .NET 6.
159+
* Until then, add line breaks to the message to help spot this within a larger output.
160+
* */
161+
162+
throw new Exception("Failed compilation with " + compilationErrors.Count + " errors:\n" + otherErrorsText + "\n\n");
156163
}
157164

158165
byte[] ElmMake(
@@ -440,11 +447,27 @@ static byte[] GetManifestResourceStreamContent(string name)
440447
}
441448

442449
static string DescribeCompilationError(CompilerSerialInterface.LocatedCompilationError locatedCompilationError) =>
450+
"in file " + string.Join('/', locatedCompilationError.location.filePath) + ": " +
443451
DescribeCompilationError(locatedCompilationError.error);
444452

445-
static string DescribeCompilationError(CompilerSerialInterface.CompilationError compilationError) =>
446-
Newtonsoft.Json.JsonConvert.SerializeObject(compilationError, Newtonsoft.Json.Formatting.Indented);
453+
static string DescribeCompilationError(CompilerSerialInterface.CompilationError compilationError)
454+
{
455+
var otherCompilationError =
456+
compilationError?.OtherCompilationError?.FirstOrDefault();
457+
458+
if (otherCompilationError != null)
459+
return otherCompilationError;
447460

461+
return
462+
Newtonsoft.Json.JsonConvert.SerializeObject(
463+
compilationError,
464+
Newtonsoft.Json.Formatting.Indented,
465+
new Newtonsoft.Json.JsonSerializerSettings
466+
{
467+
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
468+
DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include
469+
});
470+
}
448471

449472
static long EstimateCacheItemSizeInMemory(ElmValueCommonJson.Result<IReadOnlyList<CompilerSerialInterface.LocatedCompilationError>, ImmutableDictionary<IImmutableList<string>, IReadOnlyList<byte>>> item) =>
450473
(item.Err?.Sum(err => err.Sum(EstimateCacheItemSizeInMemory)) ?? 0) +

implement/elm-fullstack/ElmFullstack/compile-elm-program/src/CompileFullstackApp.elm

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ stateTypeAnnotationFromRootElmModule_2021_08 parsedModule =
473473
_ ->
474474
Err "Unexpected type annotation: Not an instance"
475475
)
476-
|> Maybe.withDefault (Err "Did not find declaration with matching name")
476+
|> Maybe.withDefault (Err ("Did not find declaration with name '" ++ elmAppInterfaceConvention.backendMainDeclarationName ++ "'"))
477477

478478

479479
stateTypeAnnotationFromRootElmModule_Before_2021_08 : Elm.Syntax.File.File -> Result String (Elm.Syntax.Node.Node Elm.Syntax.TypeAnnotation.TypeAnnotation)

implement/example-apps/elm-editor/src/CompileFullstackApp.elm

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ stateTypeAnnotationFromRootElmModule_2021_08 parsedModule =
473473
_ ->
474474
Err "Unexpected type annotation: Not an instance"
475475
)
476-
|> Maybe.withDefault (Err "Did not find declaration with matching name")
476+
|> Maybe.withDefault (Err ("Did not find declaration with name '" ++ elmAppInterfaceConvention.backendMainDeclarationName ++ "'"))
477477

478478

479479
stateTypeAnnotationFromRootElmModule_Before_2021_08 : Elm.Syntax.File.File -> Result String (Elm.Syntax.Node.Node Elm.Syntax.TypeAnnotation.TypeAnnotation)

0 commit comments

Comments
 (0)