Skip to content

Commit 39ee3c2

Browse files
authored
refactor: Refactors throwable errors (#375)
1 parent a6c476d commit 39ee3c2

File tree

9 files changed

+23
-35
lines changed

9 files changed

+23
-35
lines changed

src/board/internal.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,8 @@ export function InternalBoard<D>({
172172

173173
autoScrollHandlers.stop();
174174

175-
if (!transition) {
176-
throw new Error("Invariant violation: no transition.");
177-
}
178175
if (
176+
!transition ||
179177
!transition.layoutShift ||
180178
transition.layoutShift.conflicts.length > 0 ||
181179
transition.layoutShift.moves.length === 0

src/board/transition.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,11 @@ function submitTransition<D>(state: TransitionState<D>): TransitionState<D> {
167167
}
168168

169169
if (!transition) {
170-
throw new Error("Invariant violation: no transition.");
170+
return { transition: null, removeTransition: null, announcement: null };
171171
}
172172

173173
const { operation, itemsLayout, draggableItem: item, acquiredItem } = transition;
174174
const itemBelongsToBoard = item.id === acquiredItem?.id || itemsLayout.items.some((it) => it.id === item.id);
175-
176175
return transition.layoutShift?.conflicts.length === 0
177176
? {
178177
transition: null,
@@ -190,16 +189,15 @@ function discardTransition<D>(state: TransitionState<D>): TransitionState<D> {
190189
const { transition, removeTransition } = state;
191190

192191
if (removeTransition) {
193-
throw new Error("Can't discard remove transition.");
192+
throw new Error("Invariant violation: can't discard remove transition.");
194193
}
195194

196195
if (!transition) {
197-
throw new Error("Invariant violation: no transition.");
196+
return { transition: null, removeTransition: null, announcement: null };
198197
}
199198

200199
const { operation, itemsLayout, draggableItem: item, acquiredItem } = transition;
201200
const itemBelongsToBoard = item.id === acquiredItem?.id || itemsLayout.items.some((it) => it.id === item.id);
202-
203201
return {
204202
transition: null,
205203
removeTransition: null,
@@ -214,7 +212,7 @@ function updateTransitionWithPointerEvent<D>(
214212
const { transition } = state;
215213

216214
if (!transition) {
217-
throw new Error("Invariant violation: no transition.");
215+
return { transition: null, removeTransition: null, announcement: null };
218216
}
219217

220218
const layout = transition.layoutShift?.next ?? transition.itemsLayout;
@@ -270,7 +268,7 @@ function updateTransitionWithKeyboardEvent<D>(
270268
const { transition } = state;
271269

272270
if (!transition) {
273-
throw new Error("Invariant violation: no transition.");
271+
return { transition: null, removeTransition: null, announcement: null };
274272
}
275273

276274
const updateManualItemTransition = (transition: Transition<D>, direction: Direction): TransitionState<D> => {
@@ -326,7 +324,7 @@ function acquireTransitionItem<D>(
326324
const { transition } = state;
327325

328326
if (!transition) {
329-
throw new Error("Invariant violation: no transition.");
327+
return { transition: null, removeTransition: null, announcement: null };
330328
}
331329

332330
const { columns } = transition.itemsLayout;

src/board/utils/__tests__/path.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe("appendMovePath", () => {
9292
test("throws if can't find incremental path within 100 steps", () => {
9393
const path = [new Position({ x: 0, y: 0 })];
9494
const rect = { left: 0, right: 1, top: 100, bottom: 101 };
95-
expect(() => appendMovePath(path, rect)).toThrow("Infinite loop in appendPath.");
95+
expect(() => appendMovePath(path, rect)).toThrow("Invariant violation: infinite loop in appendPath.");
9696
});
9797
});
9898

src/board/utils/path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function appendPath(prevPath: readonly Position[], nextPosition: Position): Posi
7676

7777
while (x !== nextPosition.x || y !== nextPosition.y) {
7878
if (++safetyCounter === 100) {
79-
throw new Error("Infinite loop in appendPath.");
79+
throw new Error("Invariant violation: infinite loop in appendPath.");
8080
}
8181
if (x !== nextPosition.x) {
8282
x += vx;

src/internal/dnd-controller/controller.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ class DragAndDropController extends EventEmitter<DragAndDropEvents> {
7474
*/
7575
public start(transition: Transition) {
7676
this.transition = { ...transition };
77-
this.emit("start", this.getDragAndDropData(transition.startCoordinates));
77+
this.emit("start", this.getDragAndDropData(transition, transition.startCoordinates));
7878
}
7979

8080
/**
8181
* Updates current transition with given coordinates and issues an "update" event.
8282
*/
8383
public update(coordinates: Coordinates) {
84-
this.emit("update", this.getDragAndDropData(coordinates));
84+
if (this.transition) {
85+
this.emit("update", this.getDragAndDropData(this.transition, coordinates));
86+
}
8587
}
8688

8789
/**
@@ -104,10 +106,9 @@ class DragAndDropController extends EventEmitter<DragAndDropEvents> {
104106
* Issues an "acquire" event to notify the current transition draggable is acquired by the given droppable.
105107
*/
106108
public acquire(droppableId: ItemId, renderAcquiredItem: () => ReactNode) {
107-
if (!this.transition) {
108-
throw new Error("Invariant violation: no transition present for acquire.");
109+
if (this.transition) {
110+
this.emit("acquire", { droppableId, draggableItem: this.transition.draggableItem, renderAcquiredItem });
109111
}
110-
this.emit("acquire", { droppableId, draggableItem: this.transition.draggableItem, renderAcquiredItem });
111112
}
112113

113114
/**
@@ -131,14 +132,11 @@ class DragAndDropController extends EventEmitter<DragAndDropEvents> {
131132
return [...this.droppables.entries()];
132133
}
133134

134-
private getDragAndDropData(coordinates: Coordinates): DragAndDropData {
135-
if (!this.transition) {
136-
throw new Error("Invariant violation: no transition present for interaction.");
137-
}
138-
const positionOffset = Coordinates.cursorOffset(coordinates, this.transition.startCoordinates);
139-
const collisionRect = this.getCollisionRect(this.transition, coordinates);
135+
private getDragAndDropData(transition: Transition, coordinates: Coordinates): DragAndDropData {
136+
const positionOffset = Coordinates.cursorOffset(coordinates, transition.startCoordinates);
137+
const collisionRect = this.getCollisionRect(transition, coordinates);
140138
const { collisionIds, dropTarget } = this.getCollisions(collisionRect);
141-
return { ...this.transition, positionOffset, coordinates, collisionRect, collisionIds, dropTarget };
139+
return { ...transition, positionOffset, coordinates, collisionRect, collisionIds, dropTarget };
142140
}
143141

144142
private getCollisionRect(transition: Transition, coordinates: Coordinates) {
@@ -154,7 +152,6 @@ class DragAndDropController extends EventEmitter<DragAndDropEvents> {
154152
if (collisionIds.length === 0) {
155153
return { collisionIds, dropTarget: null };
156154
}
157-
158155
const matchedDroppable = droppableEntries.find(([id]) => id === collisionIds[0]);
159156
if (!matchedDroppable) {
160157
throw new Error("Invariant violation: no droppable matches collision.");

src/internal/global-drag-state-styles/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { DragAndDropData, useDragSubscription } from "../dnd-controller/controll
44

55
import styles from "./styles.css.js";
66

7-
function assertNever(value: never) {
8-
throw new Error("Unexpected value: " + value);
9-
}
10-
117
function setup({ operation, interactionType }: DragAndDropData) {
128
const isPointerInteraction = interactionType === "pointer";
139
switch (operation) {
@@ -23,8 +19,7 @@ function setup({ operation, interactionType }: DragAndDropData) {
2319
}
2420
break;
2521
default:
26-
// there will be a type error if not all operation types are handled
27-
assertNever(operation);
22+
throw new Error("Invariant violation: unexpected operation type.");
2823
}
2924

3025
if (isPointerInteraction) {

src/internal/item-container/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export const ItemContext = createContext<ItemContextType | null>(null);
129129
export function useItemContext() {
130130
const ctx = useContext(ItemContext);
131131
if (!ctx) {
132-
throw new Error("Unable to find BoardItem context.");
132+
throw new Error("Invariant violation: no board item context.");
133133
}
134134
return ctx;
135135
}

src/internal/utils/layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function transformItems<D>(
8888
const getItem = (itemId: ItemId) => {
8989
const item = itemById.get(itemId);
9090
if (!item) {
91-
throw new Error("Invariant violation: no matching source item found.");
91+
throw new Error("Invariant violation: no layout item matches given id.");
9292
}
9393
return item;
9494
};

src/items-palette/internal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function InternalItemsPalette<D>({
9090
inTransition={false}
9191
getItemSize={(dropContext) => {
9292
if (!dropContext) {
93-
throw new Error("Invariant violation: cannot query palette item size with no drop context.");
93+
throw new Error("Invariant violation: no drop context in palette.");
9494
}
9595
const { width, height } = dropContext.scale(item);
9696
return { width, minWidth: width, maxWidth: width, height, minHeight: height, maxHeight: height };

0 commit comments

Comments
 (0)