Skip to content

Commit

Permalink
fix: cli invalid bru file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lohxt1 committed Dec 10, 2024
1 parent 0c574ae commit 5e6da3e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
36 changes: 28 additions & 8 deletions packages/bruno-cli/src/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,16 @@ const createCollectionFromPath = (collectionPath) => {
const folderBruFileExists = fs.existsSync(folderBruFilePath);
if(folderBruFileExists) {
const folderBruContent = fs.readFileSync(folderBruFilePath, 'utf8');
let folderBruJson = collectionBruToJson(folderBruContent);
folderItem.root = folderBruJson;
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);
}
Expand All @@ -136,12 +144,24 @@ const createCollectionFromPath = (collectionPath) => {

if (!stats.isDirectory() && path.extname(filePath) === '.bru') {
const bruContent = fs.readFileSync(filePath, 'utf8');
const bruJson = bruToJson(bruContent);
currentDirItems.push({
name: file,
pathname: filePath,
...bruJson
});
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
Expand Down
5 changes: 5 additions & 0 deletions packages/bruno-cli/src/runner/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions packages/bruno-cli/src/utils/bru.js
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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);
}
};

Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
};
Expand Down

0 comments on commit 5e6da3e

Please sign in to comment.