fix: cli invalid bru file handling

This commit is contained in:
lohxt1 2024-12-10 15:24:06 +05:30
parent 0c574aeb1e
commit 5e6da3e40c
3 changed files with 41 additions and 13 deletions

View File

@ -120,9 +120,17 @@ const createCollectionFromPath = (collectionPath) => {
const folderBruFileExists = fs.existsSync(folderBruFilePath);
if(folderBruFileExists) {
const folderBruContent = fs.readFileSync(folderBruFilePath, 'utf8');
let folderBruJson = collectionBruToJson(folderBruContent);
try {
let folderBruJson = collectionBruToJson(folderBruContent, true);
folderItem.root = folderBruJson;
}
catch(err) {
console.error(chalk.red("Invalid folder bru file:", filePath));
console.error(chalk.red(err));
folderItem.error = true;
folderItem.errorMessage = `Invalid folder.bru bru file:", ${filePath} \n ${err?.message}`;
}
}
currentDirItems.push(folderItem);
}
}
@ -136,13 +144,25 @@ const createCollectionFromPath = (collectionPath) => {
if (!stats.isDirectory() && path.extname(filePath) === '.bru') {
const bruContent = fs.readFileSync(filePath, 'utf8');
const bruJson = bruToJson(bruContent);
try {
const bruJson = bruToJson(bruContent, true);
currentDirItems.push({
name: file,
pathname: filePath,
...bruJson
});
}
catch(err) {
console.error(chalk.red("Invalid bru file:", filePath));
console.error(chalk.red(err));
currentDirItems.push({
name: file,
pathname: filePath,
error: true,
errorMessage: `Invalid bru file:", ${filePath} \n ${err?.message}`
});
}
}
}
return currentDirItems
};

View File

@ -213,6 +213,11 @@ const prepareRequest = (item = {}, collection = {}) => {
const scriptFlow = brunoConfig?.scripts?.flow ?? 'sandwich';
const requestTreePath = getTreePathFromCollectionToItem(collection, item);
if (requestTreePath && requestTreePath.length > 0) {
requestTreePath?.forEach((r) => {
if(r?.error) {
throw new Error(r?.errorMessage);
}
});
mergeHeaders(collection, request, requestTreePath);
mergeScripts(collection, request, requestTreePath, scriptFlow);
mergeVars(collection, request, requestTreePath);

View File

@ -1,7 +1,7 @@
const _ = require('lodash');
const { bruToEnvJsonV2, bruToJsonV2, collectionBruToJson: _collectionBruToJson } = require('@usebruno/lang');
const collectionBruToJson = (bru) => {
const collectionBruToJson = (bru, throwOnError) => {
try {
const json = _collectionBruToJson(bru);
@ -16,8 +16,9 @@ const collectionBruToJson = (bru) => {
};
return transformedJson;
} catch (error) {
return Promise.reject(error);
} catch (err) {
if (throwOnError) throw new Error(err);
return Promise.reject(err);
}
};
@ -30,7 +31,7 @@ const collectionBruToJson = (bru) => {
* @param {string} bru The BRU file content.
* @returns {object} The JSON representation of the BRU file.
*/
const bruToJson = (bru) => {
const bruToJson = (bru, throwOnError) => {
try {
const json = bruToJsonV2(bru);
@ -68,14 +69,16 @@ const bruToJson = (bru) => {
return transformedJson;
} catch (err) {
if (throwOnError) throw new Error(err);
return Promise.reject(err);
}
};
const bruToEnvJson = (bru) => {
const bruToEnvJson = (bru, throwOnError) => {
try {
return bruToEnvJsonV2(bru);
} catch (err) {
if (throwOnError) throw new Error(err);
return Promise.reject(err);
}
};