protected IActionResult Error <T>(string message, int code) { var response = new ResponseMessage <T>(); response.AddError(code, message); return(Ok(response)); }
protected IActionResult Error <T>(Exception ex) { var response = new ResponseMessage <T>(); response.AddError(ex); return(Ok(response)); }
protected IActionResult Forbidden <T>(UnauthorizedAccessException ex) { var response = new ResponseMessage <T>(); response.AddError(ex); ControllerContext.HttpContext.Response.StatusCode = StatusCodes.Status403Forbidden; return(Ok(response)); }
protected IActionResult Forbidden <T>(string message) { var response = new ResponseMessage <T>(); response.AddError(message); ControllerContext.HttpContext.Response.StatusCode = StatusCodes.Status403Forbidden; return(Ok(response)); }
/// <summary> /// 지정된 예외정보를 포함하는 응답메시지를 빌드합니다. /// </summary> /// <param name="exceptions"></param> /// <returns></returns> public static ResponseMessage CreateResponseMessageWithException(params Exception[] exceptions) { var responseMsg = new ResponseMessage(); foreach (var ex in exceptions) { responseMsg.AddError(ex); } return(responseMsg); }
/// <summary> /// <paramref name="responseMessage"/>에 예외정보를 추가합니다. /// </summary> public static void ReportError(this ResponseMessage responseMessage, Exception ex) { if (ex == null) { return; } responseMessage.AddError(ex); if (log.IsWarnEnabled) { log.WarnException("작업처리 중 예외가 발생하여, 응답 메시지에 예외정보를 추가했습니다.", ex); } }
public override async Task OnExceptionAsync(ExceptionContext context) { var exceptionType = context.Exception.GetType(); var errorKey = Guid.NewGuid().ToString().ToUpper(); var response = new ResponseMessage <object>(); if (exceptionType != typeof(EmptyUserIdException) && exceptionType != typeof(ValidationException)) { var modelState = JsonConvert.SerializeObject(context.ModelState.Values); var routeData = JsonConvert.SerializeObject(context.RouteData.Values); var requestURL = context.HttpContext.Request.Scheme.ToString() + "://" + context.HttpContext.Request.Host.ToString() + context.HttpContext.Request.Path.ToString() + context.HttpContext.Request.QueryString.ToString(); string bodyAsText = "[]"; if (context.HttpContext.Request.ContentLength != null && context.HttpContext.Request.ContentLength > 0) { context.HttpContext.Request.Body.Seek(0, System.IO.SeekOrigin.Begin); using (var reader = new System.IO.StreamReader(context.HttpContext.Request.Body)) { bodyAsText = await reader.ReadToEndAsync(); } } string requestUserId = _userContext?.UserId?.ToString().ToUpper() ?? null; _logger.LogError(context.Exception, "{ExceptionMessage} [{ErrorGuid}, {RequestURL}, {RequestBody}, {ModelState}, {RouteData}, {RequestUserId}] ", context.Exception.Message, errorKey, requestURL, bodyAsText, modelState, routeData, requestUserId); } if (exceptionType != typeof(EmptyUserIdException)) { var displayMessage = _config.GetValue <string>("GenericErrorMessage"); if (string.IsNullOrWhiteSpace(displayMessage)) { displayMessage = "An error has occurred. Error reference number: "; } context.Exception = new Exception($"{displayMessage}{ errorKey }"); response.AddError(context.Exception, false); } context.Result = new OkObjectResult(response); await base.OnExceptionAsync(context); }
/// <summary> /// DATA 처리를 위한 요청정보를 처리해서, 응답정보를 빌드해서 반환합니다. /// </summary> /// <param name="requestMessage">요청 메시지</param> /// <returns>응답 메시지</returns> public virtual ResponseMessage Execute(RequestMessage requestMessage) { var responseMsg = new ResponseMessage(); if (IsDebugEnabled) { log.Debug("요청정보에 따른 작업을 시작합니다..."); } if (requestMessage == null) { if (log.IsWarnEnabled) { log.Warn("요청 정보가 NULL입니다."); } responseMsg.Errors.Add(new ErrorMessage { Code = 0, Message = DataServiceTool.NoRequestProvided }); return(responseMsg); } TransactionScope txScope = null; try { // 1. 사전 작업 // responseMsg.MessageId = requestMessage.MessageId; if (requestMessage.Transactional) { if (IsDebugEnabled) { log.Debug("요청작업이 Transaction을 필요로 하여, Transaction을 시작합니다..."); } txScope = AdoTool.CreateTransactionScope(TransactionScopeOption.Required, IsolationLevel.ReadCommitted); } // // 2. 본 작업 // ExecuteInternal(requestMessage, responseMsg); // 3. 사후 작업 // if (txScope != null) { txScope.Complete(); if (IsDebugEnabled) { log.Debug("요청작업의 Transaction을 Commit 했습니다."); } } } catch (Exception ex) { responseMsg.AddError(ex); if (log.IsErrorEnabled) { log.Error("요청정보에 따른 작업처리 중 예외가 발생했습니다.", ex); } if (txScope != null) { txScope.Dispose(); } } if (IsDebugEnabled) { log.Debug("요청정보에 따른 작업을 완료했습니다!!! Request MessageId=[{0}], Response MessageId=[{1}]", requestMessage.MessageId, responseMsg.MessageId); } return(responseMsg); }