File: error.ml

package info (click to toggle)
ben 1.14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 672 kB
  • sloc: ml: 4,116; sh: 345; javascript: 78; ansic: 39; makefile: 29; python: 18
file content (82 lines) | stat: -rw-r--r-- 4,043 bytes parent folder | download | duplicates (2)
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
(**************************************************************************)
(*  Copyright © 2009-2013 Stéphane Glondu <steph@glondu.net>              *)
(*            © 2010-2013 Mehdi Dogguy <mehdi@dogguy.org>                 *)
(*                                                                        *)
(*  This program is free software: you can redistribute it and/or modify  *)
(*  it under the terms of the GNU Affero General Public License as        *)
(*  published by the Free Software Foundation, either version 3 of the    *)
(*  License, or (at your option) any later version, with the additional   *)
(*  exemption that compiling, linking, and/or using OpenSSL is allowed.   *)
(*                                                                        *)
(*  This program is distributed in the hope that it will be useful, but   *)
(*  WITHOUT ANY WARRANTY; without even the implied warranty of            *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *)
(*  Affero General Public License for more details.                       *)
(*                                                                        *)
(*  You should have received a copy of the GNU Affero General Public      *)
(*  License along with this program.  If not, see                         *)
(*  <http://www.gnu.org/licenses/>.                                       *)
(**************************************************************************)

open Printf

type error =
  | Illegal_escape of char
  | Unknown_error of exn
  | Nothing_to_download
  | Curl_error of int
  | Unexpected_char of string * char * int * int
  | Bad_marshalled_data of string
  | Unknown_command of string
  | Unknown_output_format of string
  | Unknown_input_format of string
  | Unexpected_expression of string
  | Missing_configuration_file
  | Error_in_configuration_file of string
  | Missing_configuration_item of string
  | Unknown_configuration_item of string
  | Parsing_error of string * bool * int * int
  | Template_not_found of string
  | Dynlink_error of Dynlink.error

exception Error of error

let string_of_error = function
  | Illegal_escape c -> sprintf "illegal escape of %C" c
  | Unknown_error e -> sprintf "unexpected error: %s" (Printexc.to_string e)
  | Curl_error r -> sprintf "curl exited with return code %d" r
  | Nothing_to_download -> sprintf "nothing to download"
  | Unexpected_char (file, c, line, column) ->
      sprintf "unexpected char %C in file %S, line %d, position %d" c file line
        column
  | Bad_marshalled_data s -> sprintf "bad marshalled data in %s" s
  | Unknown_command s -> sprintf "unknown command: %s" s
  | Unknown_output_format s -> sprintf "unknown output format: %s" s
  | Unknown_input_format s -> sprintf "unknown input format: %s" s
  | Unexpected_expression s -> sprintf "unexpected expression: %s" s
  | Missing_configuration_file ->
      sprintf "No configuration file has been specified"
  | Error_in_configuration_file s -> sprintf "error in configuration file: %s" s
  | Missing_configuration_item s -> sprintf "missing configuration item: %s" s
  | Unknown_configuration_item s ->
      sprintf "unknown configuration item or wrong syntax for %s" s
  | Parsing_error (string, is_file, line, column) ->
      if is_file then
        sprintf "parse error in file %S, line %d, character %d" string line
          column
      else sprintf "parse error in %S, line %d, character %d" string line column
  | Template_not_found name -> sprintf "template %s not found" name
  | Dynlink_error e -> sprintf "Dynlink error: %s" (Dynlink.error_message e)

let () =
  Printexc.register_printer (function
    | Error exn -> Some ("ben-specific error: " ^ string_of_error exn)
    | _ -> None)

let raise e = Stdlib.raise (Error e)
let warn e = Printf.eprintf "W: %s\n%!" (string_of_error e)
let warn_exn msg e = Printf.eprintf "W: %s: %s\n%!" msg (Printexc.to_string e)

let error_exn msg e =
  Printf.eprintf "E: %s: %s\n" msg (Printexc.to_string e);
  Printexc.print_backtrace stderr