Skip to content

Commit e940743

Browse files
feat(jsonfilehandlererror): return the file path that caused the error
1 parent 649a423 commit e940743

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/index.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ export const overwrite = async (
163163
return write(filePath, jsonContent, indentationLevel);
164164
}
165165

166-
const notAValidObjectError = new JSONFileHandlerError('NOT_A_VALID_OBJECT');
166+
const notAValidObjectError = new JSONFileHandlerError(
167+
'NOT_A_VALID_OBJECT',
168+
filePath
169+
);
167170
return Promise.reject(notAValidObjectError);
168171
};
169172

@@ -183,11 +186,11 @@ export const read = async (filePath: string): Promise<object> => {
183186
}
184187

185188
if (stringifiedJsonContent.length === 0) {
186-
const emptyFileError = new JSONFileHandlerError('EMPTY_FILE');
189+
const emptyFileError = new JSONFileHandlerError('EMPTY_FILE', filePath);
187190
return Promise.reject(emptyFileError);
188191
}
189192

190-
const notAJsonError = new JSONFileHandlerError('NOT_A_JSON');
193+
const notAJsonError = new JSONFileHandlerError('NOT_A_JSON', filePath);
191194
return Promise.reject(notAJsonError);
192195
};
193196

@@ -223,7 +226,10 @@ export const join = async (
223226
}
224227
}
225228

226-
const notAValidObjectError = new JSONFileHandlerError('NOT_A_VALID_OBJECT');
229+
const notAValidObjectError = new JSONFileHandlerError(
230+
'NOT_A_VALID_OBJECT',
231+
filePath
232+
);
227233
return Promise.reject(notAValidObjectError);
228234
};
229235

src/models/classes.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { JSONFileHandlerErrorMessage } from './enums';
22

3-
/** Extends `Error` by adding a `code` property for ease of debugging, as
4-
* `SystemError` also contains that property.
3+
/** Extends `Error` by adding the `code` and `path` properties that are present
4+
* in `SystemError`.
55
*/
66
export class JSONFileHandlerError extends Error {
77
code: string;
8+
/** The file that caused the error (useful when multiple files are
9+
* manipulated) */
10+
path: string;
811

9-
constructor(code: keyof typeof JSONFileHandlerErrorMessage) {
12+
constructor(
13+
code: keyof typeof JSONFileHandlerErrorMessage,
14+
filePath: string
15+
) {
1016
const message = JSONFileHandlerErrorMessage[code];
1117
super(message);
1218
this.code = code;
19+
this.path = filePath;
1320
}
1421
}

0 commit comments

Comments
 (0)