Skip to content

Commit ca41e2f

Browse files
committed
use custom exception for failure to upgrade protocols
This allows Mint.WebSocket to return some details that may be of interest when debugging or testing a failed WebSocket upgrade: the response HTTP status code and response headers.
1 parent 1f590b7 commit ca41e2f

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ The format is based on [Keep a
66
Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
77
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
## 0.3.0 - 2022-02-27
10+
11+
### Changed
12+
13+
- Failure to upgrade now gives a `Mint.WebSocket.UpgradeFailureError`
14+
as the error when a server returns a status code other than 101 for
15+
HTTP/1 or a status code in the range 200..299 range for HTTP/2.
16+
917
## 0.2.0 - 2022-02-17
1018

1119
This release is a breaking change from the 0.1.0 series. This update removes

lib/mint/web_socket.ex

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ defmodule Mint.WebSocket do
111111
"""
112112

113113
alias __MODULE__.{Utils, Extension, Frame}
114-
alias Mint.WebSocketError
114+
alias Mint.{WebSocketError, WebSocket.UpgradeFailureError}
115115
alias Mint.{HTTP1, HTTP2}
116116
import Mint.HTTP, only: [get_private: 2, put_private: 3, protocol: 1]
117117

@@ -129,7 +129,7 @@ defmodule Mint.WebSocket do
129129
private: %{},
130130
buffer: <<>>
131131

132-
@type error :: Mint.Types.error() | WebSocketError.t()
132+
@type error :: Mint.Types.error() | WebSocketError.t() | UpgradeFailureError.t()
133133

134134
@typedoc """
135135
Shorthand notations for control frames
@@ -330,8 +330,9 @@ defmodule Mint.WebSocket do
330330
do_new(protocol(conn), conn, status, response_headers)
331331
end
332332

333-
defp do_new(:http1, conn, status, _response_headers) when status != 101 do
334-
{:error, conn, %WebSocketError{reason: :connection_not_upgraded}}
333+
defp do_new(:http1, conn, status, headers) when status != 101 do
334+
error = %UpgradeFailureError{status_code: status, headers: headers}
335+
{:error, conn, error}
335336
end
336337

337338
defp do_new(:http1, conn, _status, response_headers) do
@@ -352,8 +353,9 @@ defmodule Mint.WebSocket do
352353
end
353354
end
354355

355-
defp do_new(:http2, conn, _status, _response_headers) do
356-
{:error, conn, %WebSocketError{reason: :connection_not_upgraded}}
356+
defp do_new(:http2, conn, status, headers) do
357+
error = %UpgradeFailureError{status_code: status, headers: headers}
358+
{:error, conn, error}
357359
end
358360

359361
@doc """
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule Mint.WebSocket.UpgradeFailureError do
2+
@moduledoc """
3+
An error representing a failure to upgrade protocols from HTTP to WebSocket
4+
"""
5+
6+
defexception [:status_code, :headers]
7+
8+
def message(%__MODULE__{} = error) do
9+
"""
10+
Could not upgrade from HTTP to WebSocket. The server returned status code #{error.status_code} with headers:
11+
#{inspect(error.headers)}
12+
"""
13+
end
14+
end

lib/mint/web_socket_error.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ defmodule Mint.WebSocketError do
99
reason_type =
1010
quote do
1111
:extended_connect_disabled
12-
| :connection_not_upgraded
1312
| :payload_too_large
1413
| {:extension_not_negotiated, Mint.WebSocket.Extension.t()}
1514
end
@@ -27,10 +26,6 @@ defmodule Mint.WebSocketError do
2726
"extended CONNECT method not enabled"
2827
end
2928

30-
defp format_reason(:connection_not_upgraded) do
31-
"connection not upgraded by remote"
32-
end
33-
3429
defp format_reason(:payload_too_large) do
3530
"frame payload cannot exceed 9,223,372,036,854,775,807 bytes"
3631
end

0 commit comments

Comments
 (0)