Skip to content

Commit

Permalink
simplify errors and exns
Browse files Browse the repository at this point in the history
  • Loading branch information
jchavarri committed Dec 15, 2024
1 parent 6a45efa commit 4bfb448
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 20 deletions.
4 changes: 2 additions & 2 deletions examples/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ let _ =
let json = {|{ "y": 42 } |} |> Json.parseOrRaise in
match Json.Decode.(field "x" int json) with
| x -> Js.log x
| exception Json.Decode.DecodeError err ->
Js.log ("Error:" ^ Json.Decode.error_to_string err)
| exception Json.Of_json_error err ->
Js.log ("Error:" ^ Json.of_json_error_to_string err)
8 changes: 3 additions & 5 deletions ppx/browser/ppx_deriving_json_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ let to_json t = t
let of_json t = t
let to_string t = Js.Json.stringify t

exception Of_string_error of string

let of_string s =
try Js.Json.parseExn s
with exn ->
Expand All @@ -20,13 +18,13 @@ let of_string s =
(* msg really cannot be None in browser or any sane JS runtime *)
Option.value msg ~default:"JSON error"
in
raise (Of_string_error msg)
raise (Json.Of_string_error msg)

type error = Json.Decode.error =
type error = Json.of_json_error =
| Json_error of string
| Unexpected_variant of string

exception Of_json_error = Json.Decode.DecodeError
exception Of_json_error = Json.Of_json_error

let of_json_error msg = raise (Of_json_error (Json_error msg))

Expand Down
7 changes: 0 additions & 7 deletions src/Json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,6 @@ end

module Decode = struct
type 'a decoder = 'a of_json
type error = Json_error of string | Unexpected_variant of string

let error_to_string = function
| Json_error msg -> msg
| Unexpected_variant tag -> "unexpected variant: " ^ tag

exception DecodeError of error

let id json = json
let bool = Of_json.bool
Expand Down
8 changes: 2 additions & 6 deletions src/Json.mli
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ val to_json : json to_json
(** The type of a error which occurs during decoding JSON values. *)
type of_json_error = Json_error of string | Unexpected_variant of string

val of_json_error_to_string : of_json_error -> string

type exn +=
| Of_json_error of of_json_error
(** The exception raised when a decoding error occurs *)
Expand Down Expand Up @@ -263,12 +265,6 @@ module Decode : sig
type 'a decoder = 'a of_json [@@deprecated "Use `of_json` instead"]
(** The type of a decoder combinator *)

type error = Json_error of string | Unexpected_variant of string

val error_to_string : error -> string

exception DecodeError of error

val id : t of_json [@@deprecated "Use `of_json` instead"]
val bool : bool of_json [@@deprecated "Use `Of_json.bool` instead"]
val float : float of_json [@@deprecated "Use `Of_json.float` instead"]
Expand Down

0 comments on commit 4bfb448

Please sign in to comment.