Skip to content

Commit

Permalink
add diff err types
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaddy Mansour committed May 25, 2024
1 parent 02c5baf commit c08feea
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shaddyhm/configs",
"version": "0.2.0",
"version": "0.2.1",
"description": "A simple application configuration library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
7 changes: 7 additions & 0 deletions src/errors/extension.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PackageError } from './package.error';

export class ExtensionError extends PackageError {
constructor(message: string) {
super(message);
}
}
2 changes: 1 addition & 1 deletion src/errors/parser.error.ts → src/errors/files.error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PackageError } from './package.error';

export class ParserError extends PackageError {
export class FilesError extends PackageError {
constructor(message: string) {
super(message);
}
Expand Down
3 changes: 2 additions & 1 deletion src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { DirectoryError } from './directory.error';
export { ExtensionError } from './extension.error';
export { FileError } from './file.error';
export { ParserError } from './parser.error';
export { FilesError } from './files.error';
export { ResolverError } from './resolver.error';
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import * as path from 'path';
import * as util from 'util';
import * as YAML from 'yaml';

import { ResolverError } from './errors';
import { ExtensionError, FilesError, ResolverError } from './errors';
import { DirectoryError } from './errors/directory.error';
import { FileError } from './errors/file.error';
import { ParserError } from './errors/parser.error';

const readFileAsync = util.promisify(fs.readFile);

Expand Down Expand Up @@ -73,7 +72,7 @@ export class Configs implements IConfigs {
);

if (!resolver.files || !resolver.files.length)
throw new FileError(`No config files provided for ${resolver.env} env`);
throw new FilesError(`No config files provided for ${resolver.env} env`);

// validate resolver files and extensions
resolver.files.forEach((file) => {
Expand All @@ -88,7 +87,7 @@ export class Configs implements IConfigs {
const ext = path.extname(file);
const isExtensionSupported = ['.yaml', '.yml'].includes(ext);
if (!isExtensionSupported)
throw new ParserError(
throw new ExtensionError(
`Unsupported file extension ${ext}. Only .yaml and .yml are supported.`,
);
});
Expand Down
7 changes: 4 additions & 3 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import * as assert from 'assert';
import { Configs } from '../src/';
import {
DirectoryError,
ExtensionError,
FileError,
ParserError,
FilesError,
ResolverError,
} from '../src/errors';

Expand Down Expand Up @@ -47,7 +48,7 @@ describe('Test unhappy paths', () => {
},
];
});
}, new FileError('No config files provided for development env'));
}, new FilesError('No config files provided for development env'));
});

it('should throw if no resolver is found', () => {
Expand Down Expand Up @@ -95,7 +96,7 @@ describe('Test unhappy paths', () => {
},
];
});
}, new ParserError('Unsupported file extension .json. Only .yaml and .yml are supported.'));
}, new ExtensionError('Unsupported file extension .json. Only .yaml and .yml are supported.'));
});
});

Expand Down

0 comments on commit c08feea

Please sign in to comment.