{"id":3904,"date":"2022-12-01T09:14:33","date_gmt":"2022-12-01T09:14:33","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=3904"},"modified":"2023-01-25T10:21:21","modified_gmt":"2023-01-25T10:21:21","slug":"safe_mode","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=3904","title":{"rendered":"Safe_mode"},"content":{"rendered":"\n<p>Safe_mode is used precautiously in order to prevent the transformation from breaking. More specifically, the function changes the behavior of a potential exception within the transformation block so it returns <code>null<\/code> instead of breaking the entire transformation.<\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">safe_mode($input, &amp;expression)<\/code><\/pre>\n\n\n\n<p>Evaluates $input against an &amp;expression. In case of an exception, it changes the exception&#8217;s behavior to result in <code>null<\/code> instead of an invalid transformation and treats error messages as warnings.<\/p>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$input:<\/strong> The key, object, or array that is used as data for the expression.<br><strong>&amp;expression: <\/strong>The expression that is used for the evaluation.<\/p>\n\n\n\n<h3>Example<\/h3>\n\n\n\n<p>In the dataset below two key-value pairs are presented. It is worth noticing how one of them has a string value while the other one has a number value.<\/p>\n\n\n\n<pre title=\"Data\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"a\": \"none\", \/\/string value\n    \"b\": 89  \/\/number value\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-full is-resized\"><img loading=\"lazy\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/11\/error-1.png\" alt=\"\" class=\"wp-image-3932\" width=\"279\" height=\"66\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/11\/error-1.png 439w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/11\/error-1-300x71.png 300w\" sizes=\"(max-width: 279px) 100vw, 279px\" \/><\/figure><\/div>\n\n\n\n<p class=\"has-text-align-left\">In the transformation below, the sum function that is used to calculate the sum of the two key-value pairs throws an exception as it doesn&#8217;t support string values. Thus, it returns <code>null<\/code> and an invalid transformation error.<\/p>\n\n\n\n<pre title=\"Transformation without safe_mode\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    SUM:sum([a,b])\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-full is-resized\"><img loading=\"lazy\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/11\/warning.png\" alt=\"\" class=\"wp-image-3941\" width=\"283\" height=\"68\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/11\/warning.png 438w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/11\/warning-300x72.png 300w\" sizes=\"(max-width: 283px) 100vw, 283px\" \/><\/figure><\/div>\n\n\n\n<p>If the safe_mode function is used in the above-mentioned key (as seen in the transformation below), then, even though it will still return null, instead of an invalid transformation error, a warning message is generated. This is achieved, because of the safe_mode function.<\/p>\n\n\n\n<pre title=\"Transformation with safe_mode\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    SUM:safe_mode(@, &amp;sum([a,b]))\n}<\/code><\/pre>\n\n\n\n<p>In this example, both cases have the same result which is presented in the code block below:<\/p>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"SUM\": null  \/\/returns null since sum doesn't support string values\n}<\/code><\/pre>\n\n\n\n<p> <\/p>\n\n\n\n<h3>Notes<\/h3>\n\n\n\n<p>While the above example is quite simple, it clearly demonstrates the use of the safe_mode function, even though both of the above transformations have the same result. Usually, safe_mode is used in combination with <a href=\"https:\/\/info.documotor.com\/?page_id=1372\" data-type=\"page\" data-id=\"1372\">User Functions<\/a> or when a <a href=\"https:\/\/info.documotor.com\/?page_id=2169\" data-type=\"page\" data-id=\"2169\">Connected Service<\/a> is called using the service function, as these may throw an exception that is not visible or controllable within the transformation block and which could prevent DocuMotor from generating any output. For example, it can be something that breaks in a User Function&#8217;s body or an authentication error related to a connected service. By using safe_mode in such cases (in the same way as demonstrated above), it works like a safety net that ensures that an output will be generated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Safe_mode is used precautiously in order to prevent the transformation from breaking. More specifically, the function changes the behavior of a potential exception within the transformation block so it returns null instead of breaking the entire transformation. Syntax Evaluates $input against an &amp;expression. In case of an exception, it changes the exception&#8217;s behavior to result [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"parent":271,"menu_order":180,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3904"}],"collection":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3904"}],"version-history":[{"count":32,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3904\/revisions"}],"predecessor-version":[{"id":4261,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3904\/revisions\/4261"}],"up":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/271"}],"wp:attachment":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}