Skip to content

Commit d1295d2

Browse files
authored
Merge pull request #244 from codeceptjs/error-handling-improvement
fix: handle error nicely
2 parents 6cc7265 + c268858 commit d1295d2

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

lib/api/list-actions.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,38 @@ const path = require('path');
77
const codeceptjsFactory = require('../model/codeceptjs-factory');
88
const { methodsOfObject } = require('codeceptjs/lib/utils');
99

10-
module.exports = (req, res) => {
11-
const { container } = codeceptjsFactory.getInstance();
12-
const docsWebApiFolderPath = path.join(path.dirname(require.resolve('codeceptjs')), '/../docs/webapi');
13-
const docFileList = [];
14-
fs.readdirSync(docsWebApiFolderPath).map(fileName => {
15-
docFileList.push(path.basename(fileName,'.mustache'));
16-
});
17-
const helpers = container.helpers();
18-
const actions = {};
19-
for (const name in helpers) {
20-
const helper = helpers[name];
21-
methodsOfObject(helper).forEach((action) => {
22-
23-
if(docFileList.includes(action)) {
24-
let filePath = path.join(docsWebApiFolderPath, action + '.mustache');
25-
let fn = helper[action].toString().replace(/\n/g,' ').replace(/\{.*\}/gm,'{}');
26-
try{
27-
let docData = fs.readFileSync(filePath, 'utf-8');
28-
let params = parser.parse(fn);
29-
actions[action] = { params: params, actionDoc: docData };
30-
} catch(err) {
31-
debug('Error in fetching doc for file content', fn, err);
10+
module.exports = (req, res) => {
11+
try {
12+
const { container } = codeceptjsFactory.getInstance();
13+
const docsWebApiFolderPath = path.join(path.dirname(require.resolve('codeceptjs')), '/../docs/webapi');
14+
const docFileList = [];
15+
fs.readdirSync(docsWebApiFolderPath).map(fileName => {
16+
docFileList.push(path.basename(fileName,'.mustache'));
17+
});
18+
const helpers = container.helpers();
19+
const actions = {};
20+
for (const name in helpers) {
21+
const helper = helpers[name];
22+
methodsOfObject(helper).forEach((action) => {
23+
24+
if(docFileList.includes(action)) {
25+
let filePath = path.join(docsWebApiFolderPath, action + '.mustache');
26+
let fn = helper[action].toString().replace(/\n/g,' ').replace(/\{.*\}/gm,'{}');
27+
try{
28+
let docData = fs.readFileSync(filePath, 'utf-8');
29+
let params = parser.parse(fn);
30+
actions[action] = { params: params, actionDoc: docData };
31+
} catch(err) {
32+
debug('Error in fetching doc for file content', fn, err);
33+
}
3234
}
33-
}
3435

35-
});
36+
});
37+
}
38+
39+
res.send({ actions });
40+
} catch (e) {
41+
debug(`Could not fetch documentation due to: ${e.message}`);
3642
}
37-
38-
res.send({ actions });
43+
3944
};

lib/model/codeceptjs-factory.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ module.exports = new class CodeceptjsFactory {
161161
const helpersConfig = config.get('helpers');
162162

163163
for (const helperName in container.helpers()) {
164-
if (helpersConfig[helperName]) {
164+
try {
165165
container.helpers(helperName)._setConfig(helpersConfig[helperName]);
166+
} catch (e) {
167+
debug(`Cannot run _setConfig due to: ${e.message}`);
166168
}
167169
}
168170

0 commit comments

Comments
 (0)