| | | 1 | | namespace Envilder; |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Thrown by a load with contract validation enabled when the map file is |
| | | 8 | | /// invalid or a declared secret cannot be confirmed. The message names the |
| | | 9 | | /// failing keys and paths, never a secret value. |
| | | 10 | | /// </summary> |
| | | 11 | | public class ContractValidationException : Exception |
| | | 12 | | { |
| | | 13 | | /// <summary>Initializes a new instance for the check that failed.</summary> |
| | | 14 | | /// <param name="result">The full outcome of the check.</param> |
| | | 15 | | public ContractValidationException(ContractValidationResult result) |
| | 1 | 16 | | : base(BuildMessage(result)) |
| | | 17 | | { |
| | 1 | 18 | | Result = result; |
| | 1 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary>The full outcome of the check that failed.</summary> |
| | | 22 | | public ContractValidationResult Result { get; } |
| | | 23 | | |
| | | 24 | | private static string BuildMessage(ContractValidationResult result) |
| | | 25 | | { |
| | 1 | 26 | | if (result.MapFileErrors.Count > 0) |
| | | 27 | | { |
| | 1 | 28 | | var errors = string.Join( |
| | 1 | 29 | | "; ", |
| | 1 | 30 | | result.MapFileErrors.Select(error => $"{error.Key}: {error.Message}")); |
| | 1 | 31 | | return $"Map file contract validation failed: {errors}"; |
| | | 32 | | } |
| | | 33 | | |
| | 1 | 34 | | var keys = string.Join( |
| | 1 | 35 | | ", ", |
| | 1 | 36 | | result.Keys |
| | 1 | 37 | | .Where(key => key.Status != SecretExistenceStatus.Exists) |
| | 1 | 38 | | .Select(key => $"{key.Variable} ({key.Path}): {key.Status.ToWireValue()}")); |
| | 1 | 39 | | return $"Map file contract validation failed: {keys}"; |
| | | 40 | | } |
| | | 41 | | } |