Files
caliverse_server/ServerCore/Exception/ConditionCheckHelper.cs
2025-05-01 07:23:28 +09:00

75 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ServerCore;
//=============================================================================================
// 조건별 체크 관련 예외 지원 함수
//
// author : kangms
//
//=============================================================================================
public static class ConditionValidCheckHelper
{
public static void throwIfFalse( bool isCondition
, string errMsg
, Func<string, Exception>? overrideException = null )
{
if (isCondition)
return;
if (overrideException != null)
{
throw overrideException(errMsg);
}
throw new InvalidOperationException(errMsg);
}
public static void throwIfFalseWithCondition( Expression<Func<bool>> conditionExpression
, Func<string>? fnLog = null
, Func<string, Exception>? overrideException = null )
{
var compiled_expression = conditionExpression.Compile();
if (compiled_expression())
return;
string condition_text = conditionExpression.Body.ToString();
string errMsg = fnLog?.Invoke() ?? $"Failed to check condition: '{condition_text}'";
if (overrideException != null)
{
throw overrideException(errMsg);
}
throw new InvalidOperationException(errMsg);
}
}
public static class RangeCheckHelper
{
public static void throwIfOutOfRange( int value, int min, int max
, Func<string>? fnLog = null
, Func<string, Exception>? overrideException = null )
{
if (value >= min && value <= max)
return;
string message = fnLog?.Invoke() ?? $"Value {value} is out of range [{min}, {max}]";
if (overrideException != null)
{
throw overrideException(message);
}
throw new ArgumentOutOfRangeException(nameof(value), message);
}
}