mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
feat(arch): improved error handling
This commit is contained in:
parent
10682ef31f
commit
b911bd3e42
@ -27,27 +27,44 @@ export const DEFAULT_ARCHITECTURE_DB: ArchitectureFields = {
|
|||||||
services: [],
|
services: [],
|
||||||
groups: [],
|
groups: [],
|
||||||
lines: [],
|
lines: [],
|
||||||
cnt: 0,
|
registeredIds: {},
|
||||||
config: DEFAULT_ARCHITECTURE_CONFIG,
|
config: DEFAULT_ARCHITECTURE_CONFIG,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
let services = DEFAULT_ARCHITECTURE_DB.services;
|
let services = DEFAULT_ARCHITECTURE_DB.services;
|
||||||
let groups = DEFAULT_ARCHITECTURE_DB.groups;
|
let groups = DEFAULT_ARCHITECTURE_DB.groups;
|
||||||
let lines = DEFAULT_ARCHITECTURE_DB.lines;
|
let lines = DEFAULT_ARCHITECTURE_DB.lines;
|
||||||
|
let registeredIds = DEFAULT_ARCHITECTURE_DB.registeredIds;
|
||||||
let elements: Record<string, D3Element> = {};
|
let elements: Record<string, D3Element> = {};
|
||||||
let cnt = DEFAULT_ARCHITECTURE_DB.cnt;
|
|
||||||
|
|
||||||
const clear = (): void => {
|
const clear = (): void => {
|
||||||
services = structuredClone(DEFAULT_ARCHITECTURE_DB.services);
|
services = structuredClone(DEFAULT_ARCHITECTURE_DB.services);
|
||||||
groups = structuredClone(DEFAULT_ARCHITECTURE_DB.groups);
|
groups = structuredClone(DEFAULT_ARCHITECTURE_DB.groups);
|
||||||
lines = structuredClone(DEFAULT_ARCHITECTURE_DB.lines);
|
lines = structuredClone(DEFAULT_ARCHITECTURE_DB.lines);
|
||||||
|
registeredIds = structuredClone(DEFAULT_ARCHITECTURE_DB.registeredIds)
|
||||||
elements = {};
|
elements = {};
|
||||||
cnt = 0;
|
|
||||||
commonClear();
|
commonClear();
|
||||||
};
|
};
|
||||||
|
|
||||||
const addService = function (id: string, opts: Omit<ArchitectureService, 'id'> = {}) {
|
const addService = function (id: string, opts: Omit<ArchitectureService, 'id'> = {}) {
|
||||||
const { icon, in: inside, title } = opts;
|
const { icon, in: inside, title } = opts;
|
||||||
|
if (registeredIds[id] !== undefined) {
|
||||||
|
throw new Error(`The service id [${id}] is already in use by another ${registeredIds[id]}`)
|
||||||
|
}
|
||||||
|
if (inside !== undefined) {
|
||||||
|
if (id === inside) {
|
||||||
|
throw new Error(`The service [${id}] cannot be placed within itself`)
|
||||||
|
}
|
||||||
|
if (registeredIds[inside] === undefined) {
|
||||||
|
throw new Error(`The service [${id}]'s parent does not exist. Please make sure the parent is created before this service`)
|
||||||
|
}
|
||||||
|
if (registeredIds[inside] === 'service') {
|
||||||
|
throw new Error(`The service [${id}]'s parent is not a group`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
registeredIds[id] = 'service';
|
||||||
|
|
||||||
services.push({
|
services.push({
|
||||||
id,
|
id,
|
||||||
icon,
|
icon,
|
||||||
@ -59,6 +76,23 @@ const getServices = (): ArchitectureService[] => services;
|
|||||||
|
|
||||||
const addGroup = function (id: string, opts: Omit<ArchitectureGroup, 'id'> = {}) {
|
const addGroup = function (id: string, opts: Omit<ArchitectureGroup, 'id'> = {}) {
|
||||||
const { icon, in: inside, title } = opts;
|
const { icon, in: inside, title } = opts;
|
||||||
|
if (registeredIds[id] !== undefined) {
|
||||||
|
throw new Error(`The group id [${id}] is already in use by another ${registeredIds[id]}`)
|
||||||
|
}
|
||||||
|
if (inside !== undefined) {
|
||||||
|
if (id === inside) {
|
||||||
|
throw new Error(`The group [${id}] cannot be placed within itself`)
|
||||||
|
}
|
||||||
|
if (registeredIds[inside] === undefined) {
|
||||||
|
throw new Error(`The group [${id}]'s parent does not exist. Please make sure the parent is created before this group`)
|
||||||
|
}
|
||||||
|
if (registeredIds[inside] === 'service') {
|
||||||
|
throw new Error(`The group [${id}]'s parent is not a group`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
registeredIds[id] = 'group';
|
||||||
|
|
||||||
groups.push({
|
groups.push({
|
||||||
id,
|
id,
|
||||||
icon,
|
icon,
|
||||||
|
@ -51,6 +51,7 @@ export interface ArchitectureLine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ArchitectureDB extends DiagramDB {
|
export interface ArchitectureDB extends DiagramDB {
|
||||||
|
clear: () => void;
|
||||||
addService: (id: string, opts: Omit<ArchitectureService, 'id'>) => void;
|
addService: (id: string, opts: Omit<ArchitectureService, 'id'>) => void;
|
||||||
getServices: () => ArchitectureService[];
|
getServices: () => ArchitectureService[];
|
||||||
addGroup: (id: string, opts: Omit<ArchitectureGroup, 'id'>) => void;
|
addGroup: (id: string, opts: Omit<ArchitectureGroup, 'id'>) => void;
|
||||||
@ -71,6 +72,6 @@ export interface ArchitectureFields {
|
|||||||
services: ArchitectureService[];
|
services: ArchitectureService[];
|
||||||
groups: ArchitectureGroup[];
|
groups: ArchitectureGroup[];
|
||||||
lines: ArchitectureLine[];
|
lines: ArchitectureLine[];
|
||||||
cnt: number;
|
registeredIds: Record<string, 'service' | 'group'>;
|
||||||
config: ArchitectureDiagramConfig;
|
config: ArchitectureDiagramConfig;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user