From f4c5d4e28f41c72a4863a2faa1ae1a8796e4cdaa Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Sun, 6 Feb 2022 23:45:42 +0000 Subject: [PATCH] fix: improve error handling for include --- src/type/Include.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/type/Include.ts b/src/type/Include.ts index 41af982..38e94f0 100644 --- a/src/type/Include.ts +++ b/src/type/Include.ts @@ -26,6 +26,10 @@ export interface IncludeResult { setSchema: (schema: Schema) => void; } +const ERROR_INCLUDE_EXIST = 'included file does not exist'; +const ERROR_INCLUDE_PARSE = 'error loading included file'; + + /** * Instantiate an include type with a copy of the provided options, * returning the include type and its schema setter. @@ -48,10 +52,14 @@ export function createInclude(options: Readonly): IncludeResult if (options.exists(canonical)) { return true; } else { - throw new NotFoundError('included file does not exist'); + throw new NotFoundError(ERROR_INCLUDE_EXIST); } } catch (err) { - throw new NotFoundError('included file does not exist', err); + if (err instanceof Error) { + throw new NotFoundError(ERROR_INCLUDE_EXIST, err); + } else { + throw new NotFoundError(ERROR_INCLUDE_EXIST); + } } }, construct(path: string): unknown { @@ -63,7 +71,11 @@ export function createInclude(options: Readonly): IncludeResult schema: mutableOptions.schema, }); } catch (err) { - throw new InvalidArgumentError('error including file', err); + if (err instanceof Error) { + throw new InvalidArgumentError(ERROR_INCLUDE_PARSE, err); + } else { + throw new InvalidArgumentError(ERROR_INCLUDE_PARSE); + } } }, });