代码之家  ›  专栏  ›  技术社区  ›  xChaax

如何将格式良好的错误响应为JSON

  •  1
  • xChaax  · 技术社区  · 6 年前

    嗨,我是C的新手,目前我正在构建我的第一个web API。要求将发生的任何错误以JSON格式显示为响应

    这是我的答题课

    public class ResponseData 
    {
        public List<AccountDTO> AccountList { set; get; }
        public string ReturnMessage { set; get; }
        public string error { get; set; }
        public string code { get; set; }
        public string message { get; set; }
        public string innererror { get; set; }
        public string type { get; set; }
        public string stacktrace { get; set; }
    
    }
    

    这是我的控制器

    例如,如果我的代码执行 catch 块,我将收集异常的所有信息并以格式化的格式返回 JSON

    [HttpGet]
        public ResponseData getAllAccounts(string BRN, string CompanyName)
        {
    
            ResponseData response = new ResponseData();
            List<AccountDTO> accountlist = null;
            IAccountInterface usecase = new AccountDAO();
            try
            {
                if (BRN != null && CompanyName != null)
                {
                    accountlist = usecase.getAccountbyBRNCompanyName().Where(p => p.BRN.ToLower().Equals(BRN.ToLower()) && p.CompanyName.ToLower().Contains(CompanyName.ToLower())).ToList();
                    response.ReturnMessage = "Data filter by BRN and CompanyName.";
                }
    
                else
                {
                    response.ReturnMessage = "Data filter cannot be null.";
                }
                response.AccountList = accountlist;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message + ", Stack: " + e.StackTrace);
                response.code = "sample code";
                response.message = e.Message;
                response.innererror = "{";
                response.message = e.Message;
                response.type = "ExceptionType + ...";
                response.stacktrace = e.StackTrace;
    
    
    
                //response.ReturnMessage = JsonConvert.SerializeObject(errorFormat);
    
    
            }
    
            return response;
        }
    

    enter image description here response object 所以我的所有对象属性都会显示出来。

    这是我想要实现的一个示例响应。有关错误的详细信息作为JSON包含在响应中。错误将采用此格式。任何帮助和指导都非常感谢。

    {  
     "error":{  
      "code": "<This code is not related to..>",  
      "message": "<A message describing the error..>",  
      "innererror": {  
       "message": "<A message describing the error..>",  
       "type": "Microsoft.Crm.CrmHttpException",  
       "stacktrace": "<Details from the server about where the 
                      error occurred>"  
        }  
       }  
    }
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Nkosi    6 年前

    所需的JSON错误响应模型与使用的类不匹配。

    考虑重构类模型以服务于单个职责

    public class ResponseData {
        public List<AccountDTO> AccountList { set; get; }
        public string ReturnMessage { set; get; }
    }
    

    还可以重构API操作,以根据操作流返回适当的响应。

    [HttpGet]
    public IHttpActionResult getAllAccounts(string BRN, string CompanyName) {
        try {
            ResponseData response = new ResponseData();
            List<AccountDTO> accountlist = null;
            IAccountInterface usecase = new AccountDAO();
            if (BRN != null && CompanyName != null) {
                accountlist = usecase.getAccountbyBRNCompanyName().Where(p => p.BRN.ToLower().Equals(BRN.ToLower()) && p.CompanyName.ToLower().Contains(CompanyName.ToLower())).ToList();
                response.ReturnMessage = "Data filter by BRN and CompanyName.";
            } else {
                response.ReturnMessage = "Data filter cannot be null.";
            }
            response.AccountList = accountlist;
            return Ok(response);
        }
        catch (Exception e) {
            Console.WriteLine("Error: " + e.Message + ", Stack: " + e.StackTrace);
            var response = new {
                error = new {
                    code = "sample code",
                    message = e.Message,
                    innererror = new {
                        message = e.Message,
                        type = e.GetType().Name,
                        stacktrace = e.StackTrace
                    }
                }
            }
            var responseMessage = Request.CreateResponse(HttpStatusCode.InternalServerError, response);
            return ResponseMessage(responseMessage);
        }
    }
    
        2
  •  0
  •   NewfrontSolutions    6 年前
            Console.WriteLine("Error: " + e.Message + ", Stack: " + e.StackTrace);
            response.error.code = "sample code";
            response.error.message = e.Message;
            response.error.innererror = "{";
            response.error.message = e.Message;
            response.error.type = "ExceptionType + ...";
            response.error.stacktrace = e.StackTrace;