mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-02-04 07:13: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 (
|
const initThrowsErrors = function (
|
||||||
config?: MermaidConfig,
|
config?: MermaidConfig,
|
||||||
// eslint-disable-next-line no-undef
|
// eslint-disable-next-line no-undef
|
||||||
@ -144,17 +169,7 @@ const initThrowsErrors = function (
|
|||||||
element
|
element
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.warn('Catching Error (bootstrap)', error);
|
handleError(error, errors, mermaid.parseError);
|
||||||
const mermaidError: DetailedError = {
|
|
||||||
error,
|
|
||||||
str: error.str,
|
|
||||||
hash: error.hash,
|
|
||||||
message: error.str,
|
|
||||||
};
|
|
||||||
if (typeof mermaid.parseError === 'function') {
|
|
||||||
mermaid.parseError(mermaidError);
|
|
||||||
}
|
|
||||||
errors.push(mermaidError);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (errors.length > 0) {
|
if (errors.length > 0) {
|
||||||
@ -285,17 +300,7 @@ const initThrowsErrorsAsync = async function (
|
|||||||
element
|
element
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.warn('Catching Error (bootstrap)', error);
|
handleError(error, errors, mermaid.parseError);
|
||||||
const mermaidError: DetailedError = {
|
|
||||||
error,
|
|
||||||
str: error.str,
|
|
||||||
hash: error.hash,
|
|
||||||
message: error.str,
|
|
||||||
};
|
|
||||||
if (typeof mermaid.parseError === 'function') {
|
|
||||||
mermaid.parseError(mermaidError);
|
|
||||||
}
|
|
||||||
errors.push(mermaidError);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (errors.length > 0) {
|
if (errors.length > 0) {
|
||||||
|
@ -825,7 +825,7 @@ export const sanitizeCss = (str) => {
|
|||||||
export interface DetailedError {
|
export interface DetailedError {
|
||||||
str: string;
|
str: string;
|
||||||
hash: any;
|
hash: any;
|
||||||
error?: Error;
|
error?: any;
|
||||||
message?: string;
|
message?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user