1 /**
2  * Exception classes used in the library.
3  * Authors:
4  *  $(LINK2 https://github.com/FreeSlave, Roman Chistokhodov)
5  * Copyright:
6  *  Roman Chistokhodov, 2017
7  * License:
8  *  $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
9  * See_Also:
10  *  $(LINK2 http://standards.freedesktop.org/desktop-entry-spec/latest/index.html, Desktop Entry Specification)
11  */
12 
13 module inilike.exception;
14 
15 ///Base class for ini-like format errors.
16 class IniLikeException : Exception
17 {
18     ///
19     this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) pure nothrow @safe {
20         super(msg, file, line, next);
21     }
22 }
23 
24 /**
25  * Exception thrown on error related to the group.
26  */
27 class IniLikeGroupException : Exception
28 {
29     ///
30     this(string msg, string groupName, string file = __FILE__, size_t line = __LINE__, Throwable next = null) pure nothrow @safe {
31         super(msg, file, line, next);
32         _group = groupName;
33     }
34 
35     /**
36      * Name of group where error occured.
37      */
38     @nogc @safe string groupName() const nothrow pure {
39         return _group;
40     }
41 
42 private:
43     string _group;
44 }
45 
46 /**
47  * Exception thrown when trying to set invalid key or value.
48  */
49 class IniLikeEntryException : IniLikeGroupException
50 {
51     this(string msg, string group, string key, string value, string file = __FILE__, size_t line = __LINE__, Throwable next = null) pure nothrow @safe {
52         super(msg, group, file, line, next);
53         _key = key;
54         _value = value;
55     }
56 
57     /**
58      * The key the value associated with.
59      */
60     @nogc @safe string key() const nothrow pure {
61         return _key;
62     }
63 
64     /**
65      * The value associated with key.
66      */
67     @nogc @safe string value() const nothrow pure {
68         return _value;
69     }
70 
71 private:
72     string _key;
73     string _value;
74 }
75 
76 /**
77  * Exception thrown on the ini-like file read error.
78  */
79 class IniLikeReadException : IniLikeException
80 {
81     /**
82      * Create $(D IniLikeReadException) with msg, lineNumber and fileName.
83      */
84     this(string msg, size_t lineNumber, string fileName = null, IniLikeEntryException entryException = null, string file = __FILE__, size_t line = __LINE__, Throwable next = null) pure nothrow @safe {
85         super(msg, file, line, next);
86         _lineNumber = lineNumber;
87         _fileName = fileName;
88         _entryException = entryException;
89     }
90 
91     /**
92      * Number of line in the file where the exception occured, starting from 1.
93      * 0 means that error is not bound to any existing line, but instead relate to file at whole (e.g. required group or key is missing).
94      * Don't confuse with $(B line) property of $(B Throwable).
95      */
96     @nogc @safe size_t lineNumber() const nothrow pure {
97         return _lineNumber;
98     }
99 
100     /**
101      * Number of line in the file where the exception occured, starting from 0.
102      * Don't confuse with $(B line) property of $(B Throwable).
103      */
104     @nogc @safe size_t lineIndex() const nothrow pure {
105         return _lineNumber ? _lineNumber - 1 : 0;
106     }
107 
108     /**
109      * Name of ini-like file where error occured.
110      * Can be empty if fileName was not given upon $(D IniLikeFile) creating.
111      * Don't confuse with $(B file) property of $(B Throwable).
112      */
113     @nogc @safe string fileName() const nothrow pure {
114         return _fileName;
115     }
116 
117     /**
118      * Original $(D IniLikeEntryException) which caused this error.
119      * This will have the same msg.
120      * Returns: $(D IniLikeEntryException) object or null if the cause of error was something else.
121      */
122     @nogc @safe IniLikeEntryException entryException() nothrow pure {
123         return _entryException;
124     }
125 
126 private:
127     size_t _lineNumber;
128     string _fileName;
129     IniLikeEntryException _entryException;
130 }