[GH-ISSUE #747] [Feature]: add explicit null-guard to storage.create/update in both folder route modules #355

Closed
opened 2026-10-20 17:27:57 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @coderabbitai[bot] on GitHub (May 12, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/747

Originally assigned to: @Romuromylus on GitHub.

What problem does this solve?

The folder route modules (packages/server/src/routes/connection-folders.routes.ts and the parallel packages/server/src/routes/chat-folders.routes.ts) use a non-null assertion (folder!.collapsed) after storage.create / storage.update in their POST and PATCH handlers.

This is currently safe — both storage methods resolve via getById(id) immediately after the insert/update, so a null return would only happen if the underlying SQL silently failed (which would have thrown first). The PATCH path additionally has a getById precheck that guarantees the row exists.

That said, the non-null assertion is a fragile coupling: if the storage layer ever evolves to handle creation failures differently (e.g. returning null on a constraint violation rather than throwing), every handler using folder! would explode at runtime with a cryptic Cannot read property 'collapsed' of null.

Proposed solution

Replace the non-null assertion with an explicit guard in both folder route modules, in lockstep:

// POST / (create)
const folder = await storage.create({ name: name.trim(), color });
if (!folder) return reply.status(500).send({ error: "Failed to create folder" });
return reply.send({ ...folder, collapsed: folder.collapsed === "true" });
// PATCH /:id (update)
const existing = await storage.getById(req.params.id);
if (!existing) return reply.status(404).send({ error: "Folder not found" });
const folder = await storage.update(req.params.id, req.body);
if (!folder) return reply.status(500).send({ error: "Failed to update folder" });
return reply.send({ ...folder, collapsed: folder.collapsed === "true" });

Both connection-folders.routes.ts and chat-folders.routes.ts should be updated in the same PR — diverging patterns between two analogous modules invites confusion for future contributors.

Alternatives considered

  • Leave the non-null assertion — works under current semantics but rots the moment storage error-handling evolves.
  • Wrap the entire handler in a try/catch and 500 on any throw — broader change, would mask legitimate validation errors (400s) from being distinguished from internal failures.
  • Change storage.create/update to throw on missing post-write fetch — semantically equivalent to the current behaviour; doesn't remove the type-level T | null problem at the route boundary.

The explicit if (!folder) guard is the smallest change that satisfies the type system without altering observable behaviour for any current code path.

Additional context

Raised during CodeRabbit review of PR #745 (specifically this thread). Filed as a lockstep follow-up so the connection-folders implementation in #745 stays consistent with the existing chat-folders pattern rather than diverging mid-feature.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @coderabbitai[bot] on GitHub (May 12, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/747 Originally assigned to: @Romuromylus on GitHub. ## What problem does this solve? The folder route modules (`packages/server/src/routes/connection-folders.routes.ts` and the parallel `packages/server/src/routes/chat-folders.routes.ts`) use a non-null assertion (`folder!.collapsed`) after `storage.create` / `storage.update` in their POST and PATCH handlers. This is currently safe — both storage methods resolve via `getById(id)` immediately after the insert/update, so a `null` return would only happen if the underlying SQL silently failed (which would have thrown first). The PATCH path additionally has a `getById` precheck that guarantees the row exists. That said, the non-null assertion is a fragile coupling: if the storage layer ever evolves to handle creation failures differently (e.g. returning `null` on a constraint violation rather than throwing), every handler using `folder!` would explode at runtime with a cryptic `Cannot read property 'collapsed' of null`. ## Proposed solution Replace the non-null assertion with an explicit guard in both folder route modules, in lockstep: ```ts // POST / (create) const folder = await storage.create({ name: name.trim(), color }); if (!folder) return reply.status(500).send({ error: "Failed to create folder" }); return reply.send({ ...folder, collapsed: folder.collapsed === "true" }); ``` ```ts // PATCH /:id (update) const existing = await storage.getById(req.params.id); if (!existing) return reply.status(404).send({ error: "Folder not found" }); const folder = await storage.update(req.params.id, req.body); if (!folder) return reply.status(500).send({ error: "Failed to update folder" }); return reply.send({ ...folder, collapsed: folder.collapsed === "true" }); ``` Both `connection-folders.routes.ts` and `chat-folders.routes.ts` should be updated in the same PR — diverging patterns between two analogous modules invites confusion for future contributors. ## Alternatives considered - **Leave the non-null assertion** — works under current semantics but rots the moment storage error-handling evolves. - **Wrap the entire handler in a try/catch and 500 on any throw** — broader change, would mask legitimate validation errors (400s) from being distinguished from internal failures. - **Change `storage.create/update` to throw on missing post-write fetch** — semantically equivalent to the current behaviour; doesn't remove the type-level `T | null` problem at the route boundary. The explicit `if (!folder)` guard is the smallest change that satisfies the type system without altering observable behaviour for any current code path. ## Additional context Raised during CodeRabbit review of PR #745 (specifically [this thread](https://github.com/Pasta-Devs/Marinara-Engine/pull/745#discussion_r3226725893)). Filed as a lockstep follow-up so the connection-folders implementation in #745 stays consistent with the existing chat-folders pattern rather than diverging mid-feature. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:27:57 +07:00
Author
Owner

@cha1latte commented on GitHub (May 12, 2026):

Triage note: labeling this as normal-scope because it is a narrow route hardening change for existing folder modules.

The least risky next step is to add explicit null guards in both parallel route modules without changing current successful create/update behavior.

<!-- gh-comment-id:4432576925 --> @cha1latte commented on GitHub (May 12, 2026): Triage note: labeling this as `normal-scope` because it is a narrow route hardening change for existing folder modules. The least risky next step is to add explicit null guards in both parallel route modules without changing current successful create/update behavior.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#355
No description provided.