mirror of
https://github.com/LukeHagar/redocly-cli.git
synced 2025-12-09 20:57:44 +00:00
fix: handle syntax errors from plugins (#1100)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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: {} });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user