← Back to Go Course | Chapter 9: Error Handling | Lesson 6 of 6

Errors को Wrap करना

किसी error को wrap करना original problem को बाहर अतिरिक्त notes वाले एक नए envelope के अंदर रखने जैसा है, जबकि फिर भी किसी को इसे वापस खोलकर अंदर का original note देखने देते हुए।
Syntax
go
return fmt.Errorf("context: %w", err)

errors.Unwrap(err)

Wrapping with %w

fmt.Errorf का %w verb, %v या %s की जगह उपयोग होते हुए, दिए गए error को एक नए के अंदर wrap करता है, एक message prefix जोड़ते हुए जबकि original तक unwrap करने की क्षमता संरक्षित रखते हुए।

उदाहरण: Wrapping with %w

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"errors"
	"fmt"
)

// Define the function readConfig
func readConfig() error {
	// Send errors.New("file missing") back to the caller
	return errors.New("file missing")
}

// Define the function startApp
func startApp() error {
	// Check whether err := readConfig(); err != nil
	if err := readConfig(); err != nil {
		return fmt.Errorf("startApp: %w", err)
	}
	// Send nil back to the caller
	return nil
}

// main is where the program starts running
func main() {
	fmt.Println(startApp())
}

Checking Through a Wrapped Chain

wrapping की कई layers के बाद भी, errors.Is अब भी chain के सबसे नीचे original error detect कर सकता है, हर layer को call stack में ऊपर error identity checks तोड़े बिना context जोड़ने देते हुए।

उदाहरण: Checking Through a Wrapped Chain

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"errors"
	"fmt"
)

var ErrConfigMissing = errors.New("config missing")

// Define the function readConfig
func readConfig() error {
	// Send ErrConfigMissing back to the caller
	return ErrConfigMissing
}

// Define the function startApp
func startApp() error {
	// Check whether err := readConfig(); err != nil
	if err := readConfig(); err != nil {
		return fmt.Errorf("startApp failed: %w", err)
	}
	// Send nil back to the caller
	return nil
}

// main is where the program starts running
func main() {
	err := startApp()
	fmt.Println(err)
	fmt.Println("is config missing:", errors.Is(err, ErrConfigMissing))
}

Manually Unwrapping

errors.Unwrap एक level नीचे wrapped single error निकालता है, जो वह primitive है जिसे errors.Is और errors.As आंतरिक रूप से पूरी chain को step by step चलने के लिए उपयोग करते हैं।

Note: application code में errors.Is/errors.As को प्राथमिकता दें -- Unwrap ज़्यादातर अपने खुद के error-inspection tools लिखने के लिए उपयोगी है।

उदाहरण: Manually Unwrapping

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"errors"
	"fmt"
)

// main is where the program starts running
func main() {
	// Short declaration: create base and infer the type
	base := errors.New("disk full")
	// Short declaration: create wrapped and infer the type
	wrapped := fmt.Errorf("save failed: %w", base)

	inner := errors.Unwrap(wrapped)
	// Print the values followed by a newline
	fmt.Println("outer:", wrapped)
	// Print the values followed by a newline
	fmt.Println("inner:", inner)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. fmt.Errorf में %w के बजाय %v उपयोग करना, जिससे errors.Is/errors.As की original wrapped error देखने की क्षमता खो जाती है।
  2. एक ही error को कई layers में कई बार wrap करना, एक इतना लंबा error message बनाते हुए कि यह अपठनीय हो जाए।
  3. यह भूल जाना कि wrapping यह नहीं बदलता कि errors.Is/errors.As क्या देखते हैं -- वे अब भी originally wrapped sentinel या type ढूंढते हैं।
चैप्टर सारांश
  • fmt.Errorf का %w verb एक मौजूदा error को wrap करता है जबकि अतिरिक्त context जोड़ता है।
  • एक wrapped error अब भी original error के खिलाफ errors.Is/errors.As checks satisfy करता है।
  • Wrapping context की पूरी chain को संरक्षित रखता है जैसे एक error कई function calls के ज़रिए ऊपर travel करता है।
  • Errors.Unwrap manually किसी wrap chain में अगली error निकाल सकता है, हालांकि Is/As को आमतौर पर प्राथमिकता दी जाती है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.