Skip to content

Commit 0c780aa

Browse files
committedMar 31, 2017
add proper error handling and error message
1 parent 3edd2c8 commit 0c780aa

15 files changed

+1037
-704
lines changed
 

‎jscomp/bin/all_ounit_tests.i.ml

+214-160
Large diffs are not rendered by default.

‎jscomp/bin/all_ounit_tests.ml

+127-87
Large diffs are not rendered by default.

‎jscomp/bin/bsb.ml

+111-86
Large diffs are not rendered by default.

‎jscomp/bin/bsdep.ml

+135-90
Large diffs are not rendered by default.

‎jscomp/bin/bsppx.ml

+135-90
Large diffs are not rendered by default.

‎jscomp/bin/whole_compiler.ml

+135-90
Large diffs are not rendered by default.

‎jscomp/ext/ext_json_parse.ml

+104-86
Large diffs are not rendered by default.

‎jscomp/ext/ext_json_parse.mli

+7
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
type error_info
26+
27+
exception Error of error_info
28+
29+
val pp_error : Format.formatter -> error_info -> unit
2530

2631
val parse_json : Lexing.lexbuf -> Ext_json_types.t
2732
val parse_json_from_string : string -> Ext_json_types.t
33+
2834
val parse_json_from_chan : in_channel -> Ext_json_types.t
35+
2936
val parse_json_from_file : string -> Ext_json_types.t
3037

‎jscomp/ext/ext_json_parse.mll

+23-5
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,36 @@ let report_error ppf = function
4343
-> fprintf ppf "Unterminated_comment"
4444

4545

46+
type error_info =
47+
{ error : error ;
48+
loc_start : Lexing.position;
49+
loc_end :Lexing.position;
50+
}
51+
52+
let pp_error fmt {error; loc_start ; loc_end } =
53+
Format.fprintf fmt "@[%a:@ %a@ -@ %a)@]"
54+
report_error error
55+
Ext_position.print loc_start
56+
Ext_position.print loc_end
57+
58+
exception Error of error_info
59+
60+
4661

4762
let () =
4863
Printexc.register_printer
4964
(function x ->
5065
match x with
51-
| Error (e , a, b) ->
52-
Some (Format.asprintf "@[%a:@ %a@ -@ %a)@]" report_error e
53-
Ext_position.print a Ext_position.print b)
66+
| Error error_info ->
67+
Some (Format.asprintf "%a" pp_error error_info)
68+
5469
| _ -> None
5570
)
5671

5772

5873

5974

75+
6076
type token =
6177
| Comma
6278
| Eof
@@ -71,9 +87,11 @@ type token =
7187
| String of string
7288
| True
7389

74-
7590
let error (lexbuf : Lexing.lexbuf) e =
76-
raise (Error (e, lexbuf.lex_start_p, lexbuf.lex_curr_p))
91+
raise (Error { error = e;
92+
loc_start = lexbuf.lex_start_p;
93+
loc_end = lexbuf.lex_curr_p})
94+
7795

7896
let lexeme_len (x : Lexing.lexbuf) =
7997
x.lex_curr_pos - x.lex_start_pos

‎jscomp/ounit_tests/ounit_cmd_tests.ml

+16-1
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,23 @@ external ff :
155155
( [`a | `b ] [@bs.string] )
156156
(* auto-convert to ocaml poly-variant *)
157157
*)
158-
end
158+
end;
159159

160+
__LOC__ >:: begin fun _ ->
161+
let should_err = bsc_eval {|
162+
type t
163+
external mk : int -> (_ [@bs.as {json| { x : 3 } |json}]) -> t = "" [@@bs.val]
164+
|} in
165+
OUnit.assert_bool __LOC__ (Ext_string.contain_substring should_err.stderr "Invalid json literal")
166+
end
167+
;
168+
__LOC__ >:: begin fun _ ->
169+
let should_err = bsc_eval {|
170+
type t
171+
external mk : int -> (_ [@bs.as {json| { "x" : 3 } |json}]) -> t = "" [@@bs.val]
172+
|} in
173+
OUnit.assert_bool __LOC__ (Ext_string.is_empty should_err.stderr)
174+
end
160175

161176

162177
]

‎jscomp/syntax/ast_arg.ml

+19-1
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,30 @@ type kind =
5656
arg_label : label
5757
}
5858

59-
let cst_json s : cst =
59+
type invalid_json = Ext_json_parse.error_info
60+
61+
let pp_invaild_json fmt err =
62+
Format.fprintf fmt "@[Invalid json literal: %a@]@."
63+
Ext_json_parse.pp_error err
64+
exception Error of Location.t * invalid_json
65+
66+
let () =
67+
Location.register_error_of_exn (function
68+
| Error (loc,err) ->
69+
Some (Location.error_of_printer loc pp_invaild_json err)
70+
| _ -> None
71+
)
72+
73+
74+
let cst_json loc s : cst =
6075
match Ext_json_parse.parse_json (Lexing.from_string s) with
6176
| True _ -> Arg_js_true
6277
| False _ -> Arg_js_false
6378
| Null _ -> Arg_js_null
6479
| _ -> Arg_js_json s
80+
| exception Ext_json_parse.Error error_info
81+
-> raise (Error (loc , error_info))
82+
6583
let cst_int i = Arg_int_lit i
6684
let cst_string s = Arg_string_lit s
6785
let empty_label = Empty None

‎jscomp/syntax/ast_arg.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type kind =
5555
arg_label :label
5656
}
5757

58-
val cst_json : string -> cst
58+
val cst_json : Location.t -> string -> cst
5959
val cst_int : int -> cst
6060
val cst_string : string -> cst
6161

‎jscomp/syntax/ast_external_attributes.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ let get_arg_type ~nolabel optional
4444
if Ast_core_type.is_any ptyp then (* (_[@bs.as ])*)
4545
if optional then
4646
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_underscore_type_in_external
47-
else begin match Ast_attributes.process_bs_string_or_int_as ptyp.Parsetree.ptyp_attributes with
47+
else begin
48+
match Ast_attributes.process_bs_string_or_int_as ptyp.Parsetree.ptyp_attributes with
4849
| None, _ ->
4950
Bs_syntaxerr.err ptyp.ptyp_loc Invalid_underscore_type_in_external
5051

@@ -56,7 +57,8 @@ let get_arg_type ~nolabel optional
5657
Arg_cst (Ast_arg.cst_string i), Ast_literal.type_string ~loc:ptyp.ptyp_loc ()
5758
| Some (`Json_str s), others ->
5859
Ast_attributes.warn_unused_attributes others;
59-
Arg_cst (Ast_arg.cst_json s), Ast_literal.type_string ~loc:ptyp.ptyp_loc ()
60+
Arg_cst (Ast_arg.cst_json ptyp.ptyp_loc s),
61+
Ast_literal.type_string ~loc:ptyp.ptyp_loc ()
6062

6163
end
6264
else (* ([`a|`b] [@bs.string]) *)

‎jscomp/test/prepend_data_ffi.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ process.on(function (i) {
3030
xx(3, 3, "xxx", "a", "b");
3131

3232
function f(x) {
33-
x.xx(109, /* int array */[
33+
x.xx(110, /* int array */[
3434
1,
3535
2,
3636
3
3737
]);
38-
x.xx(110, 3, "xxx", /* int array */[
38+
x.xx(111, 3, "xxx", /* int array */[
3939
1,
4040
2,
4141
3
4242
]);
43-
x.xx(111, 3, "xxx", 1, 2, 3);
44-
x.xx(112, 3, "xxx", 0, "b", 1, 2, 3, 4, 5);
45-
x.xx(113, 3, true, false, ("你好"), ( ["你好",1,2,3] ), ( [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}] ), "xxx", 0, "yyy", "b", 1, 2, 3, 4, 5);
43+
x.xx(112, 3, "xxx", 1, 2, 3);
44+
x.xx(113, 3, "xxx", 0, "b", 1, 2, 3, 4, 5);
45+
x.xx(114, 3, true, false, ("你好"), ( ["你好",1,2,3] ), ( [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}] ), ( [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}] ), "xxx", 0, "yyy", "b", 1, 2, 3, 4, 5);
4646
return /* () */0;
4747
}
4848

‎jscomp/test/prepend_data_ffi.ml

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ external on_exit_slice5 :
9292
-> (_ [@bs.as {json|"你好"|json}])
9393
-> (_ [@bs.as {json| ["你好",1,2,3] |json}])
9494
-> (_ [@bs.as {json| [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}] |json}])
95+
-> (_ [@bs.as {json| [{ "arr" : ["你好",1,2,3], "encoding" : "utf8"}] |json}])
9596
-> (_ [@bs.as "xxx"])
9697
-> ([`a|`b|`c] [@bs.int])
9798
-> (_ [@bs.as "yyy"])

0 commit comments

Comments
 (0)