1 /**
2  * Exception types
3  */
4 module libsnooze.exceptions;
5 
6 import libsnooze.event : Event;
7 
8 /** 
9  * The general libsnooze error type
10  */
11 public abstract class SnoozeError : Exception
12 {
13     /** 
14      * Constructs a new `SnoozeError` with the provided
15      * error message
16      *
17      * Params:
18      *   msg = the error message
19      */
20     package this(string msg)
21     {
22         super(msg);
23     }
24 }
25 /** 
26  * This exception is thrown if the call to `wait()`
27  * was interrupted for some reason
28  */
29 public final class InterruptedException : SnoozeError
30 {
31     /** 
32      * The `Event` with which the error occurred on
33      */
34     private Event e;
35 
36     /** 
37      * Constructs a new `InterruptedException` with
38      * provided `Event`
39      *
40      * Params:
41      *   e = the `Event` on which the error occurred
42      */
43     package this(Event e)
44     {
45         super("Interrupted whilst waiting on event '"~e.toString()~"'");
46         this.e = e;
47     }
48 
49     /** 
50      * Returns the `Event` with with this error occurred
51      *
52      * Returns: the event
53      */
54     public Event getEvent()
55     {
56         return e;
57     }
58 }
59 
60 /** 
61  * The sub-kind of fatal error
62  */
63 public enum FatalError
64 {
65     /** 
66      * On error during a call to `wait()`
67      */
68     WAIT_FAILURE,
69 
70     /** 
71      * On error during a call to `notify()`
72      * or `notifyAll()`
73      */
74     NOTIFY_FAILURE
75 }
76 
77 /** 
78  * This exception is thrown during a call to `wait()`,
79  * `notify()` or `notifyAll()` when a fatal error
80  * occurs with the underlying eventing system
81  */
82 public final class FatalException : SnoozeError
83 {
84     /** 
85      * The sub-kind of error
86      */
87     private FatalError fatalType;
88 
89     /** 
90      * Constructs a new `FatalException` with the provided
91      * associated `Event`, the sub-kind of error and an optional
92      * error message
93      *
94      * Params:
95      *   e = the `Event` on which the error occurred
96      *   fatalType = the sub-kind of error as a `FatalError`
97      */
98     package this(Event e, FatalError fatalType, string extra = "")
99     {
100         string msg;
101         if(fatalType == FatalError.NOTIFY_FAILURE)
102         {
103             msg = "There was an error notifying event '"~e.toString()~"'";
104         }
105         else
106         {
107             msg = "There was an error waiting on the event '"~e.toString()~"'";
108         }
109         msg = msg~extra;
110 
111         super(msg);
112         this.fatalType = fatalType;
113     }
114 
115     /** 
116      * Returns the sub-kind of error
117      *
118      * Returns: the `FatalError`
119      */
120     public FatalError getFatalType()
121     {
122         return fatalType;
123     }
124 }