Skip to content
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

ppx: consistent errors in runtime #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

andreypopp
Copy link
Collaborator

The main thing we fix here is the consistent use of exceptions in native runtime, which used, before this commit either its own Of_json_error or yojson's Type_error.

Then there's

val of_string : string -> t

which is not required by ppx but was introduced so that the runtime modules can function as minimal JSON interfaces usable both in native and browser environments (so called universal workflow).

Before this commit the error behaviour was not specified, now we introduce exception Of_json of string in both native and browser and make of_string raise it in case of invalid JSON.

Ref: #9

The main thing we fix here is the consistent use of exceptions in native
runtime, which used, before this commit either its own Of_json_error or
yojson's Type_error.

Then there's
```
val of_string : string -> t
```
which is not required by ppx but was introduced so that the runtime
modules can function as minimal JSON interfaces usable both in native
and browser environments (so called universal workflow).

Before this commit the error behaviour was not specified, now we
introduce `exception Of_json of string` in both native and browser and
make `of_string` raise it in case of invalid JSON.
Copy link
Member

@davesnx davesnx left a comment

Choose a reason for hiding this comment

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

Looks very good

let of_string s = Js.Json.parseExn s
let of_json_error msg = raise @@ Json.Decode.DecodeError msg

exception Json_error of string
Copy link
Member

Choose a reason for hiding this comment

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

encode_error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no, this is JSON parsing error, not encode

Copy link
Member

@davesnx davesnx Oct 8, 2024

Choose a reason for hiding this comment

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

True, miss-read the code. Parsing_error then? ^^

| Some jsexn -> Js.Exn.message jsexn
| None -> None
in
let msg = Option.value msg ~default:"JSON error" in
Copy link
Member

Choose a reason for hiding this comment

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

I would add a comment here, explaining that Js.Exn.message can't be none in the browser (and probably true in any js environment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

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.

2 participants