开发者

Collecting errors within a process and send them back to the caller - best approach?

I have a method in a class that performs several tasks (calling other methods, etc). The whole process can have along the way some errors or problems, but this doesn't mean that the process is aborted. I want that after the method finished, it returns to the caller a list of all of this problems so that he can decide what to do with them.

what's the best approach to implement this?

The first thing that comes to my mind is to make the method return a List of some kind of error class, but i may need to return something else with it, so that would be a problem. Also, throwing exceptions isn't good, because that would stop the flow, and i need to collect the errors along the way, not stop execution and go back to the caller.

I was also thinking of raising some kind of event that the caller listens to, but that would mean several ev开发者_如何学Pythonents (for each error), and i just want that to happen once.

ideas?


My first idea is to create a class that would be an accumulator for these errors, e.g.

class ProcessingErrors
{
  public void ReportError(...) { store the errror};
}

which you would pass in as a parameter:

MyResult DoProcessing(RealArgs a, ProcessingErrors e)
{
  ....
  if(error) e.ReportError(...);
  ...
  return result;
}


A few approaches

  1. Your method will receive a delegate to an "error function" that will be called to report each error. The problem is that you need to pass this delegate around to all other methods.
  2. Return a Tuple<RealResult, ErrorsList> so that the caller can examine both the result and the errors list.
  3. If this is a repeated functionality and there are many methods that need to report errors - you can write a special class named, say, ErrorReportable and write LINQ operators that sequence objects of this type (if you know what a Monad is - then LINQ is just a simple monad and SelectMany is its "bind" operator). The code is cleaner but you need to do some work.


Create a delegate for errorcollecting along the way. Pass that to all classes in use, and let them report errors through this along the way. Let the calling class collect and handle the errors, or create a separate class for that. Something like below:

public delegate void ErrorCollector(string errorDescription);

public class MainControl
{

    public void Execute()
    {

        new DoA(CollectErrors).DoStuff();
        new DoB(CollectErrors).DoStuff();

        //Display Errors encountered during processing in DoA and DoB
        foreach (string s in _errorList)
        {
            Console.WriteLine(s);
        }
    }

    public IList<string> ErrorList
    { get {return _errorList;} }


    private void CollectErrors(string errorDescription)
    {
        _errorList.Add(errorDescription);    
    }

    private readonly IList<string> _errorList = new List<string>();
}

public class DoA
{
    private readonly ErrorCollector _errorCollector;

    public DoA(ErrorCollector errorCollector)
    {
        _errorCollector = errorCollector;
    }

    public void DoStuff()
    {
        //Do something

        //Perhaps error occurs: 
        _errorCollector("ERROR IN DoA!!!");
    }
}

public class DoB
{
    private readonly ErrorCollector _errorCollector;

    public DoB(ErrorCollector errorCollector)
    {
        _errorCollector = errorCollector;
    }

    public void DoStuff()
    {
        //Do something

        //Perhaps error occurs: 
        _errorCollector("ERROR IN DoB!!!");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜