Skip to content

Commit

Permalink
Example with exception-annotated fetch.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoc committed Jun 12, 2022
1 parent fc31e65 commit 15c4b8c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
3 changes: 3 additions & 0 deletions example-async/bsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "example-async",
"reanalyze": {
"analysis": ["exception"]
},
"sources": [
{
"dir": "src",
Expand Down
22 changes: 18 additions & 4 deletions example-async/src/AA.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ async function nestedPromise(x) {
return 32;
}

function $$fetch$1(url) {
return fetch(url);
}

function status(response) {
return response.status;
}

var Fetch = {
$$fetch: $$fetch$1,
status: status
};

var explainError = ((e)=>e.toString());

async function testFetch(url) {
Expand Down Expand Up @@ -193,7 +206,7 @@ async function testFetchMany() {

tests.push(testFetchMany);

async function $$fetch$1(url) {
async function $$fetch$2(url) {
var response;
try {
response = await fetch(url);
Expand All @@ -215,15 +228,15 @@ async function $$fetch$1(url) {
}

var FetchResult = {
$$fetch: $$fetch$1
$$fetch: $$fetch$2
};

function nextFetch(_response) {
return "https://github.com/";
}

async function testFetchWithResult() {
var response1 = await $$fetch$1("https://www.google.com");
var response1 = await $$fetch$2("https://www.google.com");
if (response1.TAG !== /* Ok */0) {
return ;
}
Expand All @@ -233,7 +246,7 @@ async function testFetchWithResult() {
if (url === undefined) {
return ;
}
var response2 = await $$fetch$1(url);
var response2 = await $$fetch$2(url);
if (response2.TAG !== /* Ok */0) {
return ;
}
Expand Down Expand Up @@ -288,6 +301,7 @@ export {
testTryCatch ,
singlePromise ,
nestedPromise ,
Fetch ,
explainError ,
testFetch ,
withCallback ,
Expand Down
19 changes: 14 additions & 5 deletions example-async/src/AA.res
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,23 @@ let nestedPromise =
//
// Test error handling in fetch

module Fetch = {
@raises(JsError)
let fetch = url => Fetch.fetch(url)

@raises([])
let status = response => Fetch.Response.status(response)
}

let explainError: unknown => string = %raw(`(e)=>e.toString()`)

let testFetch =
@async
(. url) => {
switch {@await Fetch.fetch(url)} {
open Fetch
switch {@await fetch(url)} {
| response =>
let status = response->Fetch.Response.status
let status = response->status
Js.log2("Fetch returned status:", status)
| exception JsError(e) => Js.log2("Fetch returned an error:", e->explainError)
}
Expand Down Expand Up @@ -132,7 +141,7 @@ let fetchAndCount = {
(. url) => {
let response = @await Fetch.fetch(url)
counter := counter.contents + 1
(counter.contents, response->Fetch.Response.status)
(counter.contents, response->Fetch.status)
}

ff
Expand Down Expand Up @@ -177,13 +186,13 @@ let testFetchWithResult =
switch @await
FetchResult.fetch(. "https://www.google.com") {
| Ok(response1) =>
Js.log2("FetchResult response1", response1->Fetch.Response.status)
Js.log2("FetchResult response1", response1->Fetch.status)
switch nextFetch(. response1) {
| None => ()
| Some(url) =>
switch @await
FetchResult.fetch(. url) {
| Ok(response2) => Js.log2("FetchResult response2", response2->Fetch.Response.status)
| Ok(response2) => Js.log2("FetchResult response2", response2->Fetch.status)
| Error(_) => ()
}
}
Expand Down

0 comments on commit 15c4b8c

Please sign in to comment.