Implement basic error handling (Fixes #21)

This commit is contained in:
uwap 2018-07-18 08:24:16 +02:00
parent 837f5a2bc4
commit e18006f8c1
3 changed files with 113 additions and 80 deletions

View file

@ -12,9 +12,16 @@ export const json = (path: string, innerType?: TopicType): TopicType => {
export type TypeOptionParam = { otherwise?: string, [string]: string };
export const option = (values: TypeOptionParam): TopicType => {
// TODO: error
const defaultValue = values.otherwise != null ? values.otherwise : "";
const mapVal = (x) => (values[x] != null ? values[x] : defaultValue);
const defaultValue = (x) => {
if (values.otherwise != null) {
return values.otherwise;
} else {
throw new Error(
`Value ${x.toString()} cannot by mapped by the option parameters given`
);
}
};
const mapVal = (x) => (values[x] != null ? values[x] : defaultValue(x));
return (x) => mapVal(x.toString());
};