Skip to content

Commit 284bbaa

Browse files
committed
refactor: Refactor configuration loading class using jssert
1 parent 0a21dbd commit 284bbaa

9 files changed

Lines changed: 204 additions & 136 deletions

File tree

src/core/config.ts

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import fs from 'node:fs';
22
import 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';
65
import validateConfig from '@/helpers/validateConfig';
76
import 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

src/core/error.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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,22 +163,24 @@ 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: ${!errorData
167-
? 'errorData'
168-
: !errorData.code
169-
? 'error code'
170-
: !errorData.message
171-
? 'message'
172-
: 'type'
173-
}`,
174-
code: `jrror-${!errorData
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
175177
? 'e1'
176178
: !errorData.code
177179
? 'e2'
178180
: !errorData.message
179181
? 'e3'
180182
: 'e4'
181-
}`,
183+
}`,
182184
type: 'error',
183185
});
184186
}
@@ -196,7 +198,7 @@ class Jrror extends JoorError {
196198
* If the error is not an instance of Jrror or JoorError, it logs the error.
197199
*
198200
* @param {unknown} error - The error to handle.
199-
*
201+
*
200202
* Meant to reduce code duplication while handling errors in the codebase.
201203
*/
202204
function handleError(error: unknown): void {
@@ -208,23 +210,30 @@ function handleError(error: unknown): void {
208210
}
209211

210212
/**
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 {
213+
* Implicitly asserts that a condition is true. If the condition is false, it throws an error with the provided message and documentation path.
214+
* Alternative to `assert(condition, message)` from the `node:assert` module.
215+
*
216+
* For naming convention, `jssert` is used to avoid confusion with the `assert` function from the `node:assert` module.
217+
*
218+
* @param {boolean} condition - The condition to assert.
219+
* @param {string} message - The error message to throw if the assertion fails.
220+
* @param {string} docsPath - The documentation path for the error.
221+
* @param {JOOR_ERROR['type']} type - The type of error to throw ["warn" | "error" | panic]
222+
*
223+
*/
224+
function jssert(
225+
condition: boolean,
226+
message: string,
227+
docsPath: string = '/assertion',
228+
type: JOOR_ERROR['type'] = 'error'
229+
): asserts condition {
221230
if (!condition) {
222231
throw new Jrror({
223232
code: 'assertion-failed',
224233
message,
225-
type: 'error',
234+
type: type,
226235
docsPath: docsPath,
227-
})
236+
});
228237
}
229238
}
230239

0 commit comments

Comments
 (0)