refactor(error-messages): prefix error keys with error closes #2919

This commit is contained in:
moklick
2023-03-20 11:19:32 +01:00
parent 29790476d8
commit e09dd87e77
10 changed files with 29 additions and 24 deletions
@@ -43,7 +43,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
const nodeId = useNodeId(); const nodeId = useNodeId();
if (!nodeId) { if (!nodeId) {
store.getState().onError?.('010', errorMessages['010']()); store.getState().onError?.('010', errorMessages['error010']());
return null; return null;
} }
@@ -45,7 +45,7 @@ export function useMarkerSymbol(type: MarkerType) {
const symbolExists = Object.prototype.hasOwnProperty.call(MarkerSymbols, type); const symbolExists = Object.prototype.hasOwnProperty.call(MarkerSymbols, type);
if (!symbolExists) { if (!symbolExists) {
store.getState().onError?.('009', errorMessages['009'](type)); store.getState().onError?.('009', errorMessages['error009'](type));
return null; return null;
} }
@@ -99,7 +99,7 @@ const EdgeRenderer = ({
let edgeType = edge.type || 'default'; let edgeType = edge.type || 'default';
if (!edgeTypes[edgeType]) { if (!edgeTypes[edgeType]) {
onError?.('011', errorMessages['011'](edgeType)); onError?.('011', errorMessages['error011'](edgeType));
edgeType = 'default'; edgeType = 'default';
} }
@@ -116,7 +116,7 @@ const EdgeRenderer = ({
const isFocusable = !!(edge.focusable || (edgesFocusable && typeof edge.focusable === 'undefined')); const isFocusable = !!(edge.focusable || (edgesFocusable && typeof edge.focusable === 'undefined'));
if (!sourceHandle || !targetHandle) { if (!sourceHandle || !targetHandle) {
onError?.('008', errorMessages['008'](sourceHandle, edge)); onError?.('008', errorMessages['error008'](sourceHandle, edge));
return null; return null;
} }
@@ -78,7 +78,7 @@ const NodeRenderer = (props: NodeRendererProps) => {
let nodeType = node.type || 'default'; let nodeType = node.type || 'default';
if (!props.nodeTypes[nodeType]) { if (!props.nodeTypes[nodeType]) {
onError?.('003', errorMessages['003'](nodeType)); onError?.('003', errorMessages['error003'](nodeType));
nodeType = 'default'; nodeType = 'default';
} }
@@ -17,7 +17,7 @@ export function useNodeOrEdgeTypes(nodeOrEdgeTypes: any, createTypes: any): any
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
const typeKeys = Object.keys(nodeOrEdgeTypes); const typeKeys = Object.keys(nodeOrEdgeTypes);
if (shallow(typesKeysRef.current, typeKeys)) { if (shallow(typesKeysRef.current, typeKeys)) {
devWarn('002', errorMessages['002']()); devWarn('002', errorMessages['error002']());
} }
typesKeysRef.current = typeKeys; typesKeysRef.current = typeKeys;
+11 -11
View File
@@ -1,20 +1,20 @@
import { Edge, HandleElement } from './types'; import { Edge, HandleElement } from './types';
export const errorMessages = { export const errorMessages = {
'001': () => error001: () =>
'[React Flow]: Seems like you have not used zustand provider as an ancestor. Help: https://reactflow.dev/error#001', '[React Flow]: Seems like you have not used zustand provider as an ancestor. Help: https://reactflow.dev/error#001',
'002': () => error002: () =>
"It looks like you've created a new nodeTypes or edgeTypes object. If this wasn't on purpose please define the nodeTypes/edgeTypes outside of the component or memoize them.", "It looks like you've created a new nodeTypes or edgeTypes object. If this wasn't on purpose please define the nodeTypes/edgeTypes outside of the component or memoize them.",
'003': (nodeType: string) => `Node type "${nodeType}" not found. Using fallback type "default".`, error003: (nodeType: string) => `Node type "${nodeType}" not found. Using fallback type "default".`,
'004': () => 'The React Flow parent container needs a width and a height to render the graph.', error004: () => 'The React Flow parent container needs a width and a height to render the graph.',
'005': () => 'Only child nodes can use a parent extent.', error005: () => 'Only child nodes can use a parent extent.',
'006': () => "Can't create edge. An edge needs a source and a target.", error006: () => "Can't create edge. An edge needs a source and a target.",
'007': (id: string) => `The old edge with id=${id} does not exist.`, error007: (id: string) => `The old edge with id=${id} does not exist.`,
'009': (type: string) => `Marker type "${type}" doesn't exist.`, error009: (type: string) => `Marker type "${type}" doesn't exist.`,
'008': (sourceHandle: HandleElement | null, edge: Edge) => error008: (sourceHandle: HandleElement | null, edge: Edge) =>
`Couldn't create edge for ${!sourceHandle ? 'source' : 'target'} handle id: "${ `Couldn't create edge for ${!sourceHandle ? 'source' : 'target'} handle id: "${
!sourceHandle ? edge.sourceHandle : edge.targetHandle !sourceHandle ? edge.sourceHandle : edge.targetHandle
}", edge id: ${edge.id}.`, }", edge id: ${edge.id}.`,
'010': () => 'Handle: No node id found. Make sure to only use a Handle inside a custom Node.', error010: () => 'Handle: No node id found. Make sure to only use a Handle inside a custom Node.',
'011': (edgeType: string) => `Edge type "${edgeType}" not found. Using fallback type "default".`, error011: (edgeType: string) => `Edge type "${edgeType}" not found. Using fallback type "default".`,
}; };
+1 -1
View File
@@ -93,7 +93,7 @@ export function calcNextPosition(
] ]
: currentExtent; : currentExtent;
} else { } else {
onError?.('005', errorMessages['005']()); onError?.('005', errorMessages['error005']());
currentExtent = nodeExtent; currentExtent = nodeExtent;
} }
+1 -1
View File
@@ -19,7 +19,7 @@ function useResizeHandler(rendererNode: MutableRefObject<HTMLDivElement | null>)
const size = getDimensions(rendererNode.current); const size = getDimensions(rendererNode.current);
if (size.height === 0 || size.width === 0) { if (size.height === 0 || size.width === 0) {
store.getState().onError?.('004', errorMessages['004']()); store.getState().onError?.('004', errorMessages['error004']());
} }
store.setState({ width: size.width || 500, height: size.height || 500 }); store.setState({ width: size.width || 500, height: size.height || 500 });
+1 -1
View File
@@ -6,7 +6,7 @@ import StoreContext from '../contexts/RFStoreContext';
import { errorMessages } from '../contants'; import { errorMessages } from '../contants';
import type { ReactFlowState } from '../types'; import type { ReactFlowState } from '../types';
const zustandErrorMessage = errorMessages['001'](); const zustandErrorMessage = errorMessages['error001']();
type ExtractState = StoreApi<ReactFlowState> extends { getState: () => infer T } ? T : never; type ExtractState = StoreApi<ReactFlowState> extends { getState: () => infer T } ? T : never;
+9 -4
View File
@@ -72,7 +72,7 @@ const connectionExists = (edge: Edge, edges: Edge[]) => {
export const addEdge = (edgeParams: Edge | Connection, edges: Edge[]): Edge[] => { export const addEdge = (edgeParams: Edge | Connection, edges: Edge[]): Edge[] => {
if (!edgeParams.source || !edgeParams.target) { if (!edgeParams.source || !edgeParams.target) {
devWarn('006', errorMessages['006']()); devWarn('006', errorMessages['error006']());
return edges; return edges;
} }
@@ -94,11 +94,16 @@ export const addEdge = (edgeParams: Edge | Connection, edges: Edge[]): Edge[] =>
return edges.concat(edge); return edges.concat(edge);
}; };
export const updateEdge = (oldEdge: Edge, newConnection: Connection, edges: Edge[], options: UpdateEdgeOptions = { shouldReplaceId: true }): Edge[] => { export const updateEdge = (
oldEdge: Edge,
newConnection: Connection,
edges: Edge[],
options: UpdateEdgeOptions = { shouldReplaceId: true }
): Edge[] => {
const { id: oldEdgeId, ...rest } = oldEdge; const { id: oldEdgeId, ...rest } = oldEdge;
if (!newConnection.source || !newConnection.target) { if (!newConnection.source || !newConnection.target) {
devWarn('006', errorMessages['006']()); devWarn('006', errorMessages['error006']());
return edges; return edges;
} }
@@ -106,7 +111,7 @@ export const updateEdge = (oldEdge: Edge, newConnection: Connection, edges: Edge
const foundEdge = edges.find((e) => e.id === oldEdgeId) as Edge; const foundEdge = edges.find((e) => e.id === oldEdgeId) as Edge;
if (!foundEdge) { if (!foundEdge) {
devWarn('007', errorMessages['007'](oldEdgeId)); devWarn('007', errorMessages['error007'](oldEdgeId));
return edges; return edges;
} }