type t =
| Interrupted
| No_error
| Windows_killed_by_task_manager
| Type_error
| Out_of_time
| Kill_error
| Unused_server
| No_server_running
| Out_of_retries
| Invalid_flowconfig
| Path_is_not_a_file
| Build_id_mismatch
| Input_error
| Lock_stolen
| Could_not_find_flowconfig
| Server_out_of_date
| Out_of_shared_memory
| Flowconfig_changed
| Server_client_directory_mismatch
| Commandline_usage_error
| No_input
| Server_start_failed of Unix.process_status
| Missing_flowlib
| Autostop
| Killed_by_monitor
| Invalid_saved_state
| Restart
| Socket_error
| Dfind_died
| Dfind_unresponsive
| Watchman_error
| Hash_table_full
| Heap_full
| Unknown_error
let error_code = function
| Interrupted -> -6
| No_error -> 0
| Windows_killed_by_task_manager -> 1
| Type_error -> 2
| Out_of_time -> 3
| Kill_error -> 4
| Unused_server -> 5
| No_server_running -> 6
| Out_of_retries -> 7
| Invalid_flowconfig -> 8
| Build_id_mismatch -> 9
| Input_error -> 10
| Lock_stolen -> 11
| Could_not_find_flowconfig -> 12
| Server_out_of_date -> 13
| Server_client_directory_mismatch -> 14
| Out_of_shared_memory -> 15
| Flowconfig_changed -> 16
| Path_is_not_a_file -> 17
| Autostop -> 18
| Killed_by_monitor -> 19
| Invalid_saved_state -> 20
| Restart -> 21
| Commandline_usage_error -> 64
| No_input -> 66
| Server_start_failed _ -> 78
| Missing_flowlib -> 97
| Socket_error -> 98
| Dfind_died -> 99
| Dfind_unresponsive -> 100
| Watchman_error -> 101
| Hash_table_full -> 102
| Heap_full -> 103
| Unknown_error -> 110
let error_type = function
| -6 -> Interrupted
| 0 -> No_error
| 1 -> Windows_killed_by_task_manager
| 2 -> Type_error
| 3 -> Out_of_time
| 4 -> Kill_error
| 5 -> Unused_server
| 6 -> No_server_running
| 7 -> Out_of_retries
| 8 -> Invalid_flowconfig
| 9 -> Build_id_mismatch
| 10 -> Input_error
| 11 -> Lock_stolen
| 12 -> Could_not_find_flowconfig
| 13 -> Server_out_of_date
| 14 -> Server_client_directory_mismatch
| 15 -> Out_of_shared_memory
| 16 -> Flowconfig_changed
| 17 -> Path_is_not_a_file
| 18 -> Autostop
| 19 -> Killed_by_monitor
| 20 -> Invalid_saved_state
| 21 -> Restart
| 64 -> Commandline_usage_error
| 66 -> No_input
| 78 -> Server_start_failed (Unix.WEXITED (-1))
| 97 -> Missing_flowlib
| 98 -> Socket_error
| 99 -> Dfind_died
| 100 -> Dfind_unresponsive
| 101 -> Watchman_error
| 102 -> Hash_table_full
| 103 -> Heap_full
| 110 -> Unknown_error
| _ -> raise Not_found
let error_type_opt i = (try Some (error_type i) with Not_found -> None)
let unpack_process_status = function
| Unix.WEXITED n -> ("exit", n)
| Unix.WSIGNALED n -> ("signaled", n)
| Unix.WSTOPPED n -> ("stopped", n)
let to_string = function
| Interrupted -> "Interrupted"
| No_error -> "Ok"
| Input_error -> "Input_error"
| Could_not_find_flowconfig -> "Could_not_find_flowconfig"
| Server_out_of_date -> "Server_out_of_date"
| Server_client_directory_mismatch -> "Server_client_directory_mismatch"
| Out_of_shared_memory -> "Out_of_shared_memory"
| Kill_error -> "Kill_error"
| Unused_server -> "Unused_server"
| No_server_running -> "No_server_running"
| Out_of_time -> "Out_of_time"
| Out_of_retries -> "Out_of_retries"
| Invalid_flowconfig -> "Invalid_flowconfig"
| Path_is_not_a_file -> "Path_is_not_a_file"
| Windows_killed_by_task_manager -> "Windows_killed_by_task_manager"
| Server_start_failed status ->
let (reason, code) = unpack_process_status status in
Utils_js.spf "Server_start_failed (%s, %d)" reason code
| Type_error -> "Type_error"
| Build_id_mismatch -> "Build_id_mismatch"
| Lock_stolen -> "Lock_stolen"
| Socket_error -> "Socket_error"
| Missing_flowlib -> "Missing_flowlib"
| Dfind_died -> "Dfind_died"
| Dfind_unresponsive -> "Dfind_unresponsive"
| Watchman_error -> "Watchman_error"
| Unknown_error -> "Unknown_error"
| Commandline_usage_error -> "Commandline_usage_error"
| No_input -> "No_input"
| Flowconfig_changed -> "Flowconfig_changed"
| Autostop -> "Autostop"
| Killed_by_monitor -> "Killed_by_monitor"
| Invalid_saved_state -> "Invalid_saved_state"
| Restart -> "Restart"
| Hash_table_full -> "Hash_table_full"
| Heap_full -> "Heap_full"
exception Exit_with of t
type json_mode = { pretty: bool }
let json_mode = ref None
let set_json_mode ~pretty = json_mode := Some { pretty }
let unset_json_mode () = json_mode := None
let json_props_of_t ?msg t =
Hh_json.(
let exit_props =
[("code", JSON_Number (error_code t |> string_of_int)); ("reason", JSON_String (to_string t))]
@ Base.Option.value_map msg ~default:[] ~f:(fun msg -> [("msg", JSON_String msg)])
in
[("flowVersion", JSON_String Flow_version.version); ("exit", JSON_Object exit_props)])
let print_json ?msg t =
match t with
| No_error
| Type_error ->
()
| _ ->
begin
match !json_mode with
| None -> ()
| Some { pretty } ->
let json = Hh_json.JSON_Object (json_props_of_t ?msg t) in
Hh_json.print_json_endline ~pretty json
end
let exit ?msg t =
(match msg with
| Some msg -> prerr_endline msg
| None -> ());
print_json ?msg t;
if FlowEventLogger.should_log () then FlowEventLogger.exit msg (to_string t);
Stdlib.exit (error_code t)