// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-08 23:09:33.336763 +0200 CEST m=+0.192836793

package errorsx

//go:generate go run ./internal/generrno/

import (
	"errors"
	"syscall"
)

// This enumeration lists the failures defined at
// https://github.com/ooni/spec/blob/master/data-formats/df-007-errors.md
const (
	//
	// System errors
	//
	FailureOperationCanceled           = "operation_canceled"
	FailureConnectionRefused           = "connection_refused"
	FailureConnectionReset             = "connection_reset"
	FailureHostUnreachable             = "host_unreachable"
	FailureTimedOut                    = "timed_out"
	FailureAddressFamilyNotSupported   = "address_family_not_supported"
	FailureAddressInUse                = "address_in_use"
	FailureAddressNotAvailable         = "address_not_available"
	FailureAlreadyConnected            = "already_connected"
	FailureBadAddress                  = "bad_address"
	FailureBadFileDescriptor           = "bad_file_descriptor"
	FailureConnectionAborted           = "connection_aborted"
	FailureConnectionAlreadyInProgress = "connection_already_in_progress"
	FailureDestinationAddressRequired  = "destination_address_required"
	FailureInterrupted                 = "interrupted"
	FailureInvalidArgument             = "invalid_argument"
	FailureMessageSize                 = "message_size"
	FailureNetworkDown                 = "network_down"
	FailureNetworkReset                = "network_reset"
	FailureNetworkUnreachable          = "network_unreachable"
	FailureNoBufferSpace               = "no_buffer_space"
	FailureNoProtocolOption            = "no_protocol_option"
	FailureNotASocket                  = "not_a_socket"
	FailureNotConnected                = "not_connected"
	FailureOperationWouldBlock         = "operation_would_block"
	FailurePermissionDenied            = "permission_denied"
	FailureProtocolNotSupported        = "protocol_not_supported"
	FailureWrongProtocolType           = "wrong_protocol_type"

	//
	// Library errors
	//
	FailureDNSBogonError           = "dns_bogon_error"
	FailureDNSNXDOMAINError        = "dns_nxdomain_error"
	FailureEOFError                = "eof_error"
	FailureGenericTimeoutError     = "generic_timeout_error"
	FailureQUICIncompatibleVersion = "quic_incompatible_version"
	FailureSSLFailedHandshake      = "ssl_failed_handshake"
	FailureSSLInvalidHostname      = "ssl_invalid_hostname"
	FailureSSLUnknownAuthority     = "ssl_unknown_authority"
	FailureSSLInvalidCertificate   = "ssl_invalid_certificate"
	FailureJSONParseError          = "json_parse_error"
)

// failureMap lists all failures so we can match them
// when they are wrapped by quic.TransportError.
var failuresMap = map[string]string{
	"operation_canceled":             "operation_canceled",
	"connection_refused":             "connection_refused",
	"connection_reset":               "connection_reset",
	"host_unreachable":               "host_unreachable",
	"timed_out":                      "timed_out",
	"address_family_not_supported":   "address_family_not_supported",
	"address_in_use":                 "address_in_use",
	"address_not_available":          "address_not_available",
	"already_connected":              "already_connected",
	"bad_address":                    "bad_address",
	"bad_file_descriptor":            "bad_file_descriptor",
	"connection_aborted":             "connection_aborted",
	"connection_already_in_progress": "connection_already_in_progress",
	"destination_address_required":   "destination_address_required",
	"interrupted":                    "interrupted",
	"invalid_argument":               "invalid_argument",
	"message_size":                   "message_size",
	"network_down":                   "network_down",
	"network_reset":                  "network_reset",
	"network_unreachable":            "network_unreachable",
	"no_buffer_space":                "no_buffer_space",
	"no_protocol_option":             "no_protocol_option",
	"not_a_socket":                   "not_a_socket",
	"not_connected":                  "not_connected",
	"operation_would_block":          "operation_would_block",
	"permission_denied":              "permission_denied",
	"protocol_not_supported":         "protocol_not_supported",
	"wrong_protocol_type":            "wrong_protocol_type",
	"dns_bogon_error":                "dns_bogon_error",
	"dns_nxdomain_error":             "dns_nxdomain_error",
	"eof_error":                      "eof_error",
	"generic_timeout_error":          "generic_timeout_error",
	"quic_incompatible_version":      "quic_incompatible_version",
	"ssl_failed_handshake":           "ssl_failed_handshake",
	"ssl_invalid_hostname":           "ssl_invalid_hostname",
	"ssl_unknown_authority":          "ssl_unknown_authority",
	"ssl_invalid_certificate":        "ssl_invalid_certificate",
	"json_parse_error":               "json_parse_error",
}

// classifySyscallError converts a syscall error to the
// proper OONI error. Returns the OONI error string
// on success, an empty string otherwise.
func classifySyscallError(err error) string {
	var errno syscall.Errno
	if !errors.As(err, &errno) {
		return ""
	}
	switch errno {
	case ECANCELED:
		return FailureOperationCanceled
	case ECONNREFUSED:
		return FailureConnectionRefused
	case ECONNRESET:
		return FailureConnectionReset
	case EHOSTUNREACH:
		return FailureHostUnreachable
	case ETIMEDOUT:
		return FailureTimedOut
	case EAFNOSUPPORT:
		return FailureAddressFamilyNotSupported
	case EADDRINUSE:
		return FailureAddressInUse
	case EADDRNOTAVAIL:
		return FailureAddressNotAvailable
	case EISCONN:
		return FailureAlreadyConnected
	case EFAULT:
		return FailureBadAddress
	case EBADF:
		return FailureBadFileDescriptor
	case ECONNABORTED:
		return FailureConnectionAborted
	case EALREADY:
		return FailureConnectionAlreadyInProgress
	case EDESTADDRREQ:
		return FailureDestinationAddressRequired
	case EINTR:
		return FailureInterrupted
	case EINVAL:
		return FailureInvalidArgument
	case EMSGSIZE:
		return FailureMessageSize
	case ENETDOWN:
		return FailureNetworkDown
	case ENETRESET:
		return FailureNetworkReset
	case ENETUNREACH:
		return FailureNetworkUnreachable
	case ENOBUFS:
		return FailureNoBufferSpace
	case ENOPROTOOPT:
		return FailureNoProtocolOption
	case ENOTSOCK:
		return FailureNotASocket
	case ENOTCONN:
		return FailureNotConnected
	case EWOULDBLOCK:
		return FailureOperationWouldBlock
	case EACCES:
		return FailurePermissionDenied
	case EPROTONOSUPPORT:
		return FailureProtocolNotSupported
	case EPROTOTYPE:
		return FailureWrongProtocolType
	}
	return ""
}
