package main

import (
	"errors"
	"fmt"
)

type CustomError struct {
	fieldName string
}

type CustomError2 struct {
	fieldName string
}

func (c CustomError) Error() string {
	return fmt.Sprintf("missing field: %s", c.fieldName)
}

func (c CustomError) Is(target error) bool {
	return c.Error() == target.Error()
}

func (c CustomError2) Error() string {
	return fmt.Sprintf("missing field: %s", c.fieldName)
}

func (c CustomError2) Is(target error) bool {
	return c.Error() == target.Error()
}

func main() {

	test1 := CustomError{"Test1"}
	test2 := CustomError2{"Test2"}
	test3 := fmt.Errorf("Test3 %w", test1)
	test4 := fmt.Errorf("Test4: %w", test3)

	if errors.As(test3, &test1) {
		fmt.Println("Test3 contains Test1")
		fmt.Printf("Test3 contains Test1 field: %s", test3.Error())
	} else {
		fmt.Println("Test3 doesn't contain Test1")
	}
	if errors.As(test3, &test2) {
		fmt.Println("Test3 contains Test2")
	} else {
		fmt.Println("Test3 doesn't contain Test1")
	}
	if errors.As(test4, &test1) {
		fmt.Println("Test4 contains Test1")
	} else {
		fmt.Println("Test3 doesn't contain Test1")
	}
	if errors.As(test4, &test2) {
		fmt.Println("Test4 contains Test2")
	} else {
		fmt.Println("Test3 doesn't contain Test1")
	}
	if errors.As(test4, &test3) {
		fmt.Println("Test4 contains Test3")
	} else {
		fmt.Println("Test3 doesn't contain Test1")
	}
}
