Исходники и презентации
This commit is contained in:
25
lessons/errors/errors_as/main.go
Normal file
25
lessons/errors/errors_as/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type DatabaseError struct{}
|
||||
|
||||
func (d DatabaseError) Error() string {
|
||||
return "database error"
|
||||
}
|
||||
|
||||
func GetDataFromDB() error {
|
||||
return fmt.Errorf("failed to get data: %w", DatabaseError{})
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := GetDataFromDB()
|
||||
if errors.As(err, &DatabaseError{}) {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Println("unknown error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user