README
¶
Basic Error Definitions Example
This example demonstrates how to define basic error codes using YAML format.
errors.yaml
- code: 1001
key: UserNotFound
message: User not found
http: 404
grpc: 5
desc: The specified user could not be found in the database
- code: 1002
key: InvalidEmail
message: Invalid email address
http: 400
grpc: 3
desc: The provided email address is not valid
- code: 1003
key: DatabaseError
message: Database connection failed
http: 500
grpc: 13
desc: Failed to connect to the database server
Generate Code
go run github.com/restayway/rescode/cmd/rescodegen --input errors.yaml --output errors_gen.go --package main
main.go
package main
import (
"fmt"
"log"
)
//go:generate go run github.com/restayway/rescode/cmd/rescodegen --input errors.yaml --output errors_gen.go --package main
func main() {
// Create errors without wrapping
userErr := UserNotFound()
fmt.Printf("User error: %v\n", userErr)
fmt.Printf("HTTP code: %d\n", userErr.HttpCode)
fmt.Printf("gRPC code: %d\n", userErr.RpcCode)
// Create error with wrapped error
dbErr := fmt.Errorf("connection timeout")
wrappedErr := DatabaseError(dbErr)
fmt.Printf("Database error: %v\n", wrappedErr)
fmt.Printf("Original error: %v\n", wrappedErr.OriginalError())
// Add additional data
emailErr := InvalidEmail().SetData(map[string]string{
"email": "invalid@",
"field": "email",
"location": "signup form",
})
fmt.Printf("Email error JSON: %v\n", emailErr.JSON())
// Use constants directly
if userErr.Code == UserNotFoundCode {
log.Printf("Handling user not found with code %d", UserNotFoundCode)
}
}
Generated Code (errors_gen.go)
The command above will generate a file like this:
// Code generated by rescodegen. DO NOT EDIT.
package main
import (
"github.com/restayway/rescode"
"google.golang.org/grpc/codes"
)
// Error code constants
const (
UserNotFoundCode uint64 = 1001
UserNotFoundHTTP int = 404
UserNotFoundGRPC codes.Code = 5
UserNotFoundMsg string = "User not found"
UserNotFoundDesc string = "The specified user could not be found in the database"
InvalidEmailCode uint64 = 1002
InvalidEmailHTTP int = 400
InvalidEmailGRPC codes.Code = 3
InvalidEmailMsg string = "Invalid email address"
InvalidEmailDesc string = "The provided email address is not valid"
DatabaseErrorCode uint64 = 1003
DatabaseErrorHTTP int = 500
DatabaseErrorGRPC codes.Code = 13
DatabaseErrorMsg string = "Database connection failed"
DatabaseErrorDesc string = "Failed to connect to the database server"
)
// UserNotFound creates a new UserNotFound error.
// The specified user could not be found in the database
func UserNotFound(err ...error) *rescode.RC {
return rescode.New(UserNotFoundCode, UserNotFoundHTTP, UserNotFoundGRPC, UserNotFoundMsg)(err...)
}
// InvalidEmail creates a new InvalidEmail error.
// The provided email address is not valid
func InvalidEmail(err ...error) *rescode.RC {
return rescode.New(InvalidEmailCode, InvalidEmailHTTP, InvalidEmailGRPC, InvalidEmailMsg)(err...)
}
// DatabaseError creates a new DatabaseError error.
// Failed to connect to the database server
func DatabaseError(err ...error) *rescode.RC {
return rescode.New(DatabaseErrorCode, DatabaseErrorHTTP, DatabaseErrorGRPC, DatabaseErrorMsg)(err...)
}
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.