### Install meh Go Package Source: https://github.com/lefinal/meh/blob/main/README.md This command installs the 'meh' package for Go projects. It fetches the latest version of the library and makes it available for use in your Go modules. ```shell go get github.com/lefinal/meh ``` -------------------------------- ### Go: Wrap errors with context using meh.Wrap Source: https://github.com/lefinal/meh/blob/main/README.md Demonstrates how to wrap errors in Go using the 'meh.Wrap' function. This preserves the original error code and adds contextual information like user ID and function-specific details. The example shows a chain of wrapped errors, illustrating how the original error message is preserved. ```go package main import ( "os" "github.com/google/uuid" "github.com/lefinal/meh" ) struct Fruit { // ... } func IncrementApplesForUser(userID uuid.UUID, includePineapples bool) error { apples, err := applesByUser(userID, includePineapples) if err!= nil { return meh.Wrap(err, "apples by user", meh.Details{ "user_id": userID, "incude_pineapples": includePineapples, }) } // ... return nil } func applesByUser(userID uuid.UUID, includePineapples bool) (int, error) { fruits, err := fruitsByUser(userID) if err != nil { return 0, meh.Wrap(err, "fruits by user", meh.Details{"user_id": userID}) } // ... return 0, nil } func fruitsByUser(userID uuid.UUID) ([]Fruit, error) { fruitsRaw, err := readFruitsFile() if err != nil { return nil, meh.Wrap(err, "read fruits file", nil) } // ... return nil, nil } func readFruitsFile() ([]byte, error) { const fruitsFilename = "fruits.txt" b, err := os.ReadFile(fruitsFilename) if err != nil { return nil, meh.NewInternalErrFromErr(err, "read fruits file", meh.Details{ "fruits_filename": fruitsFilename, }) } return b, nil } ``` -------------------------------- ### Go: Get error code using meh.ErrorCode Source: https://github.com/lefinal/meh/blob/main/README.md Illustrates how to retrieve the specific error code from a wrapped error using the `meh.ErrorCode` function. This function traverses the error chain to find the first error that is not of type `meh.ErrNeutral`, effectively returning the original or most relevant error code. ```go package main import ( "fmt" "github.com/lefinal/meh" ) func main() { // Assume 'wrappedErr' is an error previously wrapped with meh.Wrap var wrappedErr error // Replace with your actual wrapped error // Example: Simulate a wrapped error for demonstration wrappedErr = meh.Wrap(meh.NewErr("original_code", "original message"), "layer 1", nil) wrappedErr = meh.Wrap(wrappedErr, "layer 2", nil) code := meh.ErrorCode(wrappedErr) fmt.Printf("The error code is: %s\n", code) } ``` -------------------------------- ### Go: HTTP error handling with mehhttp Source: https://github.com/lefinal/meh/blob/main/README.md Demonstrates using 'mehhttp' for handling HTTP errors. It allows setting status code mappings with `mehhttp.SetHTTPStatusCodeMapping` and responding with appropriate codes using `mehhttp.LogAndRespondError`. This function also logs error details along with request information. ```go package main import ( "net/http" "github.com/lefinal/meh/mehhttp" "github.com/lefinal/meh/mehlog" "go.uber.org/zap" ) func main() { // Initialize zap logger (replace with your actual logger initialization) logger, _ := zap.NewProduction() // Set HTTP status code mapping (example) mehhttp.SetHTTPStatusCodeMapping(map[string]int{ "custom_not_found": http.StatusNotFound, "internal_error": http.StatusInternalServerError, }) // Assume 'err' is an error object and 'w' is http.ResponseWriter var err error // Replace with your actual error w := &http.Response{} // Replace with actual http.ResponseWriter // Log the error and respond with the correct HTTP status code mehhttp.LogAndRespondError(logger, err, w) } ``` -------------------------------- ### Create Custom Error with meh Source: https://github.com/lefinal/meh/blob/main/README.md Demonstrates manual creation of a custom error using the `meh.Error` struct. This allows specifying a code, a wrapped error, a message, and arbitrary details. ```go return &meh.Error{ Code: meh.ErrInternal, WrappedErr: err, Message: "read file", Details: meh.Details{ "file_name": "my-file.txt" } } ``` -------------------------------- ### Go: Logging errors with mehlog Source: https://github.com/lefinal/meh/blob/main/README.md Shows how to integrate 'mehlog' for logging errors with Zap. This package translates error codes into log levels automatically. Use `mehlog.SetDefaultLevelTranslator` to customize the translation and `mehlog.Log` to log the error. ```go package main import ( "github.com/lefinal/meh/mehlog" "go.uber.org/zap" ) func main() { // Initialize zap logger (replace with your actual logger initialization) logger, _ := zap.NewProduction() // Set a custom level translator (optional) mehlog.SetDefaultLevelTranslator(func(code string) zap.Level { // Example: map specific codes to levels if code == "custom_error" { return zap.ErrorLevel } return zap.InfoLevel // Default level }) // Assume 'err' is an error object obtained from elsewhere var err error // Replace with your actual error // Log the error using mehlog.Log mehlog.Log(logger, err) } ``` -------------------------------- ### Specific Error Generators with Native Codes in meh Source: https://github.com/lefinal/meh/blob/main/README.md Offers convenience functions for creating errors with predefined native codes like `ErrInternal`, `ErrBadInput`, `ErrNotFound`, etc. These functions simplify error creation by pre-setting the error code. ```go func NewInternalErr(message string, details Details) error func NewInternalErrFromErr(err error, message string, details Details) error func NewBadInputErr(message string, details Details) error func NewBadInputErrFromErr(err error, message string, details Details) error func NewNotFoundErr(message string, details Details) error func NewNotFoundErrFromErr(err error, message string, details Details) error func NewUnauthorizedErr(message string, details Details) error func NewUnauthorizedErrFromErr(err error, message string, details Details) error func NewForbiddenErr(message string, details Details) error func NewForbiddenErrFromErr(err error, message string, details Details) error ``` -------------------------------- ### General Error Generators in meh Source: https://github.com/lefinal/meh/blob/main/README.md Provides utility functions for creating new errors with specified codes, messages, and details. The `FromErr` variants allow wrapping an existing error. ```go func NewErr(code Code, message string, details Details) error func NewErrFromErr(err error, code Code, message string, details Details) error ``` -------------------------------- ### Go: PostgreSQL error generation with mehpg Source: https://github.com/lefinal/meh/blob/main/README.md Provides functions for generating 'meh' errors from PostgreSQL errors (`pgconn.PgError`). `NewQueryDBErr` and `NewScanRowsErr` are used to create specific error types like `meh.ErrBadInput` based on PostgreSQL error codes, such as data exceptions (22) or integrity constraint violations (23). ```go package main import ( "github.com/lefinal/meh/mehpg" ) func main() { // Assume 'pgErr' is a pgconn.PgError object obtained from a PostgreSQL operation var pgErr error // Replace with your actual pgconn.PgError query := "SELECT * FROM users" // Example using NewQueryDBErr dbErr := mehpg.NewQueryDBErr(pgErr, "Failed to query database", query) // Example using NewScanRowsErr scanErr := mehpg.NewScanRowsErr(pgErr, "Failed to scan rows", query) // 'dbErr' and 'scanErr' can now be used with meh error handling functions } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.