feat: type key-values only#2
Merged
Merged
Conversation
Allowing for arbitrary lists of key-values was causing unintentional panicking when the developer mistakes the type of a value. E.g. using an error code with type string instead of errors.Code. By Allowing only typed values to be used we eliminate one potential panic.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR restricts the errors.With API to accept only typed key-value arguments, preventing panics from incorrect varargs use.
- Changed
Withsignature to only acceptKeyValuertypes and removed varargs parsing helpers. - Updated tests to use the new
KeyValuer-only interface. - Revised README and example code to reflect the new usage model.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| with.go | Updated With signature, removed argsToKeyValues and cutKV. |
| with_test.go | Replaced generic varargs tests with KeyValuer-only tests. |
| op_test.go | Adjusted test to pass Op directly instead of splitting key/value. |
| README.md | Expanded usage section and examples to show KeyValuer calls. |
Comments suppressed due to low confidence (4)
with_test.go:18
- [nitpick] Consider renaming this to
TestWithNilErrorReturnsNilorTestWith_NilErr_ReturnsNilfor clearer intent about testing the nil-err case.
func TestWithNoError(t *testing.T) {
with_test.go:25
- A test for the case where a non-nil error is passed with zero
KeyValuerarguments (i.e.errors.With(rootErr)) would ensure the function still returns the original error.
func TestWith(t *testing.T) {
with.go:14
- [nitpick] Consider making this panic message more descriptive, e.g. include the key's type or change it to
panic("key type is not comparable")to aid debugging.
panic("key is not comparable")
README.md:10
- The example uses
fmt.Errorfbut thefmtpackage isn't imported in the snippet. Add"fmt"to the import block for the example to compile as shown.
import (
xico42
approved these changes
May 23, 2025
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.
Allowing for arbitrary lists of key-values was causing unintentional panicking when the developer mistakes the type of a value. E.g. using an error code with type string instead of errors.Code.
By Allowing only typed values to be used we eliminate one potential panic.