aboutsummaryrefslogtreecommitdiff
path: root/lib/errors
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2024-03-14 08:26:58 +0100
committerSébastien Dailly <sebastien@dailly.me>2024-03-14 08:26:58 +0100
commit6b377719c10d5ab3343fd5221f99a4a21008e25a (patch)
treea7c1e9a820d339a2f161af3e09cf9e3161286796 /lib/errors
Initial commitmain
Diffstat (limited to 'lib/errors')
-rw-r--r--lib/errors/dune9
-rw-r--r--lib/errors/importErrors.ml98
-rw-r--r--lib/errors/importErrors.mli46
3 files changed, 153 insertions, 0 deletions
diff --git a/lib/errors/dune b/lib/errors/dune
new file mode 100644
index 0000000..ab71219
--- /dev/null
+++ b/lib/errors/dune
@@ -0,0 +1,9 @@
+(library
+ (name importErrors)
+ (libraries
+ csv
+ sqlite3
+ importCSV
+ importDataTypes
+ )
+)
diff --git a/lib/errors/importErrors.ml b/lib/errors/importErrors.ml
new file mode 100644
index 0000000..04f9deb
--- /dev/null
+++ b/lib/errors/importErrors.ml
@@ -0,0 +1,98 @@
+open StdLabels
+module CSV = ImportCSV
+module Table = ImportDataTypes.Table
+
+let bom = "\xEF\xBB\xBF"
+
+type xlsError = {
+ source : Table.t;
+ row : int;
+ sheet : int;
+ target : Table.t option;
+ value : CSV.DataType.t;
+ exn : exn;
+}
+
+exception
+ JsonError of {
+ json : string;
+ element : string;
+ }
+
+exception InvalidEncoding of string
+exception NullKey of int
+exception SqlError of Sqlite3.Rc.t
+exception MisplacedWindow
+exception Unknown_source of string
+exception Unknown_extension of string
+
+exception Cycle of string list
+(** Cycle between the dpendencies *)
+
+exception
+ TypeError of {
+ expected : ImportDataTypes.Types.t;
+ actual : ImportDataTypes.Types.t;
+ expression : string;
+ subset : string;
+ }
+
+let repr_error = function
+ | SqlError s -> Printf.sprintf "%s Error" (Sqlite3.Rc.to_string s)
+ | JsonError { json; element } -> Printf.sprintf "%s : %s" element json
+ | NullKey k ->
+ Printf.sprintf "The key %s is null" (ImportCSV.Csv.column_to_string k)
+ | Unknown_source source ->
+ Printf.sprintf "The source %s is referenced without beiing declared"
+ source
+ | MisplacedWindow -> "A group function cannot appear here"
+ | TypeError { expected; actual; expression; subset } ->
+ Printf.sprintf
+ "In this expression %s has type %s but %s was expected:\n%s" subset
+ (ImportDataTypes.Types.string_of_t actual)
+ (ImportDataTypes.Types.string_of_t expected)
+ expression
+ | Unknown_extension ext -> Printf.sprintf "Unknown file extension %s" ext
+ | Cycle deps ->
+ Printf.sprintf "Cycle between the dependencies : %s"
+ (String.concat ~sep:"," deps)
+ | Failure e -> e
+ | e ->
+ Printexc.print_backtrace Stdlib.stdout;
+ Printexc.to_string e
+
+type t = Csv.out_channel Lazy.t
+
+let output_error : t -> xlsError -> unit =
+ fun csv error ->
+ let target =
+ match error.target with
+ | None -> ""
+ | Some value -> value.name
+ in
+
+ Csv.output_record (Lazy.force csv)
+ [
+ Table.name error.source;
+ string_of_int error.sheet;
+ string_of_int error.row;
+ target;
+ CSV.DataType.to_string error.value;
+ repr_error error.exn;
+ ]
+
+let log : with_bom:bool -> string -> string -> t =
+ fun ~with_bom prefix dirname ->
+ lazy
+ (let file = Filename.concat dirname (String.cat prefix "_errors.csv") in
+
+ prerr_endline @@ String.cat "Errors found. See the file " file;
+
+ (* Open the error file *)
+ let channel = Out_channel.open_bin file in
+ if with_bom then output_string channel bom;
+ let log_error = Csv.to_channel ~separator:';' ~excel_tricks:true channel in
+ (* Generate a header for the error file *)
+ Csv.output_record log_error
+ [ "Source"; "Tab"; "Row"; "Target"; "Value"; "Error" ];
+ log_error)
diff --git a/lib/errors/importErrors.mli b/lib/errors/importErrors.mli
new file mode 100644
index 0000000..7d17a5d
--- /dev/null
+++ b/lib/errors/importErrors.mli
@@ -0,0 +1,46 @@
+module CSV = ImportCSV
+
+exception SqlError of Sqlite3.Rc.t
+exception MisplacedWindow
+
+exception Unknown_source of string
+(** Exception raised when a source used in a path is not declared as an
+ external *)
+
+exception Cycle of string list
+(** Cycle between the dpendencies *)
+
+exception Unknown_extension of string
+
+exception
+ JsonError of {
+ json : string;
+ element : string;
+ }
+
+type xlsError = {
+ source : ImportDataTypes.Table.t;
+ row : int;
+ sheet : int;
+ target : ImportDataTypes.Table.t option;
+ value : CSV.DataType.t;
+ exn : exn;
+}
+
+exception
+ TypeError of {
+ expected : ImportDataTypes.Types.t;
+ actual : ImportDataTypes.Types.t;
+ expression : string;
+ subset : string;
+ }
+
+exception InvalidEncoding of string
+exception NullKey of int
+
+val repr_error : exn -> string
+
+type t = Csv.out_channel Lazy.t
+
+val output_error : t -> xlsError -> unit
+val log : with_bom:bool -> string -> string -> t