### Retrieve stack trace lines as strings Source: https://github.com/jonbodner/stackerr/blob/master/README.md Use stackerr.Trace to get a slice of strings representing the stack trace. Provide a text.Template for custom formatting. Returns nil if no stack trace is found. ```go s := stackerr.New("This is a stack trace error") callStack, err := stackerr.Trace(s, stackerr.StandardFormat) fmt.Println(callStack) ``` -------------------------------- ### Run Go tests with trimpath Source: https://github.com/jonbodner/stackerr/blob/master/README.md When running tests for the stackerr package, use the -trimpath flag to ensure consistent file paths in stack traces. ```bash go test -trimpath ./... ``` -------------------------------- ### Retrieve Stack Trace with stackerr.Trace Source: https://context7.com/jonbodner/stackerr/llms.txt Retrieves the stack trace as a slice of strings, formatted using a customizable Go template. Returns nil for errors without stack traces. Demonstrates standard and custom formatting. ```go package main import ( "errors" "fmt" "strings" "text/template" "github.com/jonbodner/stackerr" ) func main() { err := stackerr.New("something went wrong") // Get trace with standard format trace, traceErr := stackerr.Trace(err, stackerr.StandardFormat) if traceErr != nil { panic(traceErr) } fmt.Println("Stack trace lines:") for i, line := range trace { fmt.Printf(" %d: %s\n", i, line) } // Output: // Stack trace lines: // 0: main.main (main.go:14) // 1: runtime.main (runtime/proc.go:250) // Custom template format customFormat := template.Must(template.New("custom").Parse( "{{.File}}:{{.Line}} in {{.Function}}", )) customTrace, _ := stackerr.Trace(err, customFormat) fmt.Println("\nCustom format:") fmt.Println(strings.Join(customTrace, "\n")) // Output: // Custom format: // main.go:14 in main.main // runtime/proc.go:250 in runtime.main // Returns nil for errors without stack trace plainErr := errors.New("no stack") noTrace, _ := stackerr.Trace(plainErr, stackerr.StandardFormat) fmt.Printf("\nTrace for plain error: %v\n", noTrace) // Output: Trace for plain error: } ``` -------------------------------- ### Default Stack Trace Formatting with stackerr.StandardFormat Source: https://context7.com/jonbodner/stackerr/llms.txt The default template for formatting stack trace entries. Produces output in the format "FUNCTION_NAME (FILE_PATH:LINE_NUMBER)". Demonstrates usage with the default format and a custom JSON format. ```go package main import ( "fmt" "text/template" "github.com/jonbodner/stackerr" ) func main() { err := stackerr.New("test error") // Using the standard format trace, _ := stackerr.Trace(err, stackerr.StandardFormat) fmt.Println("Standard format output:") for _, line := range trace { fmt.Println(line) } // Output: // Standard format output: // main.main (main.go:12) // runtime.main (runtime/proc.go:250) // Available template variables: .Function, .File, .Line jsonFormat := template.Must(template.New("json").Parse( `{"func":"{{.Function}}","file":"{{.File}}","line":{{.Line}}}`, )) jsonTrace, _ := stackerr.Trace(err, jsonFormat) fmt.Println("\nJSON format output:") for _, line := range jsonTrace { fmt.Println(line) } // Output: // JSON format output: // {"func":"main.main","file":"main.go","line":12} // {"func":"runtime.main","file":"runtime/proc.go","line":250} } ``` -------------------------------- ### Go Errors.Is and Errors.As Compatibility with Stackerr Source: https://context7.com/jonbodner/stackerr/llms.txt Demonstrates how stackerr integrates with Go's standard error comparison and type assertion mechanisms. Use errors.Is to check for specific error types within a wrapped stack trace. ```go package main import ( "errors" "fmt" "github.com/jonbodner/stackerr" ) var ErrNotFound = errors.New("not found") var ErrPermission = errors.New("permission denied") func FindResource(id string) error { // Wrap sentinel error with stack trace return stackerr.Errorf("resource %s: %w", id, ErrNotFound) } func main() { err := FindResource("abc123") // errors.Is works through the stack trace wrapper if errors.Is(err, ErrNotFound) { fmt.Println("Resource was not found") } // Output: Resource was not found if !errors.Is(err, ErrPermission) { fmt.Println("Error is not a permission error") } // Output: Error is not a permission error // Unwrap to get the inner error inner := errors.Unwrap(err) fmt.Printf("Unwrapped: %v\n", inner) // Output: Unwrapped: resource abc123: not found // Compare stackerr errors err1 := stackerr.New("same message") err2 := stackerr.New("same message") fmt.Printf("Same message errors equal: %v\n", errors.Is(err1, err2)) // Output: Same message errors equal: true } ``` -------------------------------- ### stackerr.StandardFormat Constant Source: https://context7.com/jonbodner/stackerr/llms.txt The default template for formatting stack trace entries. Produces output in the format "FUNCTION_NAME (FILE_PATH:LINE_NUMBER)". ```APIDOC ## stackerr.StandardFormat ### Description The default template for formatting stack trace entries. Produces output in the format "FUNCTION_NAME (FILE_PATH:LINE_NUMBER)". ### Method `stackerr.StandardFormat` (template.Template) ### Parameters None ### Request Example ```go package main import ( "fmt" "text/template" "github.com/jonbodner/stackerr" ) func main() { err := stackerr.New("test error") // Using the standard format trace, _ := stackerr.Trace(err, stackerr.StandardFormat) fmt.Println("Standard format output:") for _, line := range trace { fmt.Println(line) } // Available template variables: .Function, .File, .Line jsonFormat := template.Must(template.New("json").Parse( `{"func":"{{.Function}}","file":"{{.File}}","line":{{.Line}}}`, )) jsonTrace, _ := stackerr.Trace(err, jsonFormat) fmt.Println("\nJSON format output:") for _, line := range jsonTrace { fmt.Println(line) } } ``` ### Response #### Success Response (200) - **StandardFormat** (*template.Template) - The pre-defined standard template. #### Response Example ```json { "StandardFormat": "{{.Function}} ({{.File}}:{{.Line}})" } ``` ``` -------------------------------- ### Create a new error with a stack trace Source: https://github.com/jonbodner/stackerr/blob/master/README.md Use stackerr.New to create a new error with a stack trace when only a string message is needed, serving as a replacement for errors.New. ```go func DoSomething(input string) (string, error) { if input == "" { return "", stackerr.New("cannot supply an empty string to DoSomething") } result, err := ThingToCall(input) return result, stackerr.Wrap(err) } ``` -------------------------------- ### Format error with stack trace using %+v Source: https://github.com/jonbodner/stackerr/blob/master/README.md Use the %+v format specifier with fmt.Printf to display the stack trace as a string. This method is suitable for errors directly containing a stack trace. ```go s := stackerr.New("This is a stack trace error") fmt.Printf("%+v\n",s) ``` -------------------------------- ### Format Verbs (%v, %+v, %s, %q) Source: https://context7.com/jonbodner/stackerr/llms.txt Stackerr errors support standard fmt format verbs with special handling for %+v to include stack traces. ```APIDOC ## Format Verbs (%v, %+v, %s, %q) ### Description Stackerr errors support standard fmt format verbs with special handling for `+v` to include stack traces. ### Method Standard `fmt` package functions (e.g., `fmt.Printf`, `fmt.Sprintf`). ### Parameters None ### Request Example ```go package main import ( "fmt" "github.com/jonbodner/stackerr" ) func main() { err := stackerr.New("operation failed") // %s - Simple string representation fmt.Printf("%%s: %s\n", err) // %v - Value representation (same as %s) fmt.Printf("%%v: %v\n", err) // %+v - Detailed value with stack trace fmt.Printf("%%+v:\n%+v\n", err) // %q - Quoted string representation fmt.Printf("%%q: %q\n", err) } ``` ### Response #### Success Response (200) - **Formatted String** (string) - The error formatted according to the specified verb. #### Response Example ```json { "formattedString": "operation failed" } ``` ``` -------------------------------- ### stackerr.Trace Function Source: https://context7.com/jonbodner/stackerr/llms.txt Retrieves the stack trace as a slice of strings, formatted using a customizable Go template. Returns nil for errors without stack traces. ```APIDOC ## stackerr.Trace ### Description Retrieves the stack trace as a slice of strings, formatted using a customizable Go template. Returns nil for errors without stack traces. ### Method `stackerr.Trace(err error, tmpl *template.Template) ([]string, error)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "errors" "fmt" "strings" "text/template" "github.com/jonbodner/stackerr" ) func main() { err := stackerr.New("something went wrong") // Get trace with standard format trace, traceErr := stackerr.Trace(err, stackerr.StandardFormat) if traceErr != nil { panic(traceErr) } fmt.Println("Stack trace lines:") for i, line := range trace { fmt.Printf(" %d: %s\n", i, line) } // Custom template format customFormat := template.Must(template.New("custom").Parse( "{{.File}}:{{.Line}} in {{.Function}}", )) customTrace, _ := stackerr.Trace(err, customFormat) fmt.Println("\nCustom format:") fmt.Println(strings.Join(customTrace, "\n")) // Returns nil for errors without stack trace plainErr := errors.New("no stack") noTrace, _ := stackerr.Trace(plainErr, stackerr.StandardFormat) fmt.Printf("\nTrace for plain error: %v\n", noTrace) } ``` ### Response #### Success Response (200) - **trace** ([]string) - A slice of strings representing the formatted stack trace. - **error** (error) - An error if formatting fails, otherwise nil. #### Response Example ```json { "trace": [ "main.main (main.go:14)", "runtime.main (runtime/proc.go:250)" ], "error": null } ``` ``` -------------------------------- ### Create Formatted Errors with Stack Trace Source: https://context7.com/jonbodner/stackerr/llms.txt Use stackerr.Errorf for formatted errors, similar to fmt.Errorf, which also includes a stack trace. It preserves existing stack traces when wrapping other errors using the %w verb. ```go package main import ( "errors" "fmt" "github.com/jonbodner/stackerr" ) func FetchUser(id int) error { return errors.New("user not found") } func ProcessUser(id int) error { err := FetchUser(id) if err != nil { // Wrap with context using %w verb return stackerr.Errorf("ProcessUser failed for id %d: %w", id, err) } return nil } func HandleRequest(userID int) error { err := ProcessUser(userID) if err != nil { // Further wrapping preserves original stack trace return stackerr.Errorf("HandleRequest error: %w", err) } return nil } func main() { err := HandleRequest(42) if err != nil { fmt.Printf("%+v\n", err) // Output: // HandleRequest error: ProcessUser failed for id 42: user not found // main.ProcessUser (main.go:16) // main.HandleRequest (main.go:23) // main.main (main.go:30) } // Can also create formatted errors without wrapping simpleErr := stackerr.Errorf("operation failed at step %d", 3) fmt.Printf("%s\n", simpleErr) // Output: operation failed at step 3 } ``` -------------------------------- ### Wrap an existing error with a stack trace Source: https://github.com/jonbodner/stackerr/blob/master/README.md Use stackerr.Wrap to add a stack trace to an existing error. It handles nil errors by returning nil and existing stack traces by returning the original error. ```go func DoSomething(input string) (string, error) { result, err := ThingToCall(input) return result, stackerr.Wrap(err) } ``` -------------------------------- ### Add contextual information to an error with a stack trace Source: https://github.com/jonbodner/stackerr/blob/master/README.md Use stackerr.Errorf to wrap an existing error with custom context and a stack trace, similar to fmt.Errorf. It preserves existing stack trace information if present. ```go func DoSomething(input string) (string, error) { result, err := ThingToCall(input) if err != nil { err = stackerr.Errorf("DoSomething failed on call to ThingToCall: %w", err) } return result, err } ``` -------------------------------- ### Format Verbs for Stackerr Errors Source: https://context7.com/jonbodner/stackerr/llms.txt Stackerr errors support standard fmt format verbs. %+v is specifically handled to include stack traces, while %s, %v, and %q provide string representations. ```go package main import ( "fmt" "github.com/jonbodner/stackerr" ) func main() { err := stackerr.New("operation failed") // %s - Simple string representation fmt.Printf("%%s: %s\n", err) // Output: %s: operation failed // %v - Value representation (same as %s) fmt.Printf("%%v: %v\n", err) // Output: %v: operation failed // %+v - Detailed value with stack trace fmt.Printf("%%+v:\n%+v\n", err) // Output: // %+v: // operation failed // main.main (main.go:11) // runtime.main (runtime/proc.go:250) // %q - Quoted string representation fmt.Printf("%%q: %q\n", err) // Output: %q: "operation failed" } ``` -------------------------------- ### Check for stack trace presence in error chain Source: https://github.com/jonbodner/stackerr/blob/master/README.md Use stackerr.HasStack to determine if an error or any error in its unwrap chain contains a stack trace. ```go if stackerr.HasStack(err) { // error has a stack trace } ``` -------------------------------- ### stackerr.HasStack Function Source: https://context7.com/jonbodner/stackerr/llms.txt Checks if an error has a stack trace anywhere in its unwrap chain. Useful for conditional handling based on whether stack information is available. ```APIDOC ## stackerr.HasStack ### Description Checks if an error has a stack trace anywhere in its unwrap chain. Useful for conditional handling based on whether stack information is available. ### Method `stackerr.HasStack(err error) bool` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "errors" "fmt" "github.com/jonbodner/stackerr" ) func main() { // Plain error has no stack plainErr := errors.New("plain error") fmt.Printf("plainErr has stack: %v\n", stackerr.HasStack(plainErr)) // Wrapped error has stack wrappedErr := stackerr.Wrap(plainErr) fmt.Printf("wrappedErr has stack: %v\n", stackerr.HasStack(wrappedErr)) // Stack trace is preserved through fmt.Errorf wrapping outerErr := fmt.Errorf("outer wrapper: %w", wrappedErr) fmt.Printf("outerErr has stack: %v\n", stackerr.HasStack(outerErr)) // Conditional logging based on stack availability logError := func(err error) { if stackerr.HasStack(err) { fmt.Printf("Error with trace: %+v\n", err) } else { fmt.Printf("Error (no trace): %s\n", err) } } logError(plainErr) logError(wrappedErr) } ``` ### Response #### Success Response (200) - **hasStack** (bool) - `true` if the error contains a stack trace, `false` otherwise. #### Response Example ```json { "hasStack": true } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.