package http3

import (
	
	

	
)

// Error is returned from the round tripper (for HTTP clients)
// and inside the HTTP handler (for HTTP servers) if an HTTP/3 error occurs.
// See section 8 of RFC 9114.
type Error struct {
	Remote       bool
	ErrorCode    ErrCode
	ErrorMessage string
}

var _ error = &Error{}

func ( *Error) () string {
	 := .ErrorCode.string()
	if  == "" {
		 = fmt.Sprintf("H3 error (%#x)", uint64(.ErrorCode))
	}
	// Usually errors are remote. Only make it explicit for local errors.
	if !.Remote {
		 += " (local)"
	}
	if .ErrorMessage != "" {
		 += ": " + .ErrorMessage
	}
	return 
}

func ( *Error) ( error) bool {
	,  := .(*Error)
	return  && .ErrorCode == .ErrorCode && .Remote == .Remote
}

func maybeReplaceError( error) error {
	if  == nil {
		return nil
	}

	var (
		      Error
		 *quic.StreamError
		 *quic.ApplicationError
	)
	switch {
	default:
		return 
	case errors.As(, &):
		.Remote = .Remote
		.ErrorCode = ErrCode(.ErrorCode)
	case errors.As(, &):
		.Remote = .Remote
		.ErrorCode = ErrCode(.ErrorCode)
		.ErrorMessage = .ErrorMessage
	}
	return &
}