refactor(server): single rescue boundary in dispatch#157
Merged
Conversation
Closes #129. 22 of the 26 handle/2 clauses wrapped their entire body in the identical "try do ... rescue e -> {error(Exception.message(e)), state} end" block. Beyond the sheer boilerplate (~110 lines and an extra indentation level in nearly every handler), the pattern made it easy for a future handler to forget the wrapper and crash the server loop on a bad request. The rescue now lives in one place: handle_command/2, which dispatch/2 routes through. handle_command was already the public test hook, so placing the boundary there (rather than inline in dispatch/2, as the issue sketched) keeps the tests' entry point semantics identical — several tests assert that raising commands return protocol errors via handle_command directly, and dispatch/2 takes a raw JSON line, which tests never construct. State-threading safety, verified across all six state-mutating handlers (gen, build_start, build_select, build_resolve_sub, build_finish, save): each builds its new state and returns it only on success, so a mid-handler raise reaches the boundary before any mutation escapes, and the caller's original state is returned unchanged. This is the same behavior the per-handler blocks provided. The four clauses that had no try/rescue (systems.list, characters.list variants' siblings, delete, unknown-command fallbacks) are unchanged; they either cannot raise or already return tagged errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #129.
What
22 of the 26
handle/2clauses inserver.exwrapped their body in the identicaltry do ... rescue e -> {error(Exception.message(e)), state} endblock. All per-handler blocks are deleted; the rescue now lives in exactly one place on the dispatch path.How
handle_command/2(already the public test hook) is now the single rescue boundary, using a function-levelrescue.dispatch/2routes through it.handle_command/2rather than inline indispatch/2, because several tests assert error responses by callinghandle_commanddirectly with maps —dispatch/2takes a raw JSON line the tests never construct. Same single-boundary result, and tests pass unchanged.gen,build_start,build_select,build_resolve_sub,build_finish,save): each builds its new state and returns it only on success, so a mid-handler raise reaches the boundary before any mutation escapes and the original state is returned — identical to the old per-handler behavior.Net −80 lines; the large diff line count is the unindenting of every handler body by one level.
All 389 tests pass unchanged; credo and dialyzer clean.
Summary by Bito