Define error types in Go that convert to proper JS errors #5468
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.
What?
This pull request introduces a new mechanism/API for surfacing structured, JS-visible error types from Go modules in k6, enabling proper JavaScript
instanceofchecks and richer error handling for users.This is probably not the right way to do it, but it has the benefit of demonstrating the target UX and feature, and open the discussion on how we might implement this
The changes include new helper types and functions for error construction, conversion, and export. It also, for demonstration purposes only refactors the
fsmodule to use these new error types that can be defined.This is by no mean meant to be production-ready, it might even need to live in sobek itself, but this feature has been discussed and requested internally many times, and I just wanted to kickoff the implementation. Let's make it better together 🤝
Example
Implementing a custom error type
Define your custom error structure(s) in your go module definition::
Export the type during the module exports phase:
Using the new custom type