Refactor: Consolidate handler error boilerplate (ApiError + ChannelMember extractor) #83

Closed
opened 2026-07-08 02:40:31 +00:00 by icub3d · 0 comments
Owner

Category: Refactor / simplification — found in code review 2026-07-07.

Problem

~11 resource modules each redefine an identical struct ErrorBody, type ApiResult<T>, and helpers bad_request/not_found/forbidden/conflict/internal/storage_err (messages, channels, reactions, roles, invites, membership, threads, attachments, profiles, auth, plus permissions.rs and gateway/handler.rs). Each handler also repeats the same auth → permission → channel-existence prologue.

Fix

  • Introduce one crate::error::ApiError enum implementing IntoResponse with From<StorageError>, and a crate-wide pub type ApiResult<T> = Result<Json<T>, ApiError>. Handlers then use ? directly.
  • Add a ChannelMember { perms, channel_id, user_id } extractor (FromRequestParts) that resolves the channel, verifies existence, and computes effective_permissions once — mirroring the existing MemberUser/UserPermissions extractors.

Removes several hundred lines and makes error-shape + check-ordering consistent.

**Category: Refactor / simplification** — found in code review 2026-07-07. ## Problem ~11 resource modules each redefine an identical `struct ErrorBody`, `type ApiResult<T>`, and helpers `bad_request/not_found/forbidden/conflict/internal/storage_err` (messages, channels, reactions, roles, invites, membership, threads, attachments, profiles, auth, plus permissions.rs and gateway/handler.rs). Each handler also repeats the same auth → permission → channel-existence prologue. ## Fix - [ ] Introduce one `crate::error::ApiError` enum implementing `IntoResponse` with `From<StorageError>`, and a crate-wide `pub type ApiResult<T> = Result<Json<T>, ApiError>`. Handlers then use `?` directly. - [ ] Add a `ChannelMember { perms, channel_id, user_id }` extractor (`FromRequestParts`) that resolves the channel, verifies existence, and computes `effective_permissions` once — mirroring the existing `MemberUser`/`UserPermissions` extractors. Removes several hundred lines and makes error-shape + check-ordering consistent.
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
icub3d/decentcom#83
No description provided.