Skip to content

Commit

Permalink
fix: descriptive errors for config types (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 13, 2018
1 parent 36ba2f9 commit 03cb84a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/config/type/Env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Type as YamlType } from 'js-yaml';
import { BaseError } from 'noicejs';

export const envType = new YamlType('!env', {
kind: 'scalar',
resolve(name: string) {
return Reflect.has(process.env, name);
if (Reflect.has(process.env, name)) {
return true;
} else {
throw new BaseError(`environment variable not found: ${name}`);
}
},
construct(name: string) {
return Reflect.get(process.env, name);
Expand Down
16 changes: 11 additions & 5 deletions src/config/type/Include.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { existsSync, readFileSync } from 'fs';
import { safeLoad, Type as YamlType } from 'js-yaml';
import { BaseError } from 'noicejs';

import { CONFIG_SCHEMA } from 'src/config';

export const includeType = new YamlType('!include', {
Expand All @@ -8,10 +10,14 @@ export const includeType = new YamlType('!include', {
return existsSync(path);
},
construct(path: string): any {
return safeLoad(readFileSync(path, {
encoding: 'utf-8',
}), {
schema: CONFIG_SCHEMA,
});
try {
return safeLoad(readFileSync(path, {
encoding: 'utf-8',
}), {
schema: CONFIG_SCHEMA,
});
} catch (err) {
throw new BaseError('error including file', err);
}
},
});
1 change: 0 additions & 1 deletion src/config/type/Regexp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Type as YamlType } from 'js-yaml';
import { BaseError } from 'noicejs';

export const REGEXP_DELIMITER = '/';
export const REGEXP_REGEXP = /\/(.*)\/([gimuy]*)/;

export const regexpType = new YamlType('!regexp', {
Expand Down

0 comments on commit 03cb84a

Please sign in to comment.