Skip to content

Commit

Permalink
List errors from compilation in toast if there are any
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Dec 6, 2023
1 parent dd82f2a commit 82539f5
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions backend/src/DataModeling/Converter/Csharp/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public static Assembly CompileToAssembly(string csharpCode)
diagnostic.Severity == DiagnosticSeverity.Error);

var errors = new StringBuilder();
List<string> customErrorMessages = new ();
List<string> customErrorMessages = new();
foreach (Diagnostic diagnostic in failures)
{
string errorMessage = $"{diagnostic.Id}: {diagnostic.GetMessage()}";
errors.AppendLine(errorMessage);
customErrorMessages.Add(errorMessage);
customErrorMessages.Add(diagnostic.GetMessage());
}

throw new CsharpCompilationException($"// Compiler // CompileToAssembly // Csharp compilation failed with errors: {errors}", customErrorMessages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public string CreateModelFromMetadata(ModelMetadata serviceMetadata)
CreateModelFromMetadataRecursive(classes, serviceMetadata.Elements.Values.First(el => el.ParentElement == null), serviceMetadata, serviceMetadata.TargetNamespace);

StringBuilder writer = new StringBuilder()
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using System.ComponentModel.DataAnnotations;")
.AppendLine("using System.Linq;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public static class DataModelingErrorCodes
public const string XmlSchemaConvertError = "DM_02";
public const string JsonSchemaConvertError = "DM_03";
public const string ModelMetadataConvertError = "DM_04";
public const string CsharpCompilationError = "DM_05";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void OnException(ExceptionContext context)

if (context.Exception is CsharpCompilationException compilationException)
{
context.Result = new ObjectResult(ProblemDetailsUtils.GenerateProblemDetails(context.Exception, DataModelingErrorCodes.CsharpCompilationError, HttpStatusCode.BadRequest, compilationException.CustomErrorMessages)) { StatusCode = (int)HttpStatusCode.BadRequest };
context.Result = new ObjectResult(ProblemDetailsUtils.GenerateProblemDetails(context.Exception, DataModelingErrorCodes.CsharpGenerationError, HttpStatusCode.BadRequest, compilationException.CustomErrorMessages)) { StatusCode = (int)HttpStatusCode.BadRequest };
}

if (context.Exception is CsharpGenerationException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Altinn.Studio.DataModeling.Converter.Csharp;
using FluentAssertions;
using SharedResources.Tests;
using Xunit;
Expand Down
1 change: 1 addition & 0 deletions frontend/language/src/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"administration.service_saved_name_administration_description": "Benyttes (sammen med eier) som unik identifikator for appen både i URLer og APIer. Kan ikke endres etter at appen er satt i produksjon.",
"administration.success": "Applikasjonen er tilgjengelig i miljøet.",
"administration.unavailable": "Applikasjonen er midlertidig utilgjengelig i miljøet.",
"api_errors.DM_01": "Noe gikk galt under bygging av datamodellen.",
"api_errors.GT_01": "Deling av endringer mislyktes. Vennligst prøv igjen.",
"api_errors.ResourceNotFound": "Fant ikke en fil applikasjonen din prøvde å få tak i.",
"app_create_release.application_builds_based_on": "Applikasjonen bygges basert på",
Expand Down
21 changes: 20 additions & 1 deletion frontend/packages/shared/src/contexts/ServicesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,26 @@ const handleError = (
const errorMessageKey = `api_errors.${errorCode}`;

if (i18n.exists(errorMessageKey)) {
toast.error(t(errorMessageKey), { toastId: errorMessageKey });
const mainErrorMessage = t(errorMessageKey);
toast.error(mainErrorMessage, { toastId: errorMessageKey });

const customErrorMessages = error?.response?.data?.customErrorMessages;
if (customErrorMessages && customErrorMessages.length > 0) {
toast.update(errorMessageKey, {

Check warning on line 67 in frontend/packages/shared/src/contexts/ServicesContext.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/packages/shared/src/contexts/ServicesContext.tsx#L67

Added line #L67 was not covered by tests
render: () => (
<div>

Check warning on line 69 in frontend/packages/shared/src/contexts/ServicesContext.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/packages/shared/src/contexts/ServicesContext.tsx#L69

Added line #L69 was not covered by tests
<p>{mainErrorMessage}</p>
<ul>
{customErrorMessages.map((errorMessage, index) => (
<li key={index}>{errorMessage}</li>

Check warning on line 73 in frontend/packages/shared/src/contexts/ServicesContext.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/packages/shared/src/contexts/ServicesContext.tsx#L73

Added line #L73 was not covered by tests
))}
</ul>
</div>
),
});
customErrorMessages.forEach((cem) => toast.bind(cem));

Check warning on line 79 in frontend/packages/shared/src/contexts/ServicesContext.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/packages/shared/src/contexts/ServicesContext.tsx#L79

Added line #L79 was not covered by tests
}

return;
}
}
Expand Down
3 changes: 2 additions & 1 deletion frontend/packages/shared/src/types/api/ApiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface ApiError {
title?: string;
type?: string;
errorCode?: string;
};
customErrorMessages?: string[];
}

0 comments on commit 82539f5

Please sign in to comment.