How can I write a custom contract that adds a note to another contract in case of error? #2310
Replies: 2 comments 2 replies
-
|
The issue here is that record contracts are lazy, and so actually your call to That is, the record contract's check just merges the field contracts into the record. It's only later, when the As for how to enable this use-case, we might need to add something to the standard library. We have |
Beta Was this translation helpful? Give feedback.
-
|
I've read a bit more about delayed checks and came up with this: let WithMsg = fun msg Contract =>
std.contract.custom (fun label value =>
let new_label = std.contract.label.append_note msg label in
let delayed = std.contract.apply Contract new_label value in
'Ok delayed
)
in
# ...It still doesn't work tho. It's weird because my understanding is that this is not run immediately, so it should work with records, and it mimics what the documentation does when explaining delayed checks. I've tried applying this contract to the most inner field (the one that is not provided by the user) and also to the record itself: {
a_record | WithMsg "some error" {
a_field | WithMsg "another_error" String,
another_field | WithMsg "yet_another_error" String
}
}The error message does not have the additional note in all of these cases. Note that I'm using Nickel to check a TOML file using the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to write a Nickel contract that adds a custom error note to whatever contract it is applied to. I've come up with this:
The problem is that this is very inconsistent. It works if I provide a field with the wrong type (for example,
some_field | WithMsg "custom error" Number, and I providesome_field = "a string"). When applied to record contracts, it doesn't work. For example:In this case, if I fail to provide
a_field, an error is raised (as expected) but it doesn't seem to trigger myWithMsgcontract.Is there a way to provided custom error messages to any contracts?
Beta Was this translation helpful? Give feedback.
All reactions