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
Dylan R. E. Moonfire ad0525be04 feat: initial commit
2021-09-10 12:33:42 -05:00

71 lines
1.8 KiB
C#

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;
}
var messages = errors
.SelectMany(x => x.Errors)
.Select(x => x.Message);
throw new ToolException(messages);
}
}
}