How to send non-fatal warnings from the library

The library function parses the file and returns an object. If the parser encounters unknown data, missing values, etc., it should not throw an exception and stop parsing (because it is not fatal), but there should be a way to pass information about these things to the caller (so that warnings can for example be displayed in the user interface). How can this warning be returned? I am thinking of passing a callback function / object to the library, are there any other possible solutions?

+3
source share
5 answers

I would have a set of errors easily accessible in the parser, something like this:

public class Parser
{
   public bool HasErrors { 
     get { return ParseErrors != null && ParseErrors.Count > 0; } 
   }    
   public List<string> ParseErrors { get; set; }
   public object Parse(string fileName) {}
}

Or any type of error that you wanted, of course, something in more detail.

The code calling your library will look something like this:

var p = new Parser();
var o = p.Parse("file.txt"); //Get object
if(p.HasErrors) //Uh oh, abort, do something with p.ParseErrors
+4
source

A common solution is to provide alerts (s) through a related function. Thus, the warning will only be read when the client code wants it.

+1
source

, . web- -, :

class Wrapper {
    List<Warning> Warnings { get; set; }
    bool HasWarnings { get { return Warnings != null && Warnings.Count > 0; } }
    MyObject Result { get; set; }
}

, , - , , -, . , , .

+1

:

class ParseResult (
    Outcome <succesful, warnings, failed completely>,
    ParseOutput <result>,
    List<Warning> warnings
)

, , ,

+1

, . , , , , . , , - . , , , .

0

Source: https://habr.com/ru/post/1737688/


All Articles