From 4bfb448e26e1d8d7ca3b2fc63e21a06e89a3bd9b Mon Sep 17 00:00:00 2001 From: Javier Chavarri Date: Sun, 15 Dec 2024 15:34:00 +0000 Subject: [PATCH] simplify errors and exns --- examples/decode.ml | 4 ++-- ppx/browser/ppx_deriving_json_runtime.ml | 8 +++----- src/Json.ml | 7 ------- src/Json.mli | 8 ++------ 4 files changed, 7 insertions(+), 20 deletions(-) diff --git a/examples/decode.ml b/examples/decode.ml index 356f450..2c13eb5 100644 --- a/examples/decode.ml +++ b/examples/decode.ml @@ -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) diff --git a/ppx/browser/ppx_deriving_json_runtime.ml b/ppx/browser/ppx_deriving_json_runtime.ml index 03dbefa..0d5ece2 100644 --- a/ppx/browser/ppx_deriving_json_runtime.ml +++ b/ppx/browser/ppx_deriving_json_runtime.ml @@ -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 -> @@ -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)) diff --git a/src/Json.ml b/src/Json.ml index 1ac1615..b156a86 100644 --- a/src/Json.ml +++ b/src/Json.ml @@ -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 diff --git a/src/Json.mli b/src/Json.mli index 3ba7cc7..820f002 100644 --- a/src/Json.mli +++ b/src/Json.mli @@ -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 *) @@ -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"]