1 /**
2  * Authors: Tomoya Tanjo
3  * Copyright: © 2021 Tomoya Tanjo
4  * License: Apache-2.0
5  */
6 module salad.exception;
7 
8 import dyaml : Node;
9 
10 ///
11 class SchemaException : Exception
12 {
13     ///
14     this(string msg, Node node, Throwable nextInChain = null) nothrow pure
15     {
16         auto mark = node.startMark;
17         super(msg, mark.name, mark.line+1, nextInChain);
18         column = mark.column+1;
19     }
20 
21     ///
22     ulong column;
23 }
24 
25 ///
26 E schemaEnforce(E)(lazy E exp, string msg, Node node)
27 {
28     if (auto e = exp())
29     {
30         return e;
31     }
32     throw new SchemaException(msg, node);
33 }
34 
35 ///
36 class DocumentException : Exception
37 {
38     ///
39     this(string msg, Node node, Throwable nextInChain = null) nothrow pure
40     {
41         auto mark = node.startMark;
42         super(msg, mark.name, mark.line+1, nextInChain);
43         column = mark.column+1;
44     }
45 
46     ///
47     ulong column;
48 }
49 
50 ///
51 E docEnforce(E)(lazy E exp, string msg, Node node)
52 {
53     if (auto e = exp())
54     {
55         return e;
56     }
57     throw new DocumentException(msg, node);
58 }