11import fs from 'node:fs' ;
22import path from 'node:path' ;
33
4- import Jrror , { JoorError } from '@/core/error' ;
5- import logger from '@/helpers/joorLogger' ;
4+ import { handleError , jssert } from '@/core/error' ;
65import validateConfig from '@/helpers/validateConfig' ;
76import JOOR_CONFIG from '@/types/config' ;
87
@@ -26,16 +25,12 @@ class Configuration {
2625 */
2726 private async loadConfig ( ) : Promise < void > {
2827 // Check if the configuration data is already loaded
29- if ( Configuration . configData !== null ) {
30- throw new Jrror ( {
31- code : 'config-loaded-already' ,
32- docsPath : '/configuration' ,
33- message :
34- 'The configuration data is already loaded. Attempting to load it again is not recommended' ,
35- type : 'warn' ,
36- } ) ;
37- }
38-
28+ jssert (
29+ Configuration . configData === null ,
30+ 'Configuration data is already loaded. Attempting to load it again is not recommended.' ,
31+ '/configuration' ,
32+ 'warn'
33+ ) ;
3934 try {
4035 // Default config file name is joor.config.js or else fallback to joor.config.ts
4136 let configFile = 'joor.config.js' ;
@@ -44,29 +39,21 @@ class Configuration {
4439 configFile = 'joor.config.ts' ;
4540 }
4641
47- if ( ! fs . existsSync ( path . resolve ( process . cwd ( ) , configFile ) ) ) {
48- throw new Jrror ( {
49- code : 'config-file-missing' ,
50- docsPath : '/configuration' ,
51- message :
52- 'The configuration file (joor.config.js or joor.config.ts) is missing in the root directory.' ,
53- type : 'error' ,
54- } ) ;
55- }
42+ // Check if the configuration file exists
43+ jssert (
44+ fs . existsSync ( path . resolve ( process . cwd ( ) , configFile ) ) ,
45+ 'The configuration file (joor.config.js or joor.config.ts) is missing in the root directory.' ,
46+ '/configuration' ,
47+ 'error'
48+ ) ;
5649
5750 const configPath = path . resolve ( process . cwd ( ) , configFile ) ;
5851 // Dynamically import the configuration file
5952 const configData = ( await import ( configPath ) ) . config as JOOR_CONFIG ;
6053 Configuration . configData = validateConfig ( configData ) ;
61- Configuration . configData = configData ;
6254 this . setConfigToEnv ( ) ;
6355 } catch ( error ) {
64- throw new Jrror ( {
65- code : 'config-load-failed' ,
66- message : `Error occured while loading the configuration file. ${ error } ` ,
67- type : 'panic' ,
68- docsPath : '/configuration' ,
69- } ) ;
56+ handleError ( error ) ;
7057 }
7158 }
7259
@@ -106,11 +93,7 @@ class Configuration {
10693 try {
10794 await this . loadConfig ( ) ;
10895 } catch ( error : unknown ) {
109- if ( error instanceof Jrror || error instanceof JoorError ) {
110- error . handle ( ) ;
111- } else {
112- logger . error ( error ) ;
113- }
96+ handleError ( error ) ;
11497 }
11598 }
11699
0 commit comments