This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-toolbuilder-cil/src/MfGames.ToolBuilder/ToolException.cs

71 lines
1.8 KiB
C#
Raw Normal View History

2021-09-10 17:33:42 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using FluentResults;
namespace MfGames.ToolBuilder
{
public class ToolException : Exception
{
public ToolException(IEnumerable<string> messages)
: this(messages.ToArray())
{
}
public ToolException(params string[] messages)
: base("There was an exception while processing the tool.")
{
this.Messages = messages;
}
public ToolException()
: base("There was an exception while processing the tool.")
{
this.Messages = Array.Empty<string>();
}
public ToolException(string? message)
: this()
{
this.Messages = message != null
? new[] { message }
: Array.Empty<string>();
}
public ToolException(string? message, Exception? innerException)
: base(
"There was an exception while processing the tool.",
innerException)
{
this.Messages = message != null
? new[] { message }
: Array.Empty<string>();
}
public int ExitCode { get; } = 1;
public string[] Messages { get; }
public bool SuppressStackTrace { get; set; } = true;
public static void ThrowIf(IEnumerable<ResultBase> results)
{
var errors = results
.Where(x => x.IsFailed)
.ToList();
if (errors.Count == 0)
{
return;
}
IEnumerable<string> messages = errors
2021-09-10 17:33:42 +00:00
.SelectMany(x => x.Errors)
.Select(x => x.Message);
throw new ToolException(messages);
}
}
}