Advanced Kratos error handling package with type-safe operations and nil interface trap prevention.
π― Type-Safe Error Handling: Simplified API to manipulate Kratos errors without naming conflicts β‘ Safe Error Handling: Solves Go's notorious (*T)(nil) != nil trap through intelligent adaptation π Testing Integration: Complete testify/assert and testify/require helpers to test Kratos errors
go get github.com/orzkratos/errkratosimport "github.com/orzkratos/errkratos"
// Type-safe error conversion
err := someFunction()
if erk, ok := errkratos.As(err); ok {
fmt.Printf("Kratos error: %s (code: %d)\n", erk.Reason, erk.Code)
}
// Error comparison
erk1 := errors.BadRequest("INVALID_INPUT", "missing field")
erk2 := errors.BadRequest("INVALID_INPUT", "wrong format")
if errkratos.Is(erk1, erk2) {
// Same error type (reason and code match)
}
// Convert generic error to Kratos error
erk := errkratos.From(err)import "github.com/orzkratos/errkratos/newerk"
// Configure reason code field name to store enum numeric value
newerk.SetReasonCodeFieldName("numeric_reason_code_enum")
// Create type-safe error with enum
erk := newerk.NewError(404, ErrorReason_USER_NOT_FOUND, "user %d not found", userID)
// Check error type
if newerk.IsError(err, ErrorReason_USER_NOT_FOUND, 404) {
// Handle user not found error
}import "github.com/orzkratos/errkratos/must/erkassert"
func TestSomething(t *testing.T) {
var erk *errors.Error
// Assert no error (handles nil interface with safe checks)
erkassert.NoError(t, erk)
// Assert error exists
erk = errors.InternalServer("SERVER_ERROR", "database failed")
erkassert.Error(t, erk)
// Assert error equivalence
expected := errors.BadRequest("INVALID_INPUT", "test")
erkassert.Is(t, expected, erk)
}import "github.com/orzkratos/errkratos/must/erkrequire"
func TestCritical(t *testing.T) {
var erk *errors.Error
// Require no error (stops test at once if error exists)
erkrequire.NoError(t, erk)
// Continue when no error...
}import "github.com/orzkratos/errkratos/must/erkmust"
func criticalOperation() {
erk := doSomethingImportant()
// Panic if error exists (with structured logging)
erkmust.Done(erk)
// Use Must (same function, different name)
erkmust.Must(erk)
}errkratos/
βββ errors.go # Core API (As, Is, From)
βββ newerk/ # Concise error creation API
βββ erkadapt/ # Nil interface adaptation
βββ must/ # Testing and enforcement tools
β βββ erkassert/ # testify/assert helpers
β βββ erkrequire/ # testify/require helpers
β βββ erkmust/ # Production panic utilities
βββ internal/
βββ errorspb/ # Example error definitions
Go has a known issue where a typed nil value doesn't match nil when converted to interface:
var erk *errors.Error = nil
var err error = erk
fmt.Println(erk == nil) // true
fmt.Println(err == nil) // false (!!)This causes issues in error handling. errkratos solves this through intelligent adaptation in each function.
The Erk type alias avoids import conflicts between standard errors package and Kratos errors:
// Instead of this confusion:
import (
stderrors "errors"
"github.com/go-kratos/kratos/v2/errors"
)
// Just use:
import "github.com/orzkratos/errkratos"
// And work with errkratos.Erk- ebzkratos - Error type that doesn't implement error interface
MIT License. See LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a mistake? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
-
Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git). -
Navigate: Navigate to the cloned project (
cd repo-name) -
Branch: Create a feature branch (
git checkout -b feature/xxx). - Code: Implement the changes with comprehensive tests
-
Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes and use significant commit messages
-
Stage: Stage changes (
git add .) -
Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code -
Push: Push to the branch (
git push origin feature/xxx). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! πππ