How can I catch exception from backend server using IMobileServiceSyncTable - InsertAsync?

VinciusdaCruzMaiaVinciusdaCruzMaia ✭✭BRMember ✭✭
edited July 2018 in Xamarin.Forms

I'm using IMobileServiceSyncTable from Azure Mobile App. When I use InsertAsync operation on the client side, on the backend server side, I had some validations for the data and, if that validations failure, I want throw Exception from the server side. I tried return InternalServerError(), throw HttpResponseException, but never worked on the client side. I debbuged the Post method in server side, the server throws the exception or return InternalServerError, but in the mobile client, don't occurs error.

Can anyone help me?

Here is my code on the client side:

    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;
    } 

Here is my post method on the backend server side

// 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."));
    }
}

Answers

  • ElvisXiaElvisXia Xamurai Member, Forum Administrator, Xamarin Team Xamurai
    edited July 2018

    @VinciusdaCruzMaia Could you please debug on client side to see what did you get as response?

Sign In or Register to comment.