From 1a6b67d361849d8cbc605c4ee89254df255e93f1 Mon Sep 17 00:00:00 2001 From: Dmitry Bushev Date: Fri, 14 May 2021 16:22:51 +0300 Subject: [PATCH] Add a `.to_json` conversion for `Error` (#1742) --- .../Standard/src/Base/Error/Extensions.enso | 18 +++++++++++++++++- test/Tests/src/Semantic/Error_Spec.enso | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/distribution/std-lib/Standard/src/Base/Error/Extensions.enso b/distribution/std-lib/Standard/src/Base/Error/Extensions.enso index 259f39f1aa..4e36df71de 100644 --- a/distribution/std-lib/Standard/src/Base/Error/Extensions.enso +++ b/distribution/std-lib/Standard/src/Base/Error/Extensions.enso @@ -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 - diff --git a/test/Tests/src/Semantic/Error_Spec.enso b/test/Tests/src/Semantic/Error_Spec.enso index ad32907731..41d1fd4455 100644 --- a/test/Tests/src/Semantic/Error_Spec.enso +++ b/test/Tests/src/Semantic/Error_Spec.enso @@ -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" <|