Skip to content

Commit

Permalink
add errors meta element to "catch" errors and attach them to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
eWert-Online committed Apr 17, 2024
1 parent df1cfea commit e58dfb4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/pinc_backend/Helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ module TagMeta = struct
let record r : meta = `Record r
let children () : meta = `SubTagPlaceholder
let template () : meta = `TemplatePlaceholder
let errors ?(list = []) () : meta = `Errors list

let rec merge (a : meta) (b : meta) =
match (a, b) with
Expand Down
56 changes: 41 additions & 15 deletions lib/pinc_backend/Tag.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ module Utils = struct
| None -> value
| Some transformer -> aux transformer |> State.get_output
;;

let attach_or_report meta loc err =
(* TODO: Get a list of errors here and replace the `Errors meta with a `ErrorsPlaceholder meta *)
let attached = ref false in

let meta =
meta
|> Option.map
@@ Helpers.TagMeta.map
@@ function
| `Errors lst when not !attached ->
attached := true;
`Errors (err :: lst)
| v -> v
in

if not !attached then
Pinc_Diagnostics.error loc err;

meta
;;
end

module Tag_String = struct
Expand Down Expand Up @@ -532,24 +553,29 @@ module Tag_Slot = struct
(key |> List.rev |> List.hd))
in

let () =
validate_element_count ~attributes slotted_elements
|> Result.iter_error (Pinc_Diagnostics.error tag_value.tag_loc)
let meta =
match validate_element_count ~attributes slotted_elements with
| Ok () -> meta
| Error e -> Utils.attach_or_report meta tag_value.tag_loc e
in

let () =
let meta =
slotted_elements
|> Array.iter @@ function
| { value_desc = HtmlTemplateNode (tag_name, _, _, _); value_loc }
| { value_desc = ComponentTemplateNode (tag_name, _, _); value_loc } ->
check_instance_restriction ~constraints tag_name
|> Result.iter_error (Pinc_Diagnostics.error value_loc)
| { value_desc = _; value_loc } ->
Pinc_Diagnostics.error
value_loc
"Tried to assign a non node value to a #Slot. Only template nodes are \
allowed inside slots. If you want to put another value (like a string) \
into a slot, you have to wrap it in some html tag or component."
|> Array.fold_left
(fun meta -> function
| { value_desc = HtmlTemplateNode (tag_name, _, _, _); value_loc }
| { value_desc = ComponentTemplateNode (tag_name, _, _); value_loc } -> (
match check_instance_restriction ~constraints tag_name with
| Ok () -> meta
| Error e -> Utils.attach_or_report meta value_loc e)
| { value_desc = _; value_loc } ->
Pinc_Diagnostics.error
value_loc
"Tried to assign a non node value to a #Slot. Only template nodes are \
allowed inside slots. If you want to put another value (like a \
string) into a slot, you have to wrap it in some html tag or \
component.")
meta
in

let output = slotted_elements |> Helpers.Value.array ~loc:tag_value.tag_loc in
Expand Down
1 change: 1 addition & 0 deletions lib/pinc_backend/Types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ and Type_Tag : sig
| `Record of (string * meta) list
| `SubTagPlaceholder
| `TemplatePlaceholder
| `Errors of string list
]

type data_provider =
Expand Down

0 comments on commit e58dfb4

Please sign in to comment.