Skip to content

Commit 697b65b

Browse files
Simplify fetch error handling, add bad url integration spec (#47)
This massive case statement is not useful in practice, let's simplify things
1 parent cb63d6e commit 697b65b

File tree

2 files changed

+23
-59
lines changed

2 files changed

+23
-59
lines changed

integration/src/Main.elm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ specs =
5050
, httpTimeoutSpec
5151
, httpBadBodySpec
5252
, httpBadStatusSpec
53+
, httpBadUrlSpec
5354
, randomSpec
5455
, timeZoneSpec
5556
, timeHereSpec
@@ -379,6 +380,22 @@ httpBadStatusSpec =
379380
(Spec.assertError (shouldHaveBadStatus 400))
380381

381382

383+
httpBadUrlSpec : Spec
384+
httpBadUrlSpec =
385+
Spec.describe "http bad url"
386+
"should surface a BadUrl error if url is invalid"
387+
(Http.get
388+
{ url = "WAT WAT"
389+
, headers = []
390+
, expect = Http.expectWhatever
391+
, timeout = Nothing
392+
}
393+
)
394+
(Spec.assertError
395+
(Spec.shouldEqual (Http.BadUrl "WAT WAT"))
396+
)
397+
398+
382399
randomSpec : Spec
383400
randomSpec =
384401
Spec.describe

runner/http/fetch.ts

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -75,64 +75,11 @@ export function http(request: HttpRequest): Promise<HttpResponse> {
7575
}
7676

7777
function toHttpError(err: any): HttpError {
78-
switch (err.cause?.code) {
79-
case "ENOTFOUND":
80-
return "NETWORK_ERROR";
81-
case "ECONNREFUSED":
82-
return "NETWORK_ERROR";
83-
case "ECONNRESET":
84-
return "NETWORK_ERROR";
85-
case "EAGAIN":
86-
return "NETWORK_ERROR";
87-
case "ERR_INVALID_URL":
88-
return "BAD_URL";
89-
90-
case "UND_ERR":
91-
return "NETWORK_ERROR";
92-
case "UND_ERR_CONNECT_TIMEOUT":
93-
return "NETWORK_ERROR";
94-
case "UND_ERR_HEADERS_TIMEOUT":
95-
return "NETWORK_ERROR";
96-
case "UND_ERR_HEADERS_OVERFLOW":
97-
return "NETWORK_ERROR";
98-
case "UND_ERR_BODY_TIMEOUT":
99-
return "NETWORK_ERROR";
100-
case "UND_ERR_RESPONSE_STATUS_CODE":
101-
return "NETWORK_ERROR";
102-
case "UND_ERR_INVALID_ARG":
103-
return "NETWORK_ERROR";
104-
case "UND_ERR_INVALID_RETURN_VALUE":
105-
return "NETWORK_ERROR";
106-
case "UND_ERR_ABORTED":
107-
return "NETWORK_ERROR";
108-
case "UND_ERR_DESTROYED":
109-
return "NETWORK_ERROR";
110-
case "UND_ERR_CLOSED":
111-
return "NETWORK_ERROR";
112-
case "UND_ERR_SOCKET":
113-
return "NETWORK_ERROR";
114-
case "UND_ERR_NOT_SUPPORTED":
115-
return "NETWORK_ERROR";
116-
case "UND_ERR_REQ_CONTENT_LENGTH_MISMATCH":
117-
return "NETWORK_ERROR";
118-
case "UND_ERR_RES_CONTENT_LENGTH_MISMATCH":
119-
return "NETWORK_ERROR";
120-
case "UND_ERR_INFO":
121-
return "NETWORK_ERROR";
122-
case "UND_ERR_RES_EXCEEDED_MAX_SIZE":
123-
return "NETWORK_ERROR";
124-
}
125-
126-
switch (err.name) {
127-
case "AbortError":
128-
return "TIMEOUT";
78+
if (err.name === "AbortError") {
79+
return "TIMEOUT";
80+
} else if (err.cause?.code === "ERR_INVALID_URL") {
81+
return "BAD_URL";
82+
} else {
83+
return "NETWORK_ERROR";
12984
}
130-
131-
console.warn(
132-
`Unknown Http fetch error, consider submitting a PR adding an explicit case for this
133-
https://github.com/andrewMacmurray/elm-concurrent-task/blob/main/runner/http/fetch.ts#L60
134-
`,
135-
err
136-
);
137-
return "NETWORK_ERROR";
13885
}

0 commit comments

Comments
 (0)