@@ -122,8 +122,8 @@ class JoorError extends Error {
122122 Error Code: ${ this . errorCode }
123123 Message: ${ this . message }
124124 ${ marker . greenBright (
125- 'For more information, visit:'
126- ) } ${ marker . bgGreenBright . whiteBright ( docLink ) }
125+ 'For more information, visit:'
126+ ) } ${ marker . bgGreenBright . whiteBright ( docLink ) }
127127 ` ;
128128 }
129129}
@@ -163,24 +163,22 @@ class Jrror extends JoorError {
163163 // Throws error code joor-e1 if errorData is not provided, e2 if code is not provided, e3 if message is not provided, e4 if type is not provided
164164 throw new Jrror ( {
165165 message : `Instance of Jrror has been created without passing required data.
166- Missing: ${
167- ! errorData
168- ? 'errorData'
169- : ! errorData . code
170- ? 'error code'
171- : ! errorData . message
172- ? 'message'
173- : 'type'
174- } `,
175- code : `jrror-${
176- ! errorData
166+ Missing: ${ ! errorData
167+ ? 'errorData'
168+ : ! errorData . code
169+ ? 'error code'
170+ : ! errorData . message
171+ ? 'message'
172+ : 'type'
173+ } `,
174+ code : `jrror-${ ! errorData
177175 ? 'e1'
178176 : ! errorData . code
179177 ? 'e2'
180178 : ! errorData . message
181179 ? 'e3'
182180 : 'e4'
183- } `,
181+ } `,
184182 type : 'error' ,
185183 } ) ;
186184 }
@@ -193,5 +191,42 @@ class Jrror extends JoorError {
193191 }
194192}
195193
196- export { JoorError } ;
194+ /**
195+ * Handles errors by checking their type and calling the appropriate method.
196+ * If the error is not an instance of Jrror or JoorError, it logs the error.
197+ *
198+ * @param {unknown } error - The error to handle.
199+ *
200+ * Meant to reduce code duplication while handling errors in the codebase.
201+ */
202+ function handleError ( error : unknown ) : void {
203+ if ( error instanceof Jrror || error instanceof JoorError ) {
204+ error . handle ( ) ;
205+ } else {
206+ logger . error ( error ) ;
207+ }
208+ }
209+
210+ /**
211+ * Implicitly asserts that a condition is true. If the condition is false, it throws an error with the provided message and documentation path.
212+ * Alternative to `assert(condition, message)` from the `node:assert` module.
213+ *
214+ * For naming convention, `jssert` is used to avoid confusion with the `assert` function from the `node:assert` module.
215+ *
216+ * @param {boolean } condition - The condition to assert.
217+ * @param {string } message - The error message to throw if the assertion fails.
218+ * @param {string } docsPath - The documentation path for the error.
219+ */
220+ function jssert ( condition : boolean , message : string , docsPath : string = "/assertion" ) : asserts condition {
221+ if ( ! condition ) {
222+ throw new Jrror ( {
223+ code : 'assertion-failed' ,
224+ message,
225+ type : 'error' ,
226+ docsPath : docsPath ,
227+ } )
228+ }
229+ }
230+
231+ export { JoorError , jssert , handleError } ;
197232export default Jrror ;
0 commit comments