using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ServerCore; namespace ServerBase; /*============================================================================================= Result 관련 확장 함수 1. Result + Guard 조합 처리 public Result doFunction(string input) { var owner = getOwner(); NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!") ArgumentNullReferenceCheckHelper.throwIfNull(input, () => $"input is null !!! - {owner.toBasicString()}") var result = new Result(); try { // 내부 로직 수행 if (input.isNullOrWhiteSpace()) { result.setFail(ServerErrorCode.InvalidArgument, "Input is empty."); return result; } // 비즈니스 로직 처리 성공 result.setSuccess("Operation completed."); return result; } catch (Exception e) { // 예상치 못한 예외 발생시 실패 처리 err_msg = $"Exception !!!, Failed to perform, in xxx() !!! : exception:{e} - {owner.toBasicString()}"; result.setFail(ServerErrorCode.InternalError, err_msg); return result; } } 2. Result 값에 따른 예외 처리 var result = server_logic.doFunction(); // 실패시, 예외가 발생 한다 !!!, (발생되기 원하는 예외타입을 작성) result.throwIfFail( () => $"Failed to doFunction() !!!, Details: {result.toBasicString()}", msg => new DesiredException(msg) ); author : kangms =============================================================================================*/ public static class ResultHelper { public static bool isSuccess(this Result result) { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); return result.ErrorCode.isSuccess(); } public static bool isFail(this Result result) { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); return result.ErrorCode.isFail(); } public static void setSuccess(this Result result, string resultString = "") { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); result.set(ServerErrorCode.Success, resultString); } public static void setFail(this Result result, ServerErrorCode errorCode, string resultString = "") { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); result.set(errorCode, resultString); } public static string getResultString(this Result result) { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); return result.ResultString; } public static ServerErrorCode getErrorCode(this Result result) { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); return result.ErrorCode; } public static void set(this Result result, ServerErrorCode errorCode, string resultString) { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); result.ErrorCode = errorCode; result.ResultString = resultString; } public static void throwIfFail( this Result result , Func? fnLog = null , Func? overrideException = null ) { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); if (result.ErrorCode.isSuccess()) return; string message = fnLog?.Invoke() ?? $"Operation failed. ErrorCode: {result.ErrorCode}, ResultString: {result.ResultString}"; if (null != overrideException) { throw overrideException(message); } throw new InvalidOperationException(message); } public static string toBasicString(this Result result) { ArgumentNullReferenceCheckHelper.throwIfNull(result, () => "Result is null !!!"); return $"ErrorInfo: errCode:{result.getErrorCode()}, errDesc:{result.getResultString()}"; } }