http_error.ex (1999B)
1 defmodule Mint.HTTPError do 2 @moduledoc """ 3 An HTTP error. 4 5 This exception struct is used to represent HTTP errors of all sorts and for 6 both HTTP/1 and HTTP/2. 7 8 A `Mint.HTTPError` struct is an exception, so it can be raised as any 9 other exception. 10 11 ## Struct 12 13 The `Mint.HTTPError` struct is opaque, that is, not all of its fields are public. 14 The list of public fields is: 15 16 * `:reason` - the error reason. Can be one of: 17 18 * a term of type `t:Mint.HTTP1.error_reason/0`. See its documentation for 19 more information. 20 21 * a term of type `t:Mint.HTTP2.error_reason/0`. See its documentation for 22 more information. 23 24 * `{:proxy, reason}`, which is used when an HTTP error happens when connecting 25 to a tunnel proxy. `reason` can be: 26 27 * `:tunnel_timeout` - when the tunnel times out. 28 29 * `{:unexpected_status, status}` - when the proxy returns an unexpected 30 status `status`. 31 32 * `{:unexpected_trailing_responses, responses}` - when the proxy returns 33 unexpected responses (`responses`). 34 35 ## Message representation 36 37 If you want to convert an error reason to a human-friendly message (for example 38 for using in logs), you can use `Exception.message/1`: 39 40 iex> {:error, %Mint.HTTPError{} = error} = Mint.HTTP.connect(:http, "badresponse.com", 80) 41 iex> Exception.message(error) 42 "the response contains two or more Content-Length headers" 43 44 """ 45 46 alias Mint.{HTTP1, HTTP2} 47 48 @type proxy_reason() :: 49 {:proxy, 50 HTTP1.error_reason() 51 | HTTP2.error_reason() 52 | :tunnel_timeout 53 | {:unexpected_status, non_neg_integer()} 54 | {:unexpected_trailing_responses, list()}} 55 56 @type t() :: %__MODULE__{ 57 reason: HTTP1.error_reason() | HTTP2.error_reason() | proxy_reason() | term() 58 } 59 60 defexception [:reason, :module] 61 62 def message(%__MODULE__{reason: reason, module: module}) do 63 module.format_error(reason) 64 end 65 end