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,8 +120,16 @@ const createCollectionFromPath = (collectionPath) => {
const folderBruFileExists = fs.existsSync(folderBruFilePath); const folderBruFileExists = fs.existsSync(folderBruFilePath);
if(folderBruFileExists) { if(folderBruFileExists) {
const folderBruContent = fs.readFileSync(folderBruFilePath, 'utf8'); const folderBruContent = fs.readFileSync(folderBruFilePath, 'utf8');
let folderBruJson = collectionBruToJson(folderBruContent); try {
folderItem.root = folderBruJson; 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); currentDirItems.push(folderItem);
} }
@ -136,12 +144,24 @@ const createCollectionFromPath = (collectionPath) => {
if (!stats.isDirectory() && path.extname(filePath) === '.bru') { if (!stats.isDirectory() && path.extname(filePath) === '.bru') {
const bruContent = fs.readFileSync(filePath, 'utf8'); const bruContent = fs.readFileSync(filePath, 'utf8');
const bruJson = bruToJson(bruContent); try {
currentDirItems.push({ const bruJson = bruToJson(bruContent, true);
name: file, currentDirItems.push({
pathname: filePath, name: file,
...bruJson 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 return currentDirItems

View File

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

View File

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