title: Microsoft.Azure.Mobile Client - Handling Server Error using custom IMobileServiceSyncHandler - Xamarin Forms
body: <p>I have implemented the Azure - Offline Sync based on the documentation / Sample provided by Microsoft <a href="https://github.com/Azure/azure-mobile-apps-quickstarts/blob/master/client/xamarin.forms/ZUMOAPPNAME/TodoItemManager.cs" rel="nofollow noreferrer">Sample</a> in my Xamarin Forms Application.</p>
  
  <p>In the sample / documentation provided, they are using the default Service Handler.</p>
  
  <p><strong>// Simple error/conflict handling. A real application would handle the various errors like network conditions,server conflicts and others via the IMobileServiceSyncHandler.</strong> </p>
  
  <p>Since I need to implement a <strong>retry logic for 3 times</strong> if the <strong>Pull / Push fails.</strong> As per the documentation I have created a custom Service Handler(<strong>IMobileServiceSyncHandler</strong>).</p>
  
  <p><strong>Please find my code logic here.</strong></p>
  
  <pre><code>public class CustomSyncHandler : IMobileServiceSyncHandler
  {
      public async Task&lt;JObject&gt; ExecuteTableOperationAsync(IMobileServiceTableOperation operation)
      {
          MobileServiceInvalidOperationException error = null;
          Func&lt;Task&lt;JObject&gt;&gt; tryExecuteAsync = operation.ExecuteAsync;
  
          int retryCount = 3;
          for (int i = 0; i &lt; retryCount; i++)
          {
              try
              {
                  error = null;
                  var result = await tryExecuteAsync();
                  return result;
              }
              catch (MobileServiceConflictException e)
              {
                  error = e;
              }
              catch (MobileServicePreconditionFailedException e)
              {
                  error = e;
              }
              catch (MobileServiceInvalidOperationException e)
              {
                  error = e;
              }
              catch (Exception e)
              {
                  throw e;
              }
  
              if (error != null)
              {
                  if(retryCount &lt;=3) continue;
                  else
                  {
                       //Need to implement
                       //Update failed, reverting to server's copy.
                  }
              }
          }
          return null;
      }
  
      public Task OnPushCompleteAsync(MobileServicePushCompletionResult result)
      {
          return Task.FromResult(0);
      }
  }
  </code></pre>
  
  <p>But I am not sure how to handle / revert <strong>server copy in case all the 3 retry failed.</strong></p>
  
  <p>In the TODO sample they where <strong>reverting</strong> it based on the 
  <strong>MobileServicePushFailedException</strong>. But which is available  when we implement <strong>IMobileServiceSyncHandler</strong>. 
  More over if we include custom IMobileServiceSyncHandler it wont execute the code after <strong>PushAsync / PullAsync</strong>. Even the try catch wont fire in case any exception.</p>
  
  <pre><code>        try
          {
              await this.client.SyncContext.PushAsync();
  
              await this.todoTable.PullAsync(
                  //The first parameter is a query name that is used internally by the client SDK to implement incremental sync.
                  //Use a different query name for each unique query in your program
                  "allTodoItems",
                  this.todoTable.CreateQuery());
          }
          catch (MobileServicePushFailedException exc)
          {
              if (exc.PushResult != null)
              {
                  syncErrors = exc.PushResult.Errors;
              }
          }
  
          // Simple error/conflict handling. A real application would handle the various errors like network conditions,
          // server conflicts and others via the IMobileServiceSyncHandler.
          if (syncErrors != null)
          {
              foreach (var error in syncErrors)
              {
                  if (error.OperationKind == MobileServiceTableOperationKind.Update &amp;&amp; error.Result != null)
                  {
                      //Update failed, reverting to server's copy.
                      await error.CancelAndUpdateItemAsync(error.Result);
                  }
                  else
                  {
                      // Discard local change.
                      await error.CancelAndDiscardItemAsync();
                  }
  
                  Debug.WriteLine(@"Error executing sync operation. Item: {0} ({1}). Operation discarded.", error.TableName, error.Item["id"]);
              }
          }
      }
  </code></pre>
  
  <p><strong>Note</strong></p>
  
  <p>In my application I am only trying to achieve retry for 3 time in case any server error. I am not looking for to resolve conflicts. Thant is the reason I haven't added the code for the same.</p>
  
  <p>If someone came across similar issues and resolved it please help.</p>
  
  <p>Stez.</p>