Skip to content

Commit 2ecc216

Browse files
feat(scully): add exit by default when have a plugin error and cli option (#496)
* feat(scully): add exit by default when have a plugin error. And add the flag for dont use this funct * test(scully): add test for errors & undefined * refactor(scully): make the 'exit on error' work on _all_ plugins not only render ones Co-authored-by: sanderelias <sanderelias+git@gmail.com>
1 parent 407afbc commit 2ecc216

6 files changed

Lines changed: 50 additions & 3 deletions

File tree

docs/scully-cmd-line.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The Scully CLI has the following options available:
2525
- [ssl-key](#ssl-key)
2626
- [highlight](#highlight)
2727
- [tds](#tds)
28+
- [pluginsError](#pluginsError)
2829

2930
## Serve
3031

@@ -163,3 +164,12 @@ The following APIs are supported on the test data server:
163164
- `/posts` - Returns a list of posts
164165
- `/posts/:id` - Returns a post by id
165166
- `/slow/:delay` - Returns 200 code after a delay has gone by. Eg: `/slow/2000` takes 2 seconds.
167+
168+
## pluginsError
169+
170+
```bash
171+
npx scully --pluginsError=false
172+
```
173+
174+
Show the error from the plugin, but continue rendering.
175+
If you dont use the flag (by default is true) when you have an error into any plugin, the scully's run exit.

plugins/demos/errorPlugin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const {registerPlugin, logWarn} = require('../../dist/scully');
2+
3+
const errorPlugin = async (html, options) => {
4+
try {
5+
throw new Error(`new error`);
6+
} catch (e) {
7+
logWarn(`errorPlugin works!`);
8+
}
9+
return undefined;
10+
};
11+
12+
const validator = async config => [];
13+
registerPlugin('render', 'errorPlugin', errorPlugin, validator);

scully.sampleBlog.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {getFlashPreventionPlugin} = require('./dist/scully-plugin-flash-preventio
33
require('./plugins/demos/extra-plugin.js');
44
require('./plugins/demos/tocPlugin');
55
require('./plugins/demos/voidPlugin');
6+
require('./plugins/demos/errorPlugin');
67
const {setPluginConfig} = require('./dist/scully');
78

89
const FlashPrevention = getFlashPreventionPlugin();

scully/pluginManagement/pluginWrap.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {performance} from 'perf_hooks';
2+
import {pluginsError} from '../utils/cli-options';
23
import {logError, yellow} from '../utils/log';
34
import {performanceIds} from '../utils/performanceIds';
45
import {backupData, routeConfigData} from './pluginConfig';
@@ -39,8 +40,15 @@ export async function wrap(type: string, name: string, plugin: (...args) => any
3940
}
4041
result = await plugin(...args);
4142
} catch (e) {
42-
logError(` The ${type} plugin "${yellow(name)} has thrown the below error, results are ignored.`);
43+
logError(
44+
` The ${type} plugin "${yellow(name)} has thrown the below error,
45+
while trying to render route "${yellow(currentRoute || 'unknown')}"
46+
${pluginsError ? 'Scully will exit' : 'Results are ignored.'}`
47+
);
4348
console.error(e);
49+
if (pluginsError) {
50+
process.exit(15);
51+
}
4452
} finally {
4553
if (customConfig) {
4654
plugin[configData] = plugin[backupData];

scully/renderPlugins/executePlugins.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {plugins} from '../pluginManagement/pluginRepository';
33
import {HandledRoute} from '../routerPlugins/addOptionalRoutesPlugin';
44
import {logError, yellow} from '../utils/log';
55
import {puppeteerRender} from './puppeteerRenderPlugin';
6+
import {pluginsError} from '../utils/cli-options';
67

78
export const executePluginsForRoute = async (route: HandledRoute) => {
89
/** make one array with all handlers for this route, filter out empty ones */

scully/utils/cli-options.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,16 @@ export const {showBrowser, path, port, folder, sge} = yargs
7474
.alias('sge', 'showGuessError')
7575
.describe('sb', 'dumps the error from guess to the console').argv;
7676

77-
export const {configFileName, project, baseFilter, scanRoutes, pjFirst, hl, serverTimeout} = yargs
77+
export const {
78+
configFileName,
79+
project,
80+
baseFilter,
81+
scanRoutes,
82+
pjFirst,
83+
hl,
84+
serverTimeout,
85+
pluginsError,
86+
} = yargs
7887
/** config file */
7988
.string('cf')
8089
.alias('cf', 'configFile')
@@ -115,7 +124,12 @@ export const {configFileName, project, baseFilter, scanRoutes, pjFirst, hl, serv
115124
.string('hl')
116125
.alias('hl', 'highlight')
117126
.default('hl', false)
118-
.describe('bf', 'provide a minimatch glob for the unhandled routes').argv;
127+
.describe('hl', 'provide a minimatch glob for the unhandled routes')
128+
/** Exit Scully with plugin error */
129+
.boolean('pe')
130+
.alias('pe', 'pluginsError')
131+
.default('pe', true)
132+
.describe('pe', "Exit scully's run when exist an error in a plugin").argv;
119133

120134
yargs.help();
121135

0 commit comments

Comments
 (0)