Errors को Wrap करना
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
// 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())
}
Login to try C/C++/Java/PHP code in the editor
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
// 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))
}
Login to try C/C++/Java/PHP code in the editor
Manually Unwrapping
errors.Unwrap एक level नीचे wrapped single error निकालता है, जो वह primitive है जिसे errors.Is और errors.As आंतरिक रूप से पूरी chain को step by step चलने के लिए उपयोग करते हैं।
उदाहरण: Manually Unwrapping
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- fmt.Errorf में %w के बजाय %v उपयोग करना, जिससे errors.Is/errors.As की original wrapped error देखने की क्षमता खो जाती है।
- एक ही error को कई layers में कई बार wrap करना, एक इतना लंबा error message बनाते हुए कि यह अपठनीय हो जाए।
- यह भूल जाना कि 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: