Bladeren bron

Update error classes

customisations
alemart 1 jaar geleden
bovenliggende
commit
d94273a354
1 gewijzigde bestanden met toevoegingen van 55 en 10 verwijderingen
  1. 55
    10
      src/utils/errors.ts

+ 55
- 10
src/utils/errors.ts Bestand weergeven

20
  * Error classes
20
  * Error classes
21
  */
21
  */
22
 
22
 
23
-type MartinsErrorCause = Error | null;
24
-
25
 /**
23
 /**
26
  * Generic error class
24
  * Generic error class
27
  */
25
  */
30
     /**
28
     /**
31
      * Constructor
29
      * Constructor
32
      * @param message error message
30
      * @param message error message
33
-     * @param cause optional error cause
31
+     * @param cause cause of the error
34
      */
32
      */
35
-    constructor(message = '', public readonly cause: MartinsErrorCause = null)
33
+    constructor(message = '', public readonly cause: Error | null = null)
36
     {
34
     {
37
-        super([
38
-            message,
39
-            cause ? cause.toString() : '[martins-js]'
40
-        ].join('\n-> '));
35
+        super(message);
41
     }
36
     }
42
 
37
 
43
     /**
38
     /**
44
      * Error name
39
      * Error name
45
      */
40
      */
46
-    public get name(): string
41
+    public abstract get name(): string;
42
+    /*{
43
+        // incorrect when minified
44
+        //return this.constructor.name;
45
+    }*/
46
+
47
+    /**
48
+     * Convert to string
49
+     */
50
+    public toString(): string
47
     {
51
     {
48
-        return this.constructor.name;
52
+        const extendedMessage = this.cause ? '\n-> ' + this.cause.toString() : '';
53
+
54
+        if(this.message != '')
55
+            return this.name + ': ' + this.message + extendedMessage;
56
+        else
57
+            return this.name + extendedMessage;
49
     }
58
     }
50
 }
59
 }
51
 
60
 
54
  */
63
  */
55
 export class IllegalArgumentError extends MartinsError
64
 export class IllegalArgumentError extends MartinsError
56
 {
65
 {
66
+    public get name(): string
67
+    {
68
+        return 'IllegalArgumentError';
69
+    }
57
 }
70
 }
58
 
71
 
59
 /**
72
 /**
62
  */
75
  */
63
 export class IllegalOperationError extends MartinsError
76
 export class IllegalOperationError extends MartinsError
64
 {
77
 {
78
+    public get name(): string
79
+    {
80
+        return 'IllegalOperationError';
81
+    }
65
 }
82
 }
66
 
83
 
67
 /**
84
 /**
69
  */
86
  */
70
 export class NotSupportedError extends MartinsError
87
 export class NotSupportedError extends MartinsError
71
 {
88
 {
89
+    public get name(): string
90
+    {
91
+        return 'NotSupportedError';
92
+    }
72
 }
93
 }
73
 
94
 
74
 /**
95
 /**
76
  */
97
  */
77
 export class AccessDeniedError extends MartinsError
98
 export class AccessDeniedError extends MartinsError
78
 {
99
 {
100
+    public get name(): string
101
+    {
102
+        return 'AccessDeniedError';
103
+    }
79
 }
104
 }
80
 
105
 
81
 /**
106
 /**
83
  */
108
  */
84
 export class TimeoutError extends MartinsError
109
 export class TimeoutError extends MartinsError
85
 {
110
 {
111
+    public get name(): string
112
+    {
113
+        return 'TimeoutError';
114
+    }
86
 }
115
 }
87
 
116
 
88
 /**
117
 /**
90
  */
119
  */
91
 export class AssertionError extends MartinsError
120
 export class AssertionError extends MartinsError
92
 {
121
 {
122
+    public get name(): string
123
+    {
124
+        return 'AssertionError';
125
+    }
93
 }
126
 }
94
 
127
 
95
 /**
128
 /**
97
  */
130
  */
98
 export class TrackingError extends MartinsError
131
 export class TrackingError extends MartinsError
99
 {
132
 {
133
+    public get name(): string
134
+    {
135
+        return 'TrackingError';
136
+    }
100
 }
137
 }
101
 
138
 
102
 /**
139
 /**
104
  */
141
  */
105
 export class DetectionError extends MartinsError
142
 export class DetectionError extends MartinsError
106
 {
143
 {
144
+    public get name(): string
145
+    {
146
+        return 'DetectionError';
147
+    }
107
 }
148
 }
108
 
149
 
109
 /**
150
 /**
111
  */
152
  */
112
 export class TrainingError extends MartinsError
153
 export class TrainingError extends MartinsError
113
 {
154
 {
155
+    public get name(): string
156
+    {
157
+        return 'TrainingError';
158
+    }
114
 }
159
 }

Laden…
Annuleren
Opslaan