From c8db5726362eca2d75bf2af4d546401ecc46a72b Mon Sep 17 00:00:00 2001 From: ramki-bruno Date: Mon, 21 Apr 2025 19:39:29 +0530 Subject: [PATCH] Improved error-toast for creating and importing collections - Added a util for formatting the error form IPC - Updated Toast global styles to prevent text overflow. Whenever long file paths are shown, it overflows the Toast container. --- .../src/components/Sidebar/CreateCollection/index.js | 4 +++- .../bruno-app/src/components/Sidebar/TitleBar/index.js | 4 +++- packages/bruno-app/src/providers/Toaster/index.js | 10 +++++++++- packages/bruno-app/src/utils/common/error.js | 7 +++++++ packages/bruno-app/src/utils/common/index.js | 4 ++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/bruno-app/src/components/Sidebar/CreateCollection/index.js b/packages/bruno-app/src/components/Sidebar/CreateCollection/index.js index d5466d172..7b331c42c 100644 --- a/packages/bruno-app/src/components/Sidebar/CreateCollection/index.js +++ b/packages/bruno-app/src/components/Sidebar/CreateCollection/index.js @@ -11,6 +11,8 @@ import PathDisplay from 'components/PathDisplay/index'; import { useState } from 'react'; import { IconArrowBackUp, IconEdit } from '@tabler/icons'; import Help from 'components/Help'; +import { multiLineMsg } from "utils/common"; +import { formatIpcError } from "utils/common/error"; const CreateCollection = ({ onClose }) => { const inputRef = useRef(); @@ -45,7 +47,7 @@ const CreateCollection = ({ onClose }) => { toast.success('Collection created!'); onClose(); }) - .catch((e) => toast.error('An error occurred while creating the collection - ' + e)); + .catch((e) => toast.error(multiLineMsg('An error occurred while creating the collection', formatIpcError(e)))); } }); diff --git a/packages/bruno-app/src/components/Sidebar/TitleBar/index.js b/packages/bruno-app/src/components/Sidebar/TitleBar/index.js index 1adfd3366..65badf3aa 100644 --- a/packages/bruno-app/src/components/Sidebar/TitleBar/index.js +++ b/packages/bruno-app/src/components/Sidebar/TitleBar/index.js @@ -11,6 +11,8 @@ import { useDispatch } from 'react-redux'; import { showHomePage } from 'providers/ReduxStore/slices/app'; import { openCollection, importCollection } from 'providers/ReduxStore/slices/collections/actions'; import StyledWrapper from './StyledWrapper'; +import { multiLineMsg } from "utils/common"; +import { formatIpcError } from "utils/common/error"; const TitleBar = () => { const [importedCollection, setImportedCollection] = useState(null); @@ -35,7 +37,7 @@ const TitleBar = () => { }) .catch((err) => { console.error(err); - toast.error('An error occurred while importing the collection. Check the logs for more information.'); + toast.error(multiLineMsg('An error occurred while importing the collection.', formatIpcError(err))); }); }; diff --git a/packages/bruno-app/src/providers/Toaster/index.js b/packages/bruno-app/src/providers/Toaster/index.js index 1ae25764c..10dab3297 100644 --- a/packages/bruno-app/src/providers/Toaster/index.js +++ b/packages/bruno-app/src/providers/Toaster/index.js @@ -7,9 +7,17 @@ export const ToastContext = React.createContext(); export const ToastProvider = (props) => { const { storedTheme } = useTheme(); - const toastOptions = { duration: 2000 }; + const toastOptions = { + duration: 2000, + style: { + // Break long word like file-path, URL etc. to prevent overflow + overflowWrap: 'anywhere' + } + }; + if (storedTheme === 'dark') { toastOptions.style = { + ...toastOptions.style, borderRadius: '10px', background: '#3d3d3d', color: '#fff' diff --git a/packages/bruno-app/src/utils/common/error.js b/packages/bruno-app/src/utils/common/error.js index e81e3fadc..676aeb932 100644 --- a/packages/bruno-app/src/utils/common/error.js +++ b/packages/bruno-app/src/utils/common/error.js @@ -34,3 +34,10 @@ export const toastError = (error, defaultErrorMsg = 'An error occurred') => { return toast.error(errorMsg); }; + +export function formatIpcError(error) { + if (!error?.message) return ''; // Return empty string to avoid errors which say `null` or `undefined` + // https://github.com/electron/electron/blob/659e79fc08c6ffc2f7506dd1358918d97d240147/lib/renderer/api/ipc-renderer.ts#L24-L30 + // There is no other way to get rid of this error prefix as of now. + return error.message.replace(/^Error invoking remote method '.+?': (Error: )?/, ''); +} diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index 8bafbb8f9..970e33abc 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -181,4 +181,8 @@ export const getEncoding = (headers) => { // Parse the charset from content type: https://stackoverflow.com/a/33192813 const charsetMatch = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(headers?.['content-type'] || ''); return charsetMatch?.[1]; +} + +export const multiLineMsg = (...messages) => { + return messages.filter(m => m !== undefined && m !== null && m !== '').join('\n'); } \ No newline at end of file