Error 메시지 리턴

Main

static async Task Main(string[] args)
        {
            var service = new MyService();

            Console.WriteLine("Enter input (normal, error, refund):");
            string userInput = Console.ReadLine();

            ServiceResponse<string> result = await service.ProcessTransactionAsync(userInput);

            if (result.Error.ErrorExist)
            {
                Console.WriteLine($"Error {result.Error.ResultErrorNo}: {result.Error.ResultMsg}");
            }
            else
            {
                Console.WriteLine($"Success: {result.Data}");
            }

            Console.ReadLine();
        }

Model

namespace ConsoleSabre.Models
{
    public class ErrorModel
    {
        /// <summary>
        /// 각 처리 단계에서의 에러 유무
        /// </summary>
        public bool ErrorExist { get; set; } = false;

        /// <summary>
        /// 에러 위치 (H: 호스트 오류, L: 로컬 시스템 오류)
        /// </summary>
        public string ErrorLocation { get; set; } = "";

        /// <summary>
        /// 결과 에러 번호
        /// </summary>
        public string ResultErrorNo { get; set; } = "0";

        /// <summary>
        /// 결과 에러 메시지
        /// </summary>
        public string ResultMsg { get; set; } = "";
    }

    // 서비스 응답 결과를 담기 위한 제네릭 클래스
    public class ServiceResponse<T>
    {
        /// <summary>
        /// 에러 관련 정보를 포함하는 모델
        /// </summary>
        public ErrorModel Error { get; set; } = new ErrorModel();

        /// <summary>
        /// 실제 반환 데이터 (정상 처리 결과)
        /// </summary>
        public T Data { get; set; }
    }
}

MyService

using ConsoleSabre.Models;

namespace ConsoleSabre.Services
{
    public class MyService
    {
        // 에러 발생시 호출할 비동기 메서드 (실제 구현에서는 트랜잭션 취소 등 처리)
        public async Task IgnoreTransactionLLSRQ()
        {
            // 예시: 비동기 작업 시뮬레이션
            await Task.Delay(10);
            Console.WriteLine("Ignore executed.");
        }

        // 세션 종료를 위한 비동기 메서드 (실제 구현에서는 세션 종료 로직이 포함될 수 있음)
        public async Task EndSession()
        {
            // 예시: 비동기 작업 시뮬레이션
            await Task.Delay(10);
            Console.WriteLine("EndSession executed.");
        }

        // 공통 에러 처리 함수: 에러 모델에 값을 세팅하고 필요한 비동기 처리를 진행
        private async Task<bool> HandleErrorAsync(ErrorModel errorModel, string errorCode, string errorMessage, string errorLocation)
        {
            errorModel.ErrorExist = true;
            errorModel.ResultErrorNo = errorCode;
            errorModel.ResultMsg = errorMessage;
            errorModel.ErrorLocation = errorLocation; // (예:L 로컬 시스템 오류)

            // 공통 에러 처리 로직 실행
            await IgnoreTransactionLLSRQ();
            await EndSession();
            return true;
        }

        // 비즈니스 로직을 처리하는 예제 서비스 메서드
        // 입력 값에 따라 정상 처리 또는 에러 조건을 시뮬레이션 합니다.
        public async Task<ServiceResponse<string>> ProcessTransactionAsync(string input)
        {
            var response = new ServiceResponse<string>();

            try
            {
                // 조건 1: 입력이 "error"인 경우 에러 처리
                if (string.Equals(input, "error", StringComparison.OrdinalIgnoreCase))
                {
                    await HandleErrorAsync(response.Error, "431", "Simulated error occurred.","");
                    return response;
                }

                // 조건 2: 입력이 "refund"인 경우 또 다른 에러 처리 (예시: 환불 관련 오류)
                if (string.Equals(input, "refund", StringComparison.OrdinalIgnoreCase))
                {
                    string errorMsg = "Refund error: insufficient funds";
                    errorMsg += ". Please contact support.";
                    await HandleErrorAsync(response.Error, "432", errorMsg,"");
                    return response;
                }

                // 정상 처리: 입력 값을 기반으로 결과 데이터 생성
                response.Data = $"Processed input: {input}";
            }
            catch (Exception ex)
            {
                // 예외 발생 시 에러 처리
                await HandleErrorAsync(response.Error, "500", $"Unhandled exception: {ex.Message}", "");
            }

            return response;
        }
    }
}