Skip to content

Commit f6fd413

Browse files
coreytiry4n1m3
andcommitted
Split Error types into API and SDK
Co-authored-by: Ryan Spore <[email protected]>
1 parent f6e1532 commit f6fd413

18 files changed

+71
-48
lines changed

lib/playwright/sdk/channel/error.ex renamed to lib/playwright/api/error.ex

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
defmodule Playwright.SDK.Channel.Error do
1+
defmodule Playwright.API.Error do
22
@moduledoc false
33
# `Error` represents an error message received from the Playwright server
44
# that is in response to a `Message` previously sent.
5-
alias Playwright.SDK.Channel
65

76
@enforce_keys [:type, :message]
87
defstruct [:type, :message]
@@ -13,17 +12,15 @@ defmodule Playwright.SDK.Channel.Error do
1312
}
1413

1514
def new(%{error: %{name: name, message: message} = _error}, _catalog) do
16-
%Channel.Error{
15+
%__MODULE__{
1716
type: name,
1817
message: String.split(message, "\n") |> List.first()
1918
}
2019
end
21-
22-
# TODO: determine why we get here...
23-
# DONE: see comment at error_handling.ex:9.
24-
def new(%{error: %{message: message} = _error}, _catalog) do
25-
%Channel.Error{
26-
type: "NotImplementedError",
20+
def new(%{error: %{message: message} = error}, _catalog) do
21+
dbg(error)
22+
%__MODULE__{
23+
type: "UnknownError",
2724
message: String.split(message, "\n") |> List.first()
2825
}
2926
end

lib/playwright/api/errors/web_error.ex

Whitespace-only changes.

lib/playwright/browser_context.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ defmodule Playwright.BrowserContext do
468468
end)
469469
end
470470

471-
@spec grant_permissions(t(), [String.t()], options()) :: t() | {:error, Channel.Error.t()}
471+
@spec grant_permissions(t(), [String.t()], options()) :: t() | {:error, Playwright.API.Error.t()}
472472
def grant_permissions(%BrowserContext{} = context, permissions, options \\ %{}) do
473473
params = Map.merge(%{permissions: permissions}, options)
474474
post!(context, :grant_permissions, params)

lib/playwright/cdp_session.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Playwright.CDPSession do
3333
{:ok, _} ->
3434
cdp_session
3535

36-
{:error, %Channel.Error{} = error} ->
36+
{:error, %Playwright.API.Error{} = error} ->
3737
{:error, error}
3838
end
3939
end

lib/playwright/frame.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ defmodule Playwright.Frame do
905905
906906
FIXME: the following is NOT TRUE... Returns `nil` if waiting for a hidden or detached element.
907907
"""
908-
@spec wait_for_selector(t(), binary(), map()) :: ElementHandle.t() | {:error, Channel.Error.t()}
908+
@spec wait_for_selector(t(), binary(), map()) :: ElementHandle.t() | {:error, Playwright.API.Error.t()}
909909
def wait_for_selector(%Frame{session: session} = frame, selector, options \\ %{}) do
910910
Channel.post(session, {:guid, frame.guid}, :wait_for_selector, Map.merge(%{selector: selector}, options))
911911
end

lib/playwright/locator.ex

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ defmodule Playwright.Locator do
262262
If the element is detached from the DOM at any moment during the action, this method throws.
263263
264264
When all steps combined have not finished during the specified timeout, this method throws a
265-
`Playwright.SDK.Channel.Error.t()`. Passing `0` timeout disables this.
265+
`Playwright.API.Error.t()`. Passing `0` timeout disables this.
266266
267267
## Returns
268268
@@ -282,10 +282,10 @@ defmodule Playwright.Locator do
282282
| `:timeout` | option | `number()` | Maximum time in milliseconds. Pass `0` to disable timeout. The default value can be changed via `Playwright.BrowserContext.set_default_timeout/2` or `Playwright.Page.set_default_timeout/2`. `(default: 30 seconds)` |
283283
| `:trial` | option | `boolean()` | When set, this call only performs the actionability checks and skips the action. Useful to wait until the element is ready for the action without performing it. `(default: false)` |
284284
"""
285-
@spec click(t(), options_click()) :: :ok
285+
@spec click(t(), options_click()) :: t()
286286
def click(%Locator{} = locator, options \\ %{}) do
287287
options = Map.merge(options, %{strict: true})
288-
Frame.click(locator.frame, locator.selector, options)
288+
returning(locator, fn -> Frame.click(locator.frame, locator.selector, options) end)
289289
end
290290

291291
# @spec content_frame(Locator.t()) :: FrameLocator.t()
@@ -420,15 +420,15 @@ defmodule Playwright.Locator do
420420
## Returns
421421
422422
- `Playwright.ElementHandle.t()`
423-
- `{:error, Playwright.SDK.Channel.Error.t()}`
423+
- `{:error, Playwright.API.Error.t()}`
424424
425425
## Arguments
426426
427427
| key/name | type | | description |
428428
| ---------- | ------ | ---------- | ----------- |
429429
| `:timeout` | option | `number()` | Maximum time in milliseconds. Pass `0` to disable timeout. The default value can be changed by using the `Playwright.BrowserContext.set_default_timeout/2` or `Playwright.Page.set_default_timeout/2` functions. `(default: 30 seconds)` |
430430
"""
431-
@spec element_handle(t(), options()) :: ElementHandle.t() | {:error, Channel.Error.t()}
431+
@spec element_handle(t(), options()) :: ElementHandle.t() | {:error, Playwright.API.Error.t()}
432432
def element_handle(%Locator{} = locator, options \\ %{}) do
433433
options = Map.merge(%{strict: true, state: "attached"}, options)
434434

@@ -529,7 +529,7 @@ defmodule Playwright.Locator do
529529
## Returns
530530
531531
- `Playwright.ElementHandle.t()`
532-
- `{:error, Playwright.SDK.Channel.Error.t()}`
532+
- `{:error, Playwright.API.Error.t()}`
533533
534534
## Arguments
535535
@@ -539,7 +539,7 @@ defmodule Playwright.Locator do
539539
| `arg` | param | `any()` | Argument to pass to `expression` `(optional)` |
540540
| `:timeout` | option | `number()` | Maximum time in milliseconds. Pass `0` to disable timeout. The default value can be changed by using the `Playwright.BrowserContext.set_default_timeout/2` or `Playwright.Page.set_default_timeout/2` functions. `(default: 30 seconds)` |
541541
"""
542-
@spec evaluate_handle(t(), binary(), any(), options()) :: ElementHandle.t() | {:error, Channel.Error.t()}
542+
@spec evaluate_handle(t(), binary(), any(), options()) :: ElementHandle.t() | {:error, Playwright.API.Error.t()}
543543
def evaluate_handle(locator, expression, arg \\ nil, options \\ %{})
544544

545545
# NOTE: need to do all of the map-like things before a plain `map()`,
@@ -1294,7 +1294,7 @@ defmodule Playwright.Locator do
12941294
# const orderSent = page.locator('#order-sent');
12951295
# await orderSent.waitFor();
12961296

1297-
@spec wait_for(t(), options()) :: t() | {:error, Channel.Error.t()}
1297+
@spec wait_for(t(), options()) :: t() | {:error, Playwright.API.Error.t()}
12981298
def wait_for(%Locator{} = locator, options \\ %{}) do
12991299
case Frame.wait_for_selector(locator.frame, locator.selector, options) do
13001300
%ElementHandle{} ->
@@ -1309,8 +1309,12 @@ defmodule Playwright.Locator do
13091309
# ---------------------------------------------------------------------------
13101310

13111311
defp returning(subject, task) do
1312-
task.()
1313-
subject
1312+
case task.() do
1313+
{:error, _} = error ->
1314+
error
1315+
_ ->
1316+
subject
1317+
end
13141318
end
13151319

13161320
defp with_element(%Locator{frame: frame} = locator, options, task) do

lib/playwright/response.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ defmodule Playwright.Response do
6262
end
6363

6464
@spec ok({t(), t()}) :: boolean()
65-
def ok({:error, %Playwright.SDK.Channel.Error{}}) do
65+
def ok({:error, %Playwright.SDK.Error{}}) do
6666
false
6767
end
6868

lib/playwright/sdk/channel.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Playwright.SDK.Channel do
22
@moduledoc false
33
import Playwright.SDK.Helpers.ErrorHandling
4-
alias Playwright.SDK.Channel.{Catalog, Connection, Error, Event, Message, Response, Session}
4+
alias Playwright.SDK.Channel.{Catalog, Connection, Event, Message, Response, Session}
55

66
# API
77
# ---------------------------------------------------------------------------
@@ -135,7 +135,7 @@ defmodule Playwright.SDK.Channel do
135135
item
136136
end
137137

138-
defp reply(%Error{} = error, from) do
138+
defp reply(%Playwright.API.Error{} = error, from) do
139139
Task.start_link(fn ->
140140
GenServer.reply(from, {:error, error})
141141
end)

lib/playwright/sdk/channel/catalog.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ defmodule Playwright.SDK.Channel.Catalog do
77
"""
88
use GenServer
99
import Playwright.SDK.Helpers.ErrorHandling
10-
alias Playwright.SDK.Channel
1110

1211
defstruct [:awaiting, :storage]
1312

@@ -74,7 +73,7 @@ defmodule Playwright.SDK.Channel.Catalog do
7473
| `guid` | param | `binary()` | GUID to look up |
7574
| `:timeout` | option | `float()` | Maximum time to wait, in milliseconds. Defaults to `30_000` (30 seconds). |
7675
"""
77-
@spec get(pid(), binary(), map()) :: struct() | {:error, Channel.Error.t()}
76+
@spec get(pid(), binary(), map()) :: struct() | {:error, Playwright.API.Error.t()}
7877
def get(catalog, guid, options \\ %{}) do
7978
with_timeout(options, fn timeout ->
8079
GenServer.call(catalog, {:get, {:guid, guid}}, timeout)

lib/playwright/sdk/channel/response.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defmodule Playwright.SDK.Channel.Response do
3838
end
3939

4040
def recv(_session, %{error: error, id: _}) do
41-
Channel.Error.new(error, nil)
41+
Playwright.API.Error.new(error, nil)
4242
end
4343

4444
def recv(session, %{id: _} = message) do

0 commit comments

Comments
 (0)