0

This method is called in my business service from an asp.net mvc controller. Should there occur an exception or not I need to return a Result object.

The result class is experimental, maybe there is something better.

How would you do the exception handling, if I do not expect a special exception.

I just want to show the user the error message from the exception in my javascript file

with a messagebox if the success returns false.

 public Result CreateTestplan(Testplan testplan)
 {
    using (var con = new SqlConnection(_connectionString))
    using (var trans = new TransactionScope())
    {
       con.Open();

       _testplanDataProvider.AddTestplan(testplan);
       _testplanDataProvider.CreateTeststepsForTestplan(testplan.Id, testplan.TemplateId);
       trans.Complete();
   }
  }

class Result
{
   public bool Success {get;set;}
   public string Error {get;set;}
}
Pascal
  • 12,265
  • 25
  • 103
  • 195

1 Answers1

2

Wrap the entire transaction in a Try/Catch block & catch Exceptions. Within the catch block set the Error text on your Result to the exception text. Here's how it looks in code:

 public Result CreateTestplan(Testplan testplan)
 {
    Result res = new Result();
    try
    {
    using (var con = new SqlConnection(_connectionString))
    using (var trans = new TransactionScope())
    {
       con.Open();

       _testplanDataProvider.AddTestplan(testplan);
       _testplanDataProvider.CreateTeststepsForTestplan(testplan.Id, testplan.TemplateId);
       trans.Complete();
       res.Success = true;
       res.Error = string.Empty;
   }
   }
   catch (Exception e)
   {
       res.Success = false;
       res.Error = e.Message;
   }
   return result;
  }

class Result
{
   public bool Success {get;set;}
   public string Error {get;set;}
}

Of course, your service will end up swallowing any exceptions, so you need to make sure that the transaction failing doesn't leave your program in an inconsistent state.

Chris
  • 2,885
  • 18
  • 25
  • what inconsistent state could there be? Normally if a transaction fails everything is rolled back. – Pascal Jul 01 '12 at 20:59
  • @Steve yes... still wanted to give him a 5min chance to correct ;-) – Pascal Jul 01 '12 at 21:02
  • Now it works, still I find the exploitation of the default values (bool is false by default) not a good habit. I will prefer an explicit setting of the property `Success = false` and the `Error = String.Empty`. Hey, what's that `request` var now? – Steve Jul 01 '12 at 21:07
  • That's entirely fair, I'll include those edits. As for the transaction question Pascal, I was referring to any in-code objects you may have. Imagine that upon calling AddTestPlan you increment a counter (not sure why you'd do it at the start), but then the transaction fails. The counter is now incorrectly recording the number of TestPlans added. If it's just a pure SQL transaction you have nothing to worry about. – Chris Jul 01 '12 at 21:10
  • @Chris Did not understand all about that counter stuff, but yes its just sql transaction, what else could there be? Distributed transaction over multiple servers you speak of that? – Pascal Jul 01 '12 at 21:13
  • @Pascal Exactly, but if you're not using those, nothing to worry about:) – Chris Jul 01 '12 at 21:14