Skip to content

Commit

Permalink
Merge pull request #244 from codeceptjs/error-handling-improvement
Browse files Browse the repository at this point in the history
fix: handle error nicely
  • Loading branch information
kobenguyent authored Feb 5, 2024
2 parents 6cc7265 + c268858 commit d1295d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
57 changes: 31 additions & 26 deletions lib/api/list-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,38 @@ const path = require('path');
const codeceptjsFactory = require('../model/codeceptjs-factory');
const { methodsOfObject } = require('codeceptjs/lib/utils');

module.exports = (req, res) => {
const { container } = codeceptjsFactory.getInstance();
const docsWebApiFolderPath = path.join(path.dirname(require.resolve('codeceptjs')), '/../docs/webapi');
const docFileList = [];
fs.readdirSync(docsWebApiFolderPath).map(fileName => {
docFileList.push(path.basename(fileName,'.mustache'));
});
const helpers = container.helpers();
const actions = {};
for (const name in helpers) {
const helper = helpers[name];
methodsOfObject(helper).forEach((action) => {

if(docFileList.includes(action)) {
let filePath = path.join(docsWebApiFolderPath, action + '.mustache');
let fn = helper[action].toString().replace(/\n/g,' ').replace(/\{.*\}/gm,'{}');
try{
let docData = fs.readFileSync(filePath, 'utf-8');
let params = parser.parse(fn);
actions[action] = { params: params, actionDoc: docData };
} catch(err) {
debug('Error in fetching doc for file content', fn, err);
module.exports = (req, res) => {
try {
const { container } = codeceptjsFactory.getInstance();
const docsWebApiFolderPath = path.join(path.dirname(require.resolve('codeceptjs')), '/../docs/webapi');
const docFileList = [];
fs.readdirSync(docsWebApiFolderPath).map(fileName => {
docFileList.push(path.basename(fileName,'.mustache'));
});
const helpers = container.helpers();
const actions = {};
for (const name in helpers) {
const helper = helpers[name];
methodsOfObject(helper).forEach((action) => {

if(docFileList.includes(action)) {
let filePath = path.join(docsWebApiFolderPath, action + '.mustache');
let fn = helper[action].toString().replace(/\n/g,' ').replace(/\{.*\}/gm,'{}');
try{
let docData = fs.readFileSync(filePath, 'utf-8');
let params = parser.parse(fn);
actions[action] = { params: params, actionDoc: docData };
} catch(err) {
debug('Error in fetching doc for file content', fn, err);
}
}
}

});
});
}

res.send({ actions });
} catch (e) {
debug(`Could not fetch documentation due to: ${e.message}`);
}

res.send({ actions });

};
4 changes: 3 additions & 1 deletion lib/model/codeceptjs-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ module.exports = new class CodeceptjsFactory {
const helpersConfig = config.get('helpers');

for (const helperName in container.helpers()) {
if (helpersConfig[helperName]) {
try {
container.helpers(helperName)._setConfig(helpersConfig[helperName]);
} catch (e) {
debug(`Cannot run _setConfig due to: ${e.message}`);
}
}

Expand Down

0 comments on commit d1295d2

Please sign in to comment.