File: program.ml

package info (click to toggle)
approx 5.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 340 kB
  • sloc: ml: 2,220; sh: 42; makefile: 32
file content (46 lines) | stat: -rw-r--r-- 1,373 bytes parent folder | download | duplicates (6)
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
(* approx: proxy server for Debian archive files
   Copyright (C) 2011  Eric C. Cooper <ecc@cmu.edu>
   Released under the GNU General Public License *)

open Printf
open Util
open Log

let string_of_uerror = function
  | err, str, "" -> sprintf "%s: %s" str (Unix.error_message err)
  | err, str, arg -> sprintf "%s: %s (%s)" str (Unix.error_message err) arg

let string_of_exception exc =
  match exc with
  | Failure str -> "Failure: " ^ str
  | Invalid_argument str -> "Invalid argument: " ^ str
  | Sys_error str -> str
  | Unix.Unix_error (err, str, arg)-> string_of_uerror (err, str, arg)
  | Control_file.Missing (par, field) ->
      sprintf "File %s, line %d: missing \"%s\" field"
        (Control_file.file_name par)
        (Control_file.line_number par)
        (String.capitalize field)
  | e -> Printexc.to_string e

let perform f x =
  try f x
  with e -> error_message "%s" (string_of_exception e)

let backtrace () =
  let bt = Printexc.get_backtrace () in
  if bt <> "" then
    let lines = split_lines bt in
    error_message "%s" "Uncaught exception";
    List.iter (fun s ->  if s <> "" then error_message "  %s" s) lines

let main_program f x =
  try f x
  with e ->
    backtrace ();
    error_message "%s" (string_of_exception e);
    exit 1

let print fmt = error_message fmt

let file_message file msg = print "%s: %s" (Config.shorten file) msg