Files
bruno/packages/bruno-app/src/utils/common/error.js
ramki-bruno b699088dd6 Create/Import collection UX improvements (#4540)
* Fix: Improve UX for selecting location when create/import collection

Allow editing the input path where previously the `<input>` is marked
`readonly`.
Also this will allow automating test using Playwright.

* Fix: Import-collection select-location Modal closes on error

* 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.
2025-05-13 14:25:35 +05:30

45 lines
1.3 KiB
JavaScript

import toast from 'react-hot-toast';
// levels: 'warning, error'
export class BrunoError extends Error {
constructor(message, level) {
super(message);
this.name = 'BrunoError';
this.level = level || 'error';
}
}
export const parseError = (error, defaultErrorMsg = 'An error occurred') => {
if (error instanceof BrunoError) {
return error.message;
}
return error.message ? error.message : defaultErrorMsg;
};
export const toastError = (error, defaultErrorMsg = 'An error occurred') => {
let errorMsg = parseError(error, defaultErrorMsg);
if (error instanceof BrunoError) {
if (error.level === 'warning') {
return toast(errorMsg, {
icon: '⚠️',
duration: 3000
});
}
return toast.error(errorMsg, {
duration: 3000
});
}
return toast.error(errorMsg);
};
export function formatIpcError(error) {
if (!(error instanceof Error)) return error;
if (!error?.message) return ''; // Avoid returning `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: )?/, '');
}