package iroh

import (
	
	
	

	quic 
)

// ApplicationError is an application-defined connection close error.
type ApplicationError struct {
	// Code is the application-defined close code.
	Code uint64
	// Reason is the application-defined close reason.
	Reason string
	// Remote reports whether the peer sent the close.
	Remote bool
}

// Error returns a human-readable close error.
func ( *ApplicationError) () string {
	 := "local"
	if .Remote {
		 = "remote"
	}
	if .Reason == "" {
		return fmt.Sprintf("application close %d (%s)", .Code, )
	}
	return fmt.Sprintf("application close %d (%s): %s", .Code, , .Reason)
}

// Unwrap returns [net.ErrClosed].
func ( *ApplicationError) () error { return net.ErrClosed }

// AsApplicationError returns the application close error in err, if any.
func ( error) (*ApplicationError, bool) {
	var  *quic.ApplicationError
	if !errors.As(, &) {
		return nil, false
	}
	return &ApplicationError{
		Code:   uint64(.ErrorCode),
		Reason: .ErrorMessage,
		Remote: .Remote,
	}, true
}