mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
fix: Error handling
This commit is contained in:
parent
d17aa6ecdd
commit
56a8068a7f
@ -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) {
|
||||
|
@ -825,7 +825,7 @@ export const sanitizeCss = (str) => {
|
||||
export interface DetailedError {
|
||||
str: string;
|
||||
hash: any;
|
||||
error?: Error;
|
||||
error?: any;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user