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

Sentinel Errors क्या हैं

एक sentinel error एक पहले से बनी, खासतौर पर नामित error value है जो एक ज्ञात signpost की तरह काम करती है -- callers error के message text पढ़ने के बजाय नाम से उस exact signpost को जांच सकते हैं।
Syntax
go
var ErrName = errors.New("message")

if err == ErrName {
    // handle
}

Defining a Sentinel Error

एक sentinel error बस एक package-level variable है जो errors.New से एक बार बनाया जाता है, message text के बजाय identity से compare होने के लिए बनाया गया -- यह एक specific failure condition के लिए एक जाना-पहचाना signal की तरह काम करता है।

Note: Sentinel errors को convention के अनुसार Err prefix से नाम दें, जैसे ErrNotFound।

उदाहरण: Defining a Sentinel Error

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

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

var ErrNotFound = errors.New("resource not found")

// Define the function find
func find(id int) error {
	// Check whether id != 1
	if id != 1 {
		// Send ErrNotFound back to the caller
		return ErrNotFound
	}
	// Send nil back to the caller
	return nil
}

// main is where the program starts running
func main() {
	// Short declaration: create err and infer the type
	err := find(2)
	fmt.Println(err)
}

Checking Against a Sentinel

Calling code errors.Is से किसी sentinel error को जांचता है, returned error को known sentinel value से compare करते हुए -- यह सही से match करता है भले ही error रास्ते में wrapped हुई हो।

उदाहरण: Checking Against a Sentinel

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

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

var ErrNotFound = errors.New("resource not found")

// Define the function find
func find(id int) error {
	// Check whether id != 1
	if id != 1 {
		// Send ErrNotFound back to the caller
		return ErrNotFound
	}
	// Send nil back to the caller
	return nil
}

// main is where the program starts running
func main() {
	// Short declaration: create err and infer the type
	err := find(5)
	if errors.Is(err, ErrNotFound) {
		fmt.Println("handle missing resource specifically")
	}
}

Sentinel Errors in the Standard Library

Go की standard library इसी exact pattern पर निर्भर है -- io.EOF किसी stream के अंत का संकेत देता है, और किसी io.Reader से पढ़ने वाला code done को असली failure से अलग करने के लिए errors.Is(err, io.EOF) से इसे जांचता है।

उदाहरण: Sentinel Errors in the Standard Library

markup
package main

import (
	"errors"
	"fmt"
	"io"
	"strings"
)

func main() {
	r := strings.NewReader("hi")
	buf := make([]byte, 10)
	_, err := r.Read(buf)
	_, err = r.Read(buf) // second read hits the end
	if errors.Is(err, io.EOF) {
		fmt.Println("reached end of stream")
	}
}
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. errors.Is के बजाय == से किसी sentinel error को संभावित-wrapped error से compare करना।
  2. हर बार एक shared package-level sentinel को reuse करने के बजाय हर बार एक नया errors.New("not found") बनाना, equality checks तोड़ते हुए।
  3. sentinel errors को conventional Err prefix से नाम न देना, उन्हें पहचानना और document करना मुश्किल बनाते हुए।
चैप्टर सारांश
  • एक sentinel error एक package-level error value है, conventionally ErrSomething नाम की, errors.New से एक बार बनाई गई।
  • Callers इसे errors.Is(err, ErrSomething) से जांचते हैं, जो wrapped errors के ज़रिए भी काम करता है।
  • Sentinel errors specific known failure conditions का संकेत देने के लिए एक छोटे, stable public API की तरह काम करते हैं।
  • Standard library इस pattern का व्यापक उपयोग करती है, जैसे io.EOF और sql.ErrNoRows।
🔒

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.