Add a .to_json conversion for Error (#1742)

This commit is contained in:
Dmitry Bushev 2021-05-14 16:22:51 +03:00 committed by GitHub
parent 48bcebc723
commit 1a6b67d361
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -84,6 +84,23 @@ Error.to_default_visualization_data = this.catch .to_default_visualization_data
Error.to_display_text : Text
Error.to_display_text = "Error: " + (this.catch .to_display_text)
## UNSTABLE
Returns a JSON representation of the dataflow error.
> Example
Converting a dataflow error to JSON.
import Standard.Examples
example_to_json = Examples.throw_error.to_json
Error.to_json : Json.Object
Error.to_json =
error_type = ["type", "Error"]
error_content = ["content", this.catch .to_json]
error_message = ["message", this.catch .to_display_text]
Json.from_pairs [error_type, error_content, error_message]
## Transforms an error.
Arguments:
@ -126,4 +143,3 @@ Error.is_error = True
example_rethrow = Panic.rethrow Examples.throw_error
Panic.rethrow : (Any ! Any) -> Any
Panic.rethrow value = value.catch Panic.throw

View File

@ -23,6 +23,21 @@ spec =
Test.specify "should be able to be shown in the default visualization" <|
json = (Error.throw <| My_Type "aaa").to_default_visualization_data
json . should_equal <| (Json.from_pairs [["foo", "aaa"], ["type", "My_Type"]]).to_text
Test.specify "should be able to be shown in the default vector visualization" <|
vec = [My_Type "bar", Error.throw (My_Type 42)]
visualization_text = vec.to_default_visualization_data
expected_json = Json.parse '''
[
{ "foo":"bar",
"type":"My_Type"
},
{ "content":{ "foo":42, "type":"My_Type" },
"message":"My_Type",
"type":"Error"
}
]
visualization_text.should_equal expected_json.to_text
Test.specify "should implement to_display_text" <|
Error.throw Nothing . to_display_text . should_equal "Error: Nothing"
Test.specify "should be able to be mapped" <|