fix: handle syntax errors from plugins (#1100)

This commit is contained in:
Ihor Karpiuk
2023-05-29 11:02:14 +03:00
committed by GitHub
parent c099e35e3b
commit 44554fe6fb
3 changed files with 24 additions and 4 deletions

View File

@@ -16,7 +16,6 @@ import {
isAbsoluteUrl, isAbsoluteUrl,
ResolveError, ResolveError,
YamlParseError, YamlParseError,
Oas3Definition,
} from '@redocly/openapi-core'; } from '@redocly/openapi-core';
import { blue, red, yellow } from 'colorette'; import { blue, red, yellow } from 'colorette';
import { existsSync } from 'fs'; import { existsSync } from 'fs';
@@ -398,6 +397,20 @@ describe('handleErrors', () => {
); );
}); });
it('should handle SyntaxError', () => {
const testError = new SyntaxError('Unexpected identifier');
testError.stack = 'test stack';
try {
handleError(testError, ref);
} catch (e) {
expect(e).toEqual(testError);
}
expect(process.exit).toHaveBeenCalledWith(1);
expect(process.stderr.write).toHaveBeenCalledWith(
'Syntax error: Unexpected identifier test stack\n\n'
);
});
it('should throw unknown error', () => { it('should throw unknown error', () => {
const testError = new Error('Test error'); const testError = new Error('Test error');
try { try {

View File

@@ -239,9 +239,13 @@ export function handleError(e: Error, ref: string) {
); );
return process.exit(1); return process.exit(1);
} }
case SyntaxError:
return exitWithError(`Syntax error: ${e.message} ${e.stack?.split('\n\n')?.[0]}`);
default: { default: {
process.stderr.write(`Something went wrong when processing ${ref}:\n\n - ${e.message}.\n\n`); process.stderr.write(
process.exitCode = 1; red(`Something went wrong when processing ${ref}:\n\n - ${e.message}.\n\n`)
);
process.exit(1);
throw e; throw e;
} }
} }
@@ -392,7 +396,7 @@ export async function loadConfigAndHandleErrors(
try { try {
return await loadConfig(options); return await loadConfig(options);
} catch (e) { } catch (e) {
exitWithError(e.message); handleError(e, '');
return new Config({ apis: {}, styleguide: {} }); return new Config({ apis: {}, styleguide: {} });
} }
} }

View File

@@ -89,6 +89,9 @@ export function resolvePlugins(
__non_webpack_require__(absoltePluginPath) __non_webpack_require__(absoltePluginPath)
: require(absoltePluginPath); : require(absoltePluginPath);
} catch (e) { } catch (e) {
if (e instanceof SyntaxError) {
throw e;
}
throw new Error(`Failed to load plugin "${plugin}". Please provide a valid path`); throw new Error(`Failed to load plugin "${plugin}". Please provide a valid path`);
} }
} }