package ocaml-protoc

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file pb_exception.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
(*
  The MIT License (MIT)

  Copyright (c) 2016 Maxime Ransan <maxime.ransan@gmail.com>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

*)

(** {2 Compilation errors } *)

module P = Printf
module Loc = Pb_location

type programmatic_error =
  | Invalid_string_split
  | No_type_found_for_id

let string_of_programmatic_error e =
  "Programatic_error"
  ^
  match e with
  | Invalid_string_split -> "string split error"
  | No_type_found_for_id -> "no type was found for type id"

type unresolved_type = {
  field_name: string;
  type_: string;
  message_name: string;
}

type duplicate_field_number = {
  field_name: string;
  previous_field_name: string;
  message_name: string;
}

type invalid_default_value = {
  field_name: string option;
  info: string;
}

type unsupported_field_type = {
  field_name: string option;
  field_type: string;
  backend_name: string;
}

type error =
  | Unresolved_type of unresolved_type
    (* When the type of a field could not be resolved *)
  | Duplicated_field_number of duplicate_field_number
    (* When there are 2 field with either identical number or name *)
  | Invalid_default_value of invalid_default_value
    (* When a default value type type does not match the field type *)
  | Unsupported_field_type of unsupported_field_type
  | Programatic_error of programmatic_error
  | Invalid_import_qualifier of Loc.t
  | Invalid_file_name of string
  | Import_file_not_found of string
  | Invalid_packed_option of string
  | Missing_semicolon_for_enum_value of string * Loc.t
  | Invalid_enum_specification of string * Loc.t
  | Invalid_mutable_option of string option
  | Missing_one_of_name of Loc.t
  | Missing_field_label of string * string
  | Parsing_error of {
      msg: string;
      context: string;
      loc: Loc.t;
    }
  | Invalid_ppx_extension_option of string
  | Invalid_protobuf_syntax of string
  | Invalid_proto3_field_label of string * string
  | Default_field_option_not_supported of string * string
  | Invalid_first_enum_value_proto3 of string * string option
  | Invalid_key_type_for_map of string
  | Unsupported_wrapper_type of string
  | Invalid_rpc_req_type of string * string
  | Invalid_rpc_res_type of string * string

exception Compilation_error of error
(** Exception raised when a compilation error occurs *)

let string_of_error = function
  | Unresolved_type { field_name; type_; message_name } ->
    P.sprintf "unresolved type for field name : %s (type:%s, in message: %s)"
      field_name type_ message_name
  | Duplicated_field_number { field_name; previous_field_name; message_name } ->
    P.sprintf
      "duplicated field number for field name: %s (previous field name:%s, \
       message: %s)"
      field_name previous_field_name message_name
  | Invalid_default_value { field_name; info } ->
    P.sprintf "invalid default value for field name:%s (info: %s)"
      (Pb_util.Option.default "" field_name)
      info
  | Unsupported_field_type { field_name; field_type; backend_name } ->
    P.sprintf
      "unsupported field type for field name:%s with type:%s in bakend: %s"
      (Pb_util.Option.default "" field_name)
      field_type backend_name
  | Programatic_error e ->
    P.sprintf "programmatic error: %s" (string_of_programmatic_error e)
  | Invalid_import_qualifier loc ->
    P.sprintf "%sInvalid import qualified, only 'public' supported"
      (Loc.to_string loc)
  | Invalid_file_name file_name ->
    P.sprintf
      ("Invalid file name: %s, " ^^ "format must <name>.proto")
      file_name
  | Import_file_not_found file_name ->
    P.sprintf ("File: %s, " ^^ "could not be found.") file_name
  | Invalid_packed_option field_name ->
    P.sprintf "Invalid packed option for field: %s" field_name
  | Missing_semicolon_for_enum_value (enum_value, loc) ->
    P.sprintf "%sMissing semicolon for enum value: %s" (Loc.to_string loc)
      enum_value
  | Missing_one_of_name loc ->
    P.sprintf "%sMissing oneof name" (Loc.to_string loc)
  | Missing_field_label (field_name, message_name) ->
    P.sprintf
      ("Missing field label for field: %s in message: %s. "
     ^^ "[required|repeated|optional] expected")
      field_name message_name
  | Parsing_error { msg; context; loc } ->
    Printf.sprintf "%s%s at `%s'." (Loc.to_string loc) msg context
  | Invalid_enum_specification (enum_name, loc) ->
    P.sprintf
      "%sMissing enum specification (<identifier> = <id>;) for enum value: %s"
      (Loc.to_string loc) enum_name
  | Invalid_mutable_option field_name ->
    P.sprintf "Invalid mutable option for field %s"
      (Pb_util.Option.default "" field_name)
  | Invalid_ppx_extension_option message_name ->
    P.sprintf "Invalid ppx extension value for message: %s, string expected"
      message_name
  | Invalid_protobuf_syntax syntax ->
    P.sprintf
      "Invalid protobuf syntax: \"%s\", expected \"proto2\" or \"proto3\""
      syntax
  | Invalid_proto3_field_label (field_name, message_name) ->
    P.sprintf
      ("Invalid field label for field: %s in message: %s. "
     ^^ "[Required|Optional] are not supported.")
      field_name message_name
  | Invalid_rpc_req_type (service_name, rpc_name) ->
    P.sprintf
      ("Invalid type for RPC request: %s in service: %s. "
     ^^ "The type must be user-defined..")
      service_name rpc_name
  | Invalid_rpc_res_type (service_name, rpc_name) ->
    P.sprintf
      ("Invalid type for RPC response: %s in service: %s. "
     ^^ "The type must be user-defined..")
      service_name rpc_name
  | Default_field_option_not_supported (field_name, message_name) ->
    P.sprintf
      ("Explicit default values are not allowed in proto3. "
     ^^ "field name: %s, message name: %s.")
      field_name message_name
  | Invalid_first_enum_value_proto3 (enum_name, message_name) ->
    P.sprintf
      ("The first enum value must be 0 in proto3."
     ^^ "enum name: %s, message name: %s.")
      enum_name
      (Pb_util.Option.string_of_option (fun x -> x) message_name)
  | Invalid_key_type_for_map field_name ->
    P.sprintf
      ("Invalid key type for field %s. Map key type only supports integral "
     ^^ "and string type")
      field_name
  | Unsupported_wrapper_type type_name ->
    P.sprintf "Unsupported wrapper type %s, please raise issue on github."
      type_name

let () =
  Printexc.register_printer (fun exn ->
      match exn with
      | Compilation_error e -> Some (string_of_error e)
      | _ -> None)

let unresolved_type ~field_name ~type_ ~message_name () =
  raise
    (Compilation_error (Unresolved_type { field_name; type_; message_name }))

let duplicated_field_number ~field_name ~previous_field_name ~message_name () =
  raise
    (Compilation_error
       (Duplicated_field_number
          { field_name; previous_field_name; message_name }))

let invalid_default_value ?field_name ~info () =
  raise (Compilation_error (Invalid_default_value { field_name; info }))

let unsupported_field_type ?field_name ~field_type ~backend_name () =
  raise
    (Compilation_error
       (Unsupported_field_type { field_name; field_type; backend_name }))

let import_file_not_found file_name =
  raise (Compilation_error (Import_file_not_found file_name))

let programmatic_error e = raise (Compilation_error (Programatic_error e))

let invalid_import_qualifier loc =
  raise (Compilation_error (Invalid_import_qualifier loc))

let invalid_file_name file_name =
  raise (Compilation_error (Invalid_file_name file_name))

let invalid_packed_option field_name =
  raise (Compilation_error (Invalid_packed_option field_name))

let missing_semicolon_for_enum_value enum_value loc =
  raise (Compilation_error (Missing_semicolon_for_enum_value (enum_value, loc)))

let invalid_enum_specification enum_name loc =
  raise (Compilation_error (Invalid_enum_specification (enum_name, loc)))

let invalid_mutable_option ?field_name () =
  raise (Compilation_error (Invalid_mutable_option field_name))

let missing_one_of_name loc =
  raise (Compilation_error (Missing_one_of_name loc))

let missing_field_label ~field_name ~message_name =
  raise (Compilation_error (Missing_field_label (field_name, message_name)))

let invalid_ppx_extension_option message_name =
  raise (Compilation_error (Invalid_ppx_extension_option message_name))

let ocamlyacc_parsing_error loc context =
  raise
    (Compilation_error (Parsing_error { msg = "Parsing error"; context; loc }))

let protoc_parsing_error e loc context =
  raise
    (Compilation_error (Parsing_error { msg = string_of_error e; context; loc }))

let unknown_parsing_error ~msg ~context loc =
  raise (Compilation_error (Parsing_error { msg; context; loc }))

let invalid_protobuf_syntax syntax =
  raise (Compilation_error (Invalid_protobuf_syntax syntax))

let invalid_proto3_field_label ~field_name ~message_name =
  raise
    (Compilation_error (Invalid_proto3_field_label (field_name, message_name)))

let invalid_rpc_req_type ~service_name ~rpc_name () =
  raise (Compilation_error (Invalid_rpc_req_type (service_name, rpc_name)))

let invalid_rpc_res_type ~service_name ~rpc_name () =
  raise (Compilation_error (Invalid_rpc_res_type (service_name, rpc_name)))

let default_field_option_not_supported ~field_name ~message_name =
  raise
    (Compilation_error
       (Default_field_option_not_supported (field_name, message_name)))

let invalid_first_enum_value_proto3 ?message_name ~enum_name () =
  raise
    (Compilation_error
       (Invalid_first_enum_value_proto3 (enum_name, message_name)))

let invalid_key_type_for_map field_name =
  raise (Compilation_error (Invalid_key_type_for_map field_name))

let unsupported_wrapper_type type_name =
  raise (Compilation_error (Unsupported_wrapper_type type_name))
OCaml

Innovation. Community. Security.