Skip to content

Commit

Permalink
Merge pull request #3643 from Some-Dood/refactor-handle-error
Browse files Browse the repository at this point in the history
feat: make `parseError` function more type-safe
  • Loading branch information
sidharthv96 committed Oct 14, 2022
2 parents 57b883c + 7391baa commit c913fc8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
42 changes: 23 additions & 19 deletions packages/mermaid/src/Diagram.ts
Expand Up @@ -2,15 +2,17 @@ import * as configApi from './config';
import { log } from './logger';
import { getDiagram, registerDiagram } from './diagram-api/diagramAPI';
import { detectType, getDiagramLoader } from './diagram-api/detectType';
import { isDetailedError } from './utils';
import { isDetailedError, type DetailedError } from './utils';

export type ParseErrorFunction = (err: string | DetailedError, hash?: any) => void;

export class Diagram {
type = 'graph';
parser;
renderer;
db;
private detectTypeFailed = false;
// eslint-disable-next-line @typescript-eslint/ban-types
constructor(public txt: string, parseError?: Function) {
constructor(public txt: string, parseError?: ParseErrorFunction) {
const cnf = configApi.getConfig();
this.txt = txt;
try {
Expand All @@ -37,8 +39,7 @@ export class Diagram {
this.parse(this.txt, parseError);
}

// eslint-disable-next-line @typescript-eslint/ban-types
parse(text: string, parseError?: Function): boolean {
parse(text: string, parseError?: ParseErrorFunction): boolean {
if (this.detectTypeFailed) {
return false;
}
Expand All @@ -53,23 +54,24 @@ export class Diagram {
return false;
}

// eslint-disable-next-line @typescript-eslint/ban-types
handleError(error: unknown, parseError?: Function) {
handleError(error: unknown, parseError?: ParseErrorFunction) {
// Is this the correct way to access mermiad's parseError()
// method ? (or global.mermaid.parseError()) ?
if (parseError) {
if (isDetailedError(error)) {
// handle case where error string and hash were
// wrapped in object like`const error = { str, hash };`
parseError(error.str, error.hash);
} else {
// assume it is just error string and pass it on
parseError(error);
}
} else {

if (parseError === undefined) {
// No mermaid.parseError() handler defined, so re-throw it
throw error;
}

if (isDetailedError(error)) {
// Handle case where error string and hash were
// wrapped in object like`const error = { str, hash };`
parseError(error.str, error.hash);
return;
}

// Otherwise, assume it is just an error string and pass it on
parseError(error as string);
}

getParser() {
Expand All @@ -81,8 +83,10 @@ export class Diagram {
}
}

// eslint-disable-next-line @typescript-eslint/ban-types
export const getDiagramFromText = async (txt: string, parseError?: Function): Promise<Diagram> => {
export const getDiagramFromText = async (
txt: string,
parseError?: ParseErrorFunction
): Promise<Diagram> => {
const type = detectType(txt, configApi.getConfig());
try {
// Trying to find the diagram
Expand Down
5 changes: 2 additions & 3 deletions packages/mermaid/src/__mocks__/mermaidAPI.ts
Expand Up @@ -6,7 +6,7 @@
import * as configApi from '../config';
import { vi } from 'vitest';
import { addDiagrams } from '../diagram-api/diagram-orchestration';
import Diagram from '../Diagram';
import Diagram, { type ParseErrorFunction } from '../Diagram';

// Normally, we could just do the following to get the original `parse()`
// implementation, however, requireActual returns a promise and it's not documented how to use withing mock file.
Expand All @@ -15,8 +15,7 @@ import Diagram from '../Diagram';
* @param text
* @param parseError
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function parse(text: string, parseError?: Function): boolean {
function parse(text: string, parseError?: ParseErrorFunction): boolean {
addDiagrams();
const diagram = new Diagram(text, parseError);
return diagram.parse(text, parseError);
Expand Down
6 changes: 3 additions & 3 deletions packages/mermaid/src/mermaid.ts
Expand Up @@ -8,6 +8,7 @@ import utils from './utils';
import { mermaidAPI } from './mermaidAPI';
import { addDetector } from './diagram-api/detectType';
import { isDetailedError } from './utils';
import type { ParseErrorFunction } from './Diagram';

/**
* ## init
Expand Down Expand Up @@ -62,7 +63,7 @@ const init = async function (
log.warn(e.str);
}
if (mermaid.parseError) {
mermaid.parseError(e);
mermaid.parseError(e as string);
}
}
};
Expand Down Expand Up @@ -212,8 +213,7 @@ const parse = (txt: string) => {
const mermaid: {
startOnLoad: boolean;
diagrams: any;
// eslint-disable-next-line @typescript-eslint/ban-types
parseError?: Function;
parseError?: ParseErrorFunction;
mermaidAPI: typeof mermaidAPI;
parse: typeof parse;
render: typeof mermaidAPI.render;
Expand Down
5 changes: 2 additions & 3 deletions packages/mermaid/src/mermaidAPI.ts
Expand Up @@ -22,7 +22,7 @@ import classDb from './diagrams/class/classDb';
import flowDb from './diagrams/flowchart/flowDb';
import flowRenderer from './diagrams/flowchart/flowRenderer';
import ganttDb from './diagrams/gantt/ganttDb';
import Diagram, { getDiagramFromText } from './Diagram';
import Diagram, { getDiagramFromText, type ParseErrorFunction } from './Diagram';
import errorRenderer from './diagrams/error/errorRenderer';
import { attachFunctions } from './interactionDb';
import { log, setLogLevel } from './logger';
Expand All @@ -37,8 +37,7 @@ import { evaluate } from './diagrams/common/common';
* @param text
* @param parseError
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function parse(text: string, parseError?: Function): boolean {
function parse(text: string, parseError?: ParseErrorFunction): boolean {
addDiagrams();
const diagram = new Diagram(text, parseError);
return diagram.parse(text, parseError);
Expand Down

0 comments on commit c913fc8

Please sign in to comment.