File: ValidationException.cs

package info (click to toggle)
python-schema-salad 8.9.20251102115403-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,060 kB
  • sloc: python: 19,247; cpp: 2,631; cs: 1,869; java: 1,341; makefile: 187; xml: 184; sh: 103; javascript: 46
file content (78 lines) | stat: -rw-r--r-- 1,913 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
namespace ${project_name};

public class ValidationException : Exception
{
    readonly string currentMessage;
    static readonly int indentPerLevel = 2;
    string bullet = "";
    private readonly List<ValidationException> children = new();

    public ValidationException() : this("")
    {
    }

    public ValidationException(string message)
        : this(message, new List<ValidationException>())
    {
    }

    public ValidationException(string message, ValidationException child)
        : this(message, new List<ValidationException> { child })
    {
    }

    public ValidationException(string message, List<ValidationException> children) : base(message)
    {
        currentMessage = message;
        foreach (ValidationException child in children)
        {
            this.children.AddRange(child.Simplify());
        }
    }

    private string Summary(int level)
    {
        string spaces = new(' ', level * indentPerLevel);
        return spaces + bullet + this.currentMessage;
    }

    public ValidationException WithBullet(string bullet)
    {
        this.bullet = bullet;
        return this;
    }

    List<ValidationException> Simplify()
    {
        if (this.ToString().Length > 0)
        {
            return new List<ValidationException> { this };
        }
        else return this.children;
    }

    private string PrettyString(int level = 0)
    {
        List<string> parts = new();
        int nextlevel = level;
        if (this.currentMessage.Length > 0)
        {
            parts.Add(this.Summary(level));
            nextlevel++;
        }

        foreach (ValidationException child in children)
        {
            parts.Add(child.PrettyString(nextlevel));
        }

        return string.Join('\n', parts);
    }

    public override string Message => PrettyString();

    public override string ToString()
    {
        return PrettyString();
    }
}