Skip to content

Commit

Permalink
fix(reference): use JavaScript error causes interface
Browse files Browse the repository at this point in the history
Refs #2561
  • Loading branch information
char0n committed Feb 15, 2023
1 parent d3f5089 commit 437e0f5
Show file tree
Hide file tree
Showing 25 changed files with 33 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/apidom-reference/src/dereference/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const dereferenceApiDOM = async <T extends Element>(
// unwrap the element from ParseResult assuming first element is the actual result
return surrogateWrapping ? result.get(0) : result;
} catch (error: any) {
throw new DereferenceError(`Error while dereferencing file "${file.uri}"`, error);
throw new DereferenceError(`Error while dereferencing file "${file.uri}"`, { cause: error });
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-reference/src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const parseFile = async (file: IFile, options: IReferenceOptions): Promise<Parse

return result;
} catch (error: any) {
throw new ParserError(`Error while parsing file "${file.uri}"`, error);
throw new ParserError(`Error while parsing file "${file.uri}"`, { cause: error });
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ApiDesignSystemsJsonParser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'syntacticAnalysis', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ApiDesignSystemsYamlParser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const AsyncApiJson2Parser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'syntacticAnalysis', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const AsyncApiYaml2Parser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const BinaryParser: stampit.Stamp<IParser> = stampit(Parser, {

return parseResultElement;
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const BinaryParser: stampit.Stamp<IParser> = stampit(Parser, {

return parseResultElement;
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-reference/src/parse/parsers/json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const JsonParser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'syntacticAnalysis'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const OpenApiJson3_0Parser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'syntacticAnalysis', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const OpenApiJson3_1Parser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'syntacticAnalysis', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const OpenApiYaml3_0Parser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const OpenApiYaml3_1Parser: stampit.Stamp<IParser> = stampit(Parser, {
const parserOpts = pick(['sourceMap', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const YamlParser: stampit.Stamp<IParser> = stampit(Parser, {
try {
return await parse(source, { sourceMap: this.sourceMap });
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, error);
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-reference/src/resolve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const resolveApiDOM = async <T extends Element>(
const { result } = await plugins.run('resolve', [file, options], resolveStrategies);
return result;
} catch (error: any) {
throw new ResolverError(`Error while resolving file "${file.uri}"`, error);
throw new ResolverError(`Error while resolving file "${file.uri}"`, { cause: error });
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const FileResolver: stampit.Stamp<IFileResolver> = stampit(Resolver, {
try {
return await promisify(readFile)(fileSystemPath);
} catch (error: any) {
throw new ResolverError(`Error opening file "${file.uri}"`, error);
throw new ResolverError(`Error opening file "${file.uri}"`, { cause: error });
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const HttpResolverAxios: stampit.Stamp<IHttpResolverAxios> = stampit(HttpResolve
const response = await client.get<Buffer>(file.uri);
return response.data;
} catch (error: any) {
throw new ResolverError(`Error downloading "${file.uri}"`, error);
throw new ResolverError(`Error downloading "${file.uri}"`, { cause: error });
}
};
},
Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-reference/src/resolve/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export const readFile = async (file: IFile, options: IReferenceOptions): Promise
const { result } = await plugins.run('read', [file], resolvers);
return result;
} catch (error: any) {
throw new ResolverError(`Error while reading file "${file.uri}"`, error);
throw new ResolverError(`Error while reading file "${file.uri}"`, { cause: error });
}
};
8 changes: 3 additions & 5 deletions packages/apidom-reference/src/util/errors/DereferenceError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class DereferenceError extends Error {
public cause: undefined | Error;

constructor(message: string, cause?: Error) {
super(message);
this.cause = cause;
constructor(message: string, options?: { cause?: Error }) {
super(message, options);
this.cause = this.cause ?? options?.cause;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class InvalidSelectorError extends Error {
public cause: undefined | Error;

constructor(message: string, cause?: Error) {
constructor(message: string, options?: { cause?: Error }) {
super(message);
this.cause = cause;
this.cause = this.cause ?? options?.cause;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class NotImplementedError extends Error {
constructor(message = 'Not Implemented') {
constructor(message = 'Not Implemented', options?: { cause?: Error }) {
super(message);
this.cause = this.cause ?? options?.cause;
}
}

Expand Down
6 changes: 2 additions & 4 deletions packages/apidom-reference/src/util/errors/ParserError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class ParserError extends Error {
public cause: undefined | Error;

constructor(message: string, cause?: Error) {
constructor(message: string, options?: { cause?: Error }) {
super(message);
this.cause = cause;
this.cause = this.cause ?? options?.cause;
}
}

Expand Down
8 changes: 3 additions & 5 deletions packages/apidom-reference/src/util/errors/PluginError.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
class PluginError extends Error {
public cause: Error;

public plugin: any;

constructor(message: string, cause: Error, plugin: any) {
constructor(message: string, options: { cause?: Error; plugin: any }) {
super(message);
this.cause = cause;
this.plugin = plugin;
this.cause = this.cause ?? options?.cause;
this.plugin = options.plugin;
}
}

Expand Down
6 changes: 2 additions & 4 deletions packages/apidom-reference/src/util/errors/ResolverError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class ResolverError extends Error {
public cause: undefined | Error;

constructor(message: string, cause?: Error) {
constructor(message: string, options?: { cause?: Error }) {
super(message);
this.cause = cause;
this.cause = this.cause ?? options?.cause;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-reference/src/util/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const run = async (method: string, parameters: any[], plugins: any[]): Pr
const result = await plugin[method].call(plugin, ...parameters); // eslint-disable-line no-await-in-loop
return { plugin, result };
} catch (error: any) {
lastError = new PluginError('Error while running plugin', error, plugin);
lastError = new PluginError('Error while running plugin', { cause: error, plugin });
}
}

Expand Down

0 comments on commit 437e0f5

Please sign in to comment.