Updated module path and significantly extended the StoneError implementation with new functionality, including metadata handling, error wrapping, JSON serialization, and utilities like `IsStoneError`. Enhanced README with detailed examples, usage guidelines, and a thematic introduction.
122 lines
3.2 KiB
Markdown
122 lines
3.2 KiB
Markdown
# π₯ StoneError - 10 Billion Times Error Handling!
|
|
|
|
The most scientifically precise error library from the Kingdom of Science!
|
|
|
|
[]()
|
|
[]()
|
|
[]()
|
|
|
|
## π Why StoneError?
|
|
|
|
- Dr.Stone-themed error handling that would make Senku proud
|
|
- Dramatic error traces worthy of a laboratory explosion
|
|
- Scientific metadata attached to every error case
|
|
- 10 billion percent more organized than standard errors
|
|
|
|
## π₯ Installation
|
|
|
|
```bash
|
|
go get gitstormr.dev/stone-utils/stoneerror@latest
|
|
```
|
|
|
|
## β‘ Basic Usage
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"gitstormr.dev/stone-utils/stoneerror"
|
|
)
|
|
|
|
func main() {
|
|
// Create new scientific error
|
|
err := stoneerror.New(4001, "Invalid chemical formula").
|
|
WithMetadata("element", "H2O2").
|
|
WithMetadata("expected", "H2O")
|
|
|
|
// Wrap existing errors with scientific precision
|
|
dbErr := errors.New("connection timeout")
|
|
wrappedErr := stoneerror.Wrap(dbErr, 5001, "Database experiment failed").
|
|
WithMetadata("query", "SELECT * FROM chemical_elements")
|
|
}
|
|
```
|
|
|
|
## π¬ Error Output Format
|
|
|
|
```text
|
|
StoneError [4001]
|
|
ββ Message: Invalid chemical formula
|
|
ββ Time: 2023-07-15T14:30:45Z
|
|
ββ Metadata:
|
|
β ββ element: H2O2
|
|
β ββ expected: H2O
|
|
ββ Caused by:
|
|
StoneError [5001]
|
|
ββ Message: Database experiment failed
|
|
ββ Time: 2023-07-15T14:30:45Z
|
|
ββ Metadata:
|
|
β ββ query: SELECT * FROM chemical_elements
|
|
ββ Caused by:
|
|
connection timeout
|
|
```
|
|
|
|
## π§ͺ Core Features
|
|
|
|
### Scientific Error Codes
|
|
|
|
```go
|
|
const (
|
|
LAB_FAILURE = 5000 // Critical experiment failure
|
|
BAD_CHEMISTRY = 4001 // Invalid formula or mixture
|
|
EQUIPMENT_FAIL = 5002 // Tools malfunction
|
|
)
|
|
```
|
|
|
|
### Advanced Error Wrapping
|
|
|
|
```go
|
|
// Wrap any error with scientific context
|
|
result := performExperiment()
|
|
if err != nil {
|
|
return stoneerror.Wrap(err, LAB_FAILURE, "Voltaic pile test failed")
|
|
.WithMetadata("voltage", "3.7V")
|
|
.WithMetadata("materials", []string{"zinc", "copper"})
|
|
}
|
|
```
|
|
|
|
### JSON Output
|
|
|
|
```go
|
|
jsonError := err.ToJSON()
|
|
// {
|
|
// "code": 4001,
|
|
// "message": "Invalid chemical formula",
|
|
// "time": "2023-07-15T14:30:45Z",
|
|
// "metadata": {
|
|
// "element": "H2O2",
|
|
// "expected": "H2O"
|
|
// }
|
|
// }
|
|
```
|
|
|
|
```go
|
|
stonelog.InitStoneLab(stonelog.STONE_DEBUG, true, false) // true = JSON, false = Keep suffixes
|
|
// Output: {"time":"2023-07-15T12:00:00Z","level":"OBSERVATION","message":"β
Experiment successful: System ready","caller":"main.go:15"}
|
|
```
|
|
|
|
## βοΈ Scientific Best Practices
|
|
|
|
1. Always use specific error codes - Each experiment needs proper labeling!
|
|
2. Attach relevant metadata - A good scientist documents everything
|
|
3. Wrap underlying errors - Trace the full chain of causality
|
|
4. Combine related failures - Multiple data points lead to better conclusions
|
|
|
|
**Join the Scientific Revolution!**
|
|
|
|
> "With StoneError, we're 10 billion percent prepared for any failure!" - Senku Ishigami
|
|
|
|
Kingdom of Science Approved
|
|
|
|
(Now with 100% more Chrome screaming "SO BADASS!") |