From 5e6da3e40ca31824ffed3e92ca6a5f767f2e20ca Mon Sep 17 00:00:00 2001 From: lohxt1 Date: Tue, 10 Dec 2024 15:24:06 +0530 Subject: [PATCH] fix: cli invalid bru file handling --- packages/bruno-cli/src/commands/run.js | 36 ++++++++++++++----- .../bruno-cli/src/runner/prepare-request.js | 5 +++ packages/bruno-cli/src/utils/bru.js | 13 ++++--- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/packages/bruno-cli/src/commands/run.js b/packages/bruno-cli/src/commands/run.js index 1961b2ac..58f7cb35 100644 --- a/packages/bruno-cli/src/commands/run.js +++ b/packages/bruno-cli/src/commands/run.js @@ -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); } @@ -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 diff --git a/packages/bruno-cli/src/runner/prepare-request.js b/packages/bruno-cli/src/runner/prepare-request.js index 8c9daa43..2c259695 100644 --- a/packages/bruno-cli/src/runner/prepare-request.js +++ b/packages/bruno-cli/src/runner/prepare-request.js @@ -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); diff --git a/packages/bruno-cli/src/utils/bru.js b/packages/bruno-cli/src/utils/bru.js index 75707fef..793d43ae 100644 --- a/packages/bruno-cli/src/utils/bru.js +++ b/packages/bruno-cli/src/utils/bru.js @@ -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); } };