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,
ResolveError,
YamlParseError,
Oas3Definition,
} from '@redocly/openapi-core';
import { blue, red, yellow } from 'colorette';
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', () => {
const testError = new Error('Test error');
try {

View File

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

View File

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