gpt4 book ai didi

azure - 如何使用 IMobileServiceSyncTable - InsertAsync 捕获后端服务器的异常?

转载 作者:行者123 更新时间:2023-12-03 04:18:14 26 4
gpt4 key购买 nike

我正在使用 Azure 移动应用程序中的 IMobileServiceSyncTable。在 InsertAsync 操作中,在后端服务器端,我对数据进行了一些验证,如果验证失败,我想从服务器端抛出异常。我尝试返回InternalServerError(),抛出HttpResponseException,但从未在客户端工作过。我在服务器端调试Post方法,服务器抛出异常或返回InternalServerError,但在移动客户端,不会出现错误。

有人可以帮助我吗?

这是我在客户端的代码:

public async Task<bool> AddPaciente(Paciente novoPaciente)
{
//other things

try
{
await _pacienteTable.InsertAsync(novoPaciente);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);

throw new WebException(AppResources.MensagemFalhaConexaoServidorAzure);
}

await SyncPaciente();

return true;
}

这是我在后端服务器端的post方法

// POST tables/Paciente
public async Task<IHttpActionResult> PostPaciente(Paciente novoPaciente)
{

//other things

if (paciente != null)
{
var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Já existe um paciente com esse token cadastrado.")
};

//throw new HttpResponseException(responseMessage);
return InternalServerError(new Exception("Já existe um paciente com esse token cadastrado."));
}
}

最佳答案

您应该监听响应状态代码。

var response = await _pacienteTable.InsertAsync(novoPaciente);

if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsStringAsync ();
}
else
{
//Here handle the error code
throw new WebException(AppResources.MensagemFalhaConexaoServidorAzure);
}

关于azure - 如何使用 IMobileServiceSyncTable - InsertAsync 捕获后端服务器的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51142555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com