jeudi 16 juillet 2020

What is the best way to handle exceptions in this scenario

I am writing an C# WEB API which takes multipart-form data to create a record in DB and upload files to a location. As the part of creation I am also creating a Jira Issue and updating the created record with JIRA details.

  [Route("api/request/create")]
  [Consumes("multipart/form-data")]
  public async Task<HttpResponseMessage> CreateRequest()
  {
     try
     {
        var multipartMemoryStream = await Request.Content.ReadAsMultipartAsync();
        var newRequestData = await multipartMemoryStream.Contents[0].ReadAsStringAsync();

       CreateRequestCommand createRequestCommand = JsonConvert.DeserializeObject<CreateRequestCommand>(newRequestData);

       var requestId = CreateRequestInDB(createRequestCommand)
       var jira = await CreateJira(createContentRequestCommand);
       await WriteSourceFiles(multipartMemoryStream.Contents);
       await UpdateRequestWithJiraDetails(requestId, jira);
       return new SuccessResponse(requestId)
    }
   catch (Exception ex)
   {
      throw ex;
   }
}

This is working fine in most cases. How can I handle the exception in the best way so that if any of the method fails system should not retain the record in DB and delete the JIRA issue too. What is the best stratergy to maintain system in consistent state if any of the step fails.

Aucun commentaire:

Enregistrer un commentaire