fix: Error handling

This commit is contained in:
Sidharth Vinod 2022-10-19 20:30:07 +05:30
parent d17aa6ecdd
commit 56a8068a7f
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
2 changed files with 28 additions and 23 deletions

View File

@ -65,6 +65,31 @@ const init = async function (
}
};
// eslint-disable-next-line @typescript-eslint/ban-types
const handleError = (error: unknown, errors: DetailedError[], parseError?: Function) => {
log.warn(error);
if (isDetailedError(error)) {
// handle case where error string and hash were
// wrapped in object like`const error = { str, hash };`
if (parseError) {
parseError(error.str, error.hash);
}
errors.push({ ...error, message: error.str, error });
} else {
// assume it is just error string and pass it on
if (parseError) {
parseError(error);
}
if (error instanceof Error) {
errors.push({
str: error.message,
message: error.message,
hash: error.name,
error,
});
}
}
};
const initThrowsErrors = function (
config?: MermaidConfig,
// eslint-disable-next-line no-undef
@ -144,17 +169,7 @@ const initThrowsErrors = function (
element
);
} catch (error) {
log.warn('Catching Error (bootstrap)', error);
const mermaidError: DetailedError = {
error,
str: error.str,
hash: error.hash,
message: error.str,
};
if (typeof mermaid.parseError === 'function') {
mermaid.parseError(mermaidError);
}
errors.push(mermaidError);
handleError(error, errors, mermaid.parseError);
}
}
if (errors.length > 0) {
@ -285,17 +300,7 @@ const initThrowsErrorsAsync = async function (
element
);
} catch (error) {
log.warn('Catching Error (bootstrap)', error);
const mermaidError: DetailedError = {
error,
str: error.str,
hash: error.hash,
message: error.str,
};
if (typeof mermaid.parseError === 'function') {
mermaid.parseError(mermaidError);
}
errors.push(mermaidError);
handleError(error, errors, mermaid.parseError);
}
}
if (errors.length > 0) {

View File

@ -825,7 +825,7 @@ export const sanitizeCss = (str) => {
export interface DetailedError {
str: string;
hash: any;
error?: Error;
error?: any;
message?: string;
}