Net.HTTP.Error

lib/Net/HTTP/Error.gob

What goes wrong in HTTP, and how a caller's own error type takes it in.

? never converts one error type into another, so the library's fallible functions do not return HTTPError. They return [FromHTTP e] Either e a, and the caller's function decides e by its own return type: an application's error, SomeError, or HTTPError itself. So ? on an HTTP result lifts it without promote, which is left for other libraries' errors.

type APIError = BadBody(HTTPError) | NotFound
instance FromHTTP APIError { fun fromHTTP(e) = BadBody(e) }
type HTTPError = BadURL(URLError) | BadHeader(HeaderError) | BadMessage(Refusal) | TooLarge | Truncated | Overrun | UnframedTrailers | Consumed | Transport(SomeError) | SendFailed(ClientError) | BadRedirect(RedirectRefused) | UnexpectedStatus(Net.HTTP.Status.Status)

Every failure the HTTP library reports.

  • BadURL, BadHeader: a value a program made, or asked for, is not one.
  • BadMessage: the peer sent a message that breaks HTTP/1's rules. Its framing can no longer be trusted, so the connection is closed after it.
  • TooLarge: a body is longer than the limit its reader asked for.
  • Truncated: a body ended before its framing said it would.
  • Overrun: a program wrote more body than the length it declared.
  • UnframedTrailers: a program gave trailers for a body whose framing has nowhere to put them. On HTTP/1 only a chunked body has trailers.
  • Consumed: a body was asked for a second time, after an earlier receive, limited or unbounded took it.
  • Transport: the connection underneath failed, or the file or stream a request body was read from did: its own error, held as a SomeError so that any stream's errors fit and cast recovers them.
  • SendFailed: a client's request got no response, and whether the server may have acted on it (ClientError).
  • BadRedirect: a client refused to follow a redirect (RedirectRefused).
  • UnexpectedStatus: a response's status is not the one its caller expected (Net.HTTP.Client.expectStatus).

Every one of these but BadURL, BadHeader, TooLarge, BadRedirect and UnexpectedStatus leaves a message half sent or half read, and the connection is never reused after one.

type ClientError = NotSent(HTTPError) | MaybeSent(HTTPError) | BadResponse(HTTPError)

Why a client's request got no response, by what the server may have seen of it. The HTTPError inside holds what failed.

  • NotSent: no byte of the request was written. The target was refused (BadURL, BadRedirect), or resolving, the policy, connecting or the TLS handshake failed (Transport). Sending it again cannot repeat an effect.
  • MaybeSent: some of the request was written, and the connection failed or closed before the response's head arrived (Transport, Truncated). The server may have acted on it.
  • BadResponse: the response broke HTTP/1's rules (BadMessage), so the connection was closed. The server did receive the request.
type RedirectRefused = NotRedirect | NoLocation | BadLocation(HTTPError) | BadTarget(URLError) | Downgrade | NotReplayable | TooManyRedirects

Why a response is not followed to another request (Net.HTTP.Client.redirect).

  • NotRedirect: its status is not 301, 302, 303, 307 or 308.
  • NoLocation: it has no Location.
  • BadLocation: its Location is repeated or does not read as a URL reference.
  • BadTarget: the URL it names is not an http or https one.
  • Downgrade: it goes from https to http, which would send the next request in the clear.
  • NotReplayable: a 307 or 308, which sends the body again, to a request whose body cannot be sent again.
  • TooManyRedirects: one more than the client's follow allows.
type Refusal = Malformed | LineTooLong | HeadTooLarge | UnknownCoding | UnsupportedVersion | UnmetExpectation

Why the HTTP/1 codec refused a message, by the status a server answers it with (refusalStatus). A client that refuses a response closes the connection and answers nothing.

  • Malformed (400): a byte the grammar does not allow, ambiguous framing, or a Host that is missing, repeated or disagrees with the target.
  • LineTooLong (414): a request line or status line over its limit.
  • HeadTooLarge (431): a head, or trailers, over the byte limit, or more fields than the limit.
  • UnknownCoding (501): a transfer coding other than chunked.
  • UnsupportedVersion (505): a version other than HTTP/1.0 and HTTP/1.1.
  • UnmetExpectation (417): an Expect other than 100-continue.
fun refusalStatus : fun(Refusal) -> Net.HTTP.Status.Status

Returns the status a server answers a refused request with.

type HeaderError = InvalidName(String) | ReservedName(String) | InvalidValue | DuplicateField(String) | UnreadableValue(String)

Why a header field was refused. A field's name is quoted as text, escaped by show, since a refused name may be the bytes that made it dangerous. A refused value is never quoted: it may be a credential.

class FromHTTP e

An error type that can hold an HTTPError.

fun fromHTTP : [FromHTTP e] fun(HTTPError) -> e
fun transport : [Error e] fun(Either e a) -> Either HTTPError a

Returns r with its error, if any, as Transport.

fun toSomeError : [Error e] fun(Either HTTPError e) -> SomeError

Returns the error a codec operation failed with, as itself: an HTTPError, or the transport's own error.

fun flattenError : fun(Either HTTPError SomeError) -> SomeError

toSomeError, for a transport whose errors are packed already: the client's connections, which are TCP or TLS. Packing one again would hide it from cast.

instance Show RedirectRefused
instance Show Refusal
instance Show HeaderError
instance Show HTTPError
instance Show ClientError
instance Error HeaderError
instance Error HTTPError
instance FromHTTP HTTPError
instance FromHTTP SomeError

An HTTPError enters the shared error channel as itself, so cast finds it.

instance FromHTTP (Either l r) : FromHTTP l

An HTTP error is the left side's: Either HTTPError e holds it as a Left, and so does any Either l e whose l can hold one.