Skip to content

Commit

Permalink
fix: enhance exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Mar 5, 2023
1 parent e2e15c9 commit 3b741b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/loader/built-in/json/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import fs from 'node:fs';
import { handleFileLoadError } from '../../../utils';
import { handleException } from '../../../utils';
import type { Loader } from '../../type';
import { buildLoaderFilePath } from '../../utils';

Expand All @@ -18,7 +18,7 @@ export class JSONLoader implements Loader {
const file = await fs.promises.readFile(filePath);
return JSON.parse(file.toString('utf-8'));
} catch (e) {
return handleFileLoadError(e);
return handleException(e);
}
}

Expand All @@ -29,7 +29,7 @@ export class JSONLoader implements Loader {
const file = fs.readFileSync(filePath);
return JSON.parse(file.toString('utf-8'));
} catch (e) {
return handleFileLoadError(e);
return handleException(e);
}
}
}
6 changes: 3 additions & 3 deletions src/loader/built-in/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import createJITI from 'jiti';
import { pathToFileURL } from 'node:url';
import type { LocatorInfo } from '../../../locator';
import { pathToLocatorInfo } from '../../../locator';
import { handleFileLoadError, hasStringProperty, isObject } from '../../../utils';
import { handleException, hasStringProperty, isObject } from '../../../utils';
import type { Loader } from '../../type';
import { buildLoaderFilePath } from '../../utils';
import type { ScriptFileLoadOptions } from './type';
Expand Down Expand Up @@ -128,7 +128,7 @@ export class ModuleLoader implements Loader {
}

/* istanbul ignore next */
return handleFileLoadError(e);
return handleException(e);
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ export class ModuleLoader implements Loader {
});
}

return handleFileLoadError(e);
return handleException(e);
}
}
}
26 changes: 21 additions & 5 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@
* view the LICENSE file that was distributed with this source code.
*/

/* istanbul ignore next */
export function handleFileLoadError(e: unknown) : void {
import { hasOwnProperty } from './has-property';
import { isObject } from './object';

export function handleException(e: unknown) : void {
if (e instanceof Error) {
throw e;
}

if (typeof e === 'string') {
throw new Error(e);
const error = new Error('Can not process thrown exception.');

if (isObject(e)) {
if (
hasOwnProperty(e, 'message') &&
typeof e.message === 'string'
) {
error.message = e.message;
}

if (
hasOwnProperty(e, 'stack') &&
typeof e.stack === 'string'
) {
error.stack = e.stack;
}
}

throw new Error('Cannot determine file loading error.');
throw error;
}

0 comments on commit 3b741b1

Please sign in to comment.