| | 1 | | using Microsoft.CodeAnalysis; |
| | 2 | | using System.Collections.Immutable; |
| | 3 | | using System.Text; |
| | 4 | |
|
| | 5 | | namespace Kestrun.Scripting; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Exception thrown when C# script compilation fails. |
| | 9 | | /// Contains detailed diagnostic information about compilation errors. |
| | 10 | | /// </summary> |
| | 11 | | public class CompilationErrorException : Exception |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Gets the collection of diagnostics produced during compilation. |
| | 15 | | /// </summary> |
| 2 | 16 | | public ImmutableArray<Diagnostic> Diagnostics { get; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initializes a new instance of the <see cref="CompilationErrorException"/> class with a specified error message a |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 22 | | /// <param name="diagnostics">The collection of diagnostics produced during compilation.</param> |
| | 23 | | public CompilationErrorException(string message, ImmutableArray<Diagnostic> diagnostics) |
| 6 | 24 | | : base(FormatMessage(message, diagnostics)) => Diagnostics = diagnostics; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="CompilationErrorException"/> class with a specified error message, |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 30 | | /// <param name="diagnostics">The collection of diagnostics produced during compilation.</param> |
| | 31 | | /// <param name="innerException">The exception that is the cause of the current exception.</param> |
| | 32 | | public CompilationErrorException(string message, ImmutableArray<Diagnostic> diagnostics, Exception innerException) |
| 0 | 33 | | : base(FormatMessage(message, diagnostics), innerException) => Diagnostics = diagnostics; |
| | 34 | |
|
| | 35 | | private static string FormatMessage(string baseMessage, ImmutableArray<Diagnostic> diagnostics) |
| | 36 | | { |
| 3 | 37 | | var sb = new StringBuilder(); |
| 3 | 38 | | _ = sb.AppendLine(baseMessage); |
| 3 | 39 | | _ = sb.AppendLine(); |
| | 40 | |
|
| 7 | 41 | | var errors = diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error).ToArray(); |
| 7 | 42 | | var warnings = diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning).ToArray(); |
| | 43 | |
|
| 3 | 44 | | if (errors.Any()) |
| | 45 | | { |
| 3 | 46 | | _ = sb.AppendLine($"Compilation Errors ({errors.Length}):"); |
| 12 | 47 | | for (var i = 0; i < errors.Length; i++) |
| | 48 | | { |
| 3 | 49 | | var error = errors[i]; |
| 3 | 50 | | _ = sb.AppendLine($" {i + 1}. {FormatDiagnostic(error)}"); |
| | 51 | | } |
| 3 | 52 | | _ = sb.AppendLine(); |
| | 53 | | } |
| | 54 | |
|
| 3 | 55 | | if (warnings.Any()) |
| | 56 | | { |
| 1 | 57 | | _ = sb.AppendLine($"Compilation Warnings ({warnings.Length}):"); |
| 4 | 58 | | for (var i = 0; i < warnings.Length; i++) |
| | 59 | | { |
| 1 | 60 | | var warning = warnings[i]; |
| 1 | 61 | | _ = sb.AppendLine($" {i + 1}. {FormatDiagnostic(warning)}"); |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| 3 | 65 | | return sb.ToString().TrimEnd(); |
| | 66 | | } |
| | 67 | |
|
| | 68 | | private static string FormatDiagnostic(Diagnostic diagnostic) |
| | 69 | | { |
| 4 | 70 | | var location = diagnostic.Location; |
| 4 | 71 | | var position = location.IsInSource ? $" at line {location.GetLineSpan().StartLinePosition.Line + 1}, column {loc |
| | 72 | |
|
| 4 | 73 | | return $"[{diagnostic.Id}] {diagnostic.GetMessage()}{position}"; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Gets a formatted string containing all error details. |
| | 78 | | /// </summary> |
| 3 | 79 | | public string GetDetailedErrorMessage() => Message; |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Gets only the error diagnostics (excluding warnings). |
| | 83 | | /// </summary> |
| 3 | 84 | | public IEnumerable<Diagnostic> GetErrors() => Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error); |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Gets only the warning diagnostics. |
| | 88 | | /// </summary> |
| 3 | 89 | | public IEnumerable<Diagnostic> GetWarnings() => Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Warning); |
| | 90 | | } |