refactor: added some type guards#1833
Merged
Merged
Conversation
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves runtime resilience by adding safer type guards around JSON parsing in a few hotspots (gcode parsing, string formatting, and drag/drop data transfer), and simplifies the socket store state by removing the obsolete socket.error field.
Changes:
- Added defensive JSON parsing + runtime shape validation for polygon data and string-array parsing.
- Hardened drag/drop
DataTransferparsing by validating the payload and returningnullon failure; updated consumers to handlenull. - Removed the
errorproperty fromSocketStateand tightened websocket error response typing.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/workers/parseGcode.ts | Adds guarded JSON parsing for EXCLUDE_OBJECT_DEFINE polygon data to avoid worker crashes on malformed input. |
| src/util/string-formatters.ts | Ensures getStringArray only returns parsed JSON when it’s truly a string[], otherwise falls back to splitting. |
| src/util/file-data-transfer.ts | Adds a type guard + try/catch around DataTransfer JSON parsing and returns null on invalid payloads. |
| src/store/socket/types.ts | Removes the obsolete SocketState.error field. |
| src/store/socket/state.ts | Drops error from the socket default state. |
| src/store/socket/actions.ts | Adds a helper to safely extract error messages from JSON-ish strings; types onSocketError payload. |
| src/plugins/socketClient.ts | Tightens websocket API error typing to SocketError. |
| src/components/widgets/job-queue/JobQueue.vue | Handles null from drag/drop file payload parsing before using it. |
| src/components/widgets/gcode-preview/GcodePreviewCard.vue | Handles null from drag/drop file payload parsing before using it. |
| src/components/widgets/filesystem/FileSystem.vue | Handles null from drag/drop file payload parsing and adjusts drop flow accordingly. |
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.
Improved code resilience by adding some type-guards through the app.
Also removed the obsolete
socketstateerrorproperty.