Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scully): add exit by default when have a plugin error and cli option #496

Merged
merged 3 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/scully-cmd-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The Scully CLI has the following options available:
- [ssl-key](#ssl-key)
- [highlight](#highlight)
- [tds](#tds)
- [pluginsError](#pluginsError)

## Serve

Expand Down Expand Up @@ -163,3 +164,12 @@ The following APIs are supported on the test data server:
- `/posts` - Returns a list of posts
- `/posts/:id` - Returns a post by id
- `/slow/:delay` - Returns 200 code after a delay has gone by. Eg: `/slow/2000` takes 2 seconds.

## pluginsError

```bash
npx scully --pluginsError=false
```

Show the error from the plugin, but continue rendering.
If you dont use the flag (by default is true) when you have an error into any plugin, the scully's run exit.
13 changes: 13 additions & 0 deletions plugins/demos/errorPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {registerPlugin, logWarn} = require('../../dist/scully');

const errorPlugin = async (html, options) => {
try {
throw new Error(`new error`);
} catch (e) {
logWarn(`errorPlugin works!`);
}
return undefined;
};

const validator = async config => [];
registerPlugin('render', 'errorPlugin', errorPlugin, validator);
1 change: 1 addition & 0 deletions scully.sampleBlog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {getFlashPreventionPlugin} = require('./dist/scully-plugin-flash-preventio
require('./plugins/demos/extra-plugin.js');
require('./plugins/demos/tocPlugin');
require('./plugins/demos/voidPlugin');
require('./plugins/demos/errorPlugin');
const {setPluginConfig} = require('./dist/scully');

const FlashPrevention = getFlashPreventionPlugin();
Expand Down
10 changes: 9 additions & 1 deletion scully/pluginManagement/pluginWrap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {performance} from 'perf_hooks';
import {pluginsError} from '../utils/cli-options';
import {logError, yellow} from '../utils/log';
import {performanceIds} from '../utils/performanceIds';
import {backupData, routeConfigData} from './pluginConfig';
Expand Down Expand Up @@ -39,8 +40,15 @@ export async function wrap(type: string, name: string, plugin: (...args) => any
}
result = await plugin(...args);
} catch (e) {
logError(` The ${type} plugin "${yellow(name)} has thrown the below error, results are ignored.`);
logError(
` The ${type} plugin "${yellow(name)} has thrown the below error,
while trying to render route "${yellow(currentRoute || 'unknown')}"
${pluginsError ? 'Scully will exit' : 'Results are ignored.'}`
);
console.error(e);
if (pluginsError) {
process.exit(15);
}
} finally {
if (customConfig) {
plugin[configData] = plugin[backupData];
Expand Down
1 change: 1 addition & 0 deletions scully/renderPlugins/executePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {plugins} from '../pluginManagement/pluginRepository';
import {HandledRoute} from '../routerPlugins/addOptionalRoutesPlugin';
import {logError, yellow} from '../utils/log';
import {puppeteerRender} from './puppeteerRenderPlugin';
import {pluginsError} from '../utils/cli-options';

export const executePluginsForRoute = async (route: HandledRoute) => {
/** make one array with all handlers for this route, filter out empty ones */
Expand Down
18 changes: 16 additions & 2 deletions scully/utils/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ export const {showBrowser, path, port, folder, sge} = yargs
.alias('sge', 'showGuessError')
.describe('sb', 'dumps the error from guess to the console').argv;

export const {configFileName, project, baseFilter, scanRoutes, pjFirst, hl, serverTimeout} = yargs
export const {
configFileName,
project,
baseFilter,
scanRoutes,
pjFirst,
hl,
serverTimeout,
pluginsError,
} = yargs
/** config file */
.string('cf')
.alias('cf', 'configFile')
Expand Down Expand Up @@ -115,7 +124,12 @@ export const {configFileName, project, baseFilter, scanRoutes, pjFirst, hl, serv
.string('hl')
.alias('hl', 'highlight')
.default('hl', false)
.describe('bf', 'provide a minimatch glob for the unhandled routes').argv;
.describe('hl', 'provide a minimatch glob for the unhandled routes')
/** Exit Scully with plugin error */
.boolean('pe')
.alias('pe', 'pluginsError')
.default('pe', true)
.describe('pe', "Exit scully's run when exist an error in a plugin").argv;

yargs.help();

Expand Down