Skip to content

Document the new exception API #1046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2025
Merged

Document the new exception API #1046

merged 3 commits into from
Jun 4, 2025

Conversation

tsnobip
Copy link
Member

@tsnobip tsnobip commented Jun 4, 2025

Fixes #1032.

Copy link

vercel bot commented Jun 4, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rescript-lang.org ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 4, 2025 0:14am

@tsnobip tsnobip requested a review from fhammerschmidt June 4, 2025 11:30
Copy link

cloudflare-workers-and-pages bot commented Jun 4, 2025

Deploying rescript-lang-org with  Cloudflare Pages  Cloudflare Pages

Latest commit: b757c51
Status: ✅  Deploy successful!
Preview URL: https://fd92a308.rescript-lang.pages.dev
Branch Preview URL: https://update-exceptions.rescript-lang.pages.dev

View logs

@@ -434,6 +420,36 @@ try {
}
```

### Throw a value that is not an error

For some reason, if you need to throw something that is not a JS error, use `JsExn.throw`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to throw any value that is not a valid JS Error, use JsExn.throw:

Copy link
Contributor

@nojaf nojaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for tackling this.
I can't deny that it is still a very confusing area of the language.

Could you also link to the Promise API docs in async-await.mdx?
The example of what to do with Promise.catch on https://rescript-langorg-git-update-exceptions-rescript-association.vercel.app/docs/manual/v12.0.0/api/core/promise#value-catch is out of sync I think.

}

let checkAuth = async () => {
try {
await authenticate()
} catch {
| Exn.Error(e) =>
switch Exn.message(e) {
| JsExn(e) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still a little confusing where JsExn is coming from.
Is this the "Exceptions are just a special kind of variant" from the beginning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just updated this page so it stays in sync with the new API, I think this could be improved in another PR.

}

let checkAuth = async () => {
switch await authenticate() {
| _ => Console.log("ok")
| exception Exn.Error(e) =>
switch Exn.message(e) {
| exception JsExn(e) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this exhaustive? How does this work with the other built-in exceptions?
Do we cover all of them with this?
Do I add exception Invalid_argument above this case when I want to match that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already covered in the exceptions page, I fear we would repeat ourselves too much otherwise.

Copy link
Contributor

@nojaf nojaf Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is one of looking at it, but there you assume your user has read that exceptions page.
And so you assume the docs are read in a certain order.
This is not my experience with the docs. I often go and look at documentation because I have a specific problem, so I find links between these pages very valuable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nojaf you're totally right, I'd just change this a subsequent PR, I just replaced the calls to the Exn module.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Paul!

@@ -130,15 +130,15 @@ You may use `try / catch` or `switch` to handle exceptions during async executio
```res
// For simulation purposes
let authenticate = async () => {
raise(Exn.raiseRangeError("Authentication failed."))
JsError.RangeError.throwWithMessage("Authentication failed.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are JsError and JsExn the same thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsError is the module around JS errors while JsExn is the module around things that can be thrown and caught in JS, so pretty much anything actually.

@tsnobip
Copy link
Member Author

tsnobip commented Jun 4, 2025

@nojaf

Thank you very much for tackling this. I can't deny that it is still a very confusing area of the language.

Well, I really tried my best but I think the previous situation was even worse, but of course we're open to any idea that could further improve the situation!

Could you also link to the Promise API docs in async-await.mdx?

I didn't specifically update the async-await.mdx page, I just wanted to make sure we stopped referencing the deprecated API, I think improvements of this page should come in a different PR.

The example of what to do with Promise.catch on https://rescript-langorg-git-update-exceptions-rescript-association.vercel.app/docs/manual/v12.0.0/api/core/promise#value-catch is out of sync I think.

The Stdlib/Core API hasn't been synced for ages, as soon as this (#981) is done, it will be corrected since the docstring is fixed already.

Copy link
Member

@fhammerschmidt fhammerschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's still confusing and we need to improve in subsequent PRs.

@tsnobip tsnobip merged commit e18406c into master Jun 4, 2025
4 checks passed
@fhammerschmidt fhammerschmidt deleted the update-exceptions branch June 4, 2025 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exceptions documentation
3 participants