C# Acumatica REST API异常消息

C# Acumatica REST API异常消息,c#,winforms,postman,acumatica,C#,Winforms,Postman,Acumatica,我正在使用Acumatica REST API开发一个Windows窗体应用程序。我正在处理错误,并希望向用户提供有意义的错误消息。我注意到,当我使用PostMan进行测试时,我可以看到带有错误消息的内部异常 { "message": "An error has occurred.", "exceptionMessage": "Opportunity.ClassID: 'Class ID' cannot be foun

我正在使用Acumatica REST API开发一个Windows窗体应用程序。我正在处理错误,并希望向用户提供有意义的错误消息。我注意到,当我使用PostMan进行测试时,我可以看到带有错误消息的内部异常

{
  "message": "An error has occurred.",
  "exceptionMessage": "Opportunity.ClassID: 'Class ID' cannot be found in the system.\nInserting  'Opportunity' record raised at least one error. Please review the errors.",
  "exceptionType": "PX.Api.ContractBased.OutcomeEntityHasErrorsException",
  "stackTrace": "   at PX.Api.ContractBased.EntityService.GetOperationResult(EntityImpl entity, 

  ...
  ...
}
然而,当我在C#中使用HttpClient时,我只得到基本的500个错误。内部异常为null

System.Net.Http.HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).
    at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
我的put代码如下所示:

 public string Put(string entityName, string parameters, string entity)
    {
        try
        {
            var res = _httpClient.PutAsync(
                 _acumaticaAPI + entityName + "?" + parameters,
                new StringContent(entity, Encoding.UTF8, "application/json")).Result.EnsureSuccessStatusCode();

            return res.Content.ReadAsStringAsync().Result;
        }
        catch(HttpRequestException ex)
        {
            throw new HttpRequestException($"API PUT Error: {ex.ToString()}");
        }
    }

如何从表单应用程序中的异常中获得适当的详细信息?

以下是我使用的内容

请注意,在调用
client.PutAsync()
之后,我没有使用
.Result.ensureAccessStatusCode()语法

我使用的是
var response=await client.PutAsync(uri,stringContent)

之后,我检查响应代码,如果没有成功,我将继续获取响应,以便通过
err=wait response.Content.ReadAsStringAsync()获取错误消息

我正在使用NewtownSoft并将错误响应反序列化到我调用的类
ResponseMessage

        public async Task<T> PutData<T>(T data, string entity)
        {
            err = "";
            msg = new ResponseMessage();

            T respData = default(T);

            if (await Login() == false) return respData;

            var uri = new Uri(settings.url + settings.endpoint + entity); 
            try
            {
                var json = JsonConvert.SerializeObject(data);
                var stringContent = new StringContent(json, Encoding.UTF8, "application/json");

                var response = await client.PutAsync(uri, stringContent);
                
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    
                    respData = JsonConvert.DeserializeObject<T>(content);
                }
                else
                {
                    err = await response.Content.ReadAsStringAsync();
                    

                    try
                    {
                        msg = JsonConvert.DeserializeObject<ResponseMessage>(err);
                        if (msg != null) err = msg.exceptionMessage == "" ? msg.message : msg.exceptionMessage;
                        return respData;
                    }
                    catch (Exception ex)
                    {
                        err = ex.Message;
                        msg.exceptionMessage = ex.Message;
                        return respData;
                    }

                }

            }
            catch (Exception ex)
            {
                err = ex.Message;
                return respData;
            }

            return respData;
        }

这是我正在使用的

请注意,在调用
client.PutAsync()
之后,我没有使用
.Result.ensureAccessStatusCode()语法

我使用的是
var response=await client.PutAsync(uri,stringContent)

之后,我检查响应代码,如果没有成功,我将继续获取响应,以便通过
err=wait response.Content.ReadAsStringAsync()获取错误消息

我正在使用NewtownSoft并将错误响应反序列化到我调用的类
ResponseMessage

        public async Task<T> PutData<T>(T data, string entity)
        {
            err = "";
            msg = new ResponseMessage();

            T respData = default(T);

            if (await Login() == false) return respData;

            var uri = new Uri(settings.url + settings.endpoint + entity); 
            try
            {
                var json = JsonConvert.SerializeObject(data);
                var stringContent = new StringContent(json, Encoding.UTF8, "application/json");

                var response = await client.PutAsync(uri, stringContent);
                
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    
                    respData = JsonConvert.DeserializeObject<T>(content);
                }
                else
                {
                    err = await response.Content.ReadAsStringAsync();
                    

                    try
                    {
                        msg = JsonConvert.DeserializeObject<ResponseMessage>(err);
                        if (msg != null) err = msg.exceptionMessage == "" ? msg.message : msg.exceptionMessage;
                        return respData;
                    }
                    catch (Exception ex)
                    {
                        err = ex.Message;
                        msg.exceptionMessage = ex.Message;
                        return respData;
                    }

                }

            }
            catch (Exception ex)
            {
                err = ex.Message;
                return respData;
            }

            return respData;
        }