File: basic.go

package info (click to toggle)
golang-github-olekukonko-errors 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 448 kB
  • sloc: makefile: 2
file content (34 lines) | stat: -rw-r--r-- 1,460 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Package main demonstrates basic usage of the errors package from github.com/olekukonko/errors.
// It showcases creating simple errors, formatted errors, errors with stack traces, and named errors,
// highlighting the package’s enhanced error handling capabilities.
package main

import (
	"fmt"
	"github.com/olekukonko/errors"
)

// main is the entry point of the program, illustrating various ways to create and use errors
// from the errors package, printing their outputs to demonstrate their behavior.
func main() {
	// Simple error (no stack trace, fast)
	// Creates a lightweight error without capturing a stack trace for optimal performance.
	err := errors.New("connection failed")
	fmt.Println(err) // Outputs: "connection failed"

	// Formatted error
	// Creates an error with a formatted message using a printf-style syntax, similar to fmt.Errorf.
	err = errors.Newf("user %s not found", "bob")
	fmt.Println(err) // Outputs: "user bob not found"

	// Error with stack trace
	// Creates an error and captures a stack trace at the point of creation for debugging purposes.
	err = errors.Trace("critical issue")
	fmt.Println(err)         // Outputs: "critical issue"
	fmt.Println(err.Stack()) // Outputs stack trace, e.g., ["main.go:15", "caller.go:42"]

	// Named error
	// Creates an error with a specific name and stack trace, useful for categorizing errors.
	err = errors.Named("InputError")
	fmt.Println(err.Name()) // Outputs: "InputError"
}