Files
bruno/packages/bruno-app/src/utils/common/error.js
Bram Hoven ea3a9394c9 Improve error for workspace deletion (#39)
* Move dmg-license to optionalDependencies so it can be installed on windows
* Extend toastError to accept a default error message
* Throw BrunoError when in deleteWorkspace
* Handle errors with the toastError
* Use existing parseError for getting errorMsg in toastError
2022-10-21 23:57:42 +05:30

37 lines
842 B
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);
};