diff --git a/docs/reference/model/overview.md b/docs/reference/model/overview.md index 3a88c9695..91a52415e 100644 --- a/docs/reference/model/overview.md +++ b/docs/reference/model/overview.md @@ -57,4 +57,7 @@ For relatively complex general logic, it is provided through the system modules.

Provides template functions.

+ +

Provides runtime functions.

+
diff --git a/docs/reference/model/runtime.md b/docs/reference/model/runtime.md new file mode 100644 index 000000000..b65ed4b6f --- /dev/null +++ b/docs/reference/model/runtime.md @@ -0,0 +1,30 @@ +--- +title: "runtime" +linkTitle: "runtime" +type: "docs" +description: runtime functions +weight: 100 +--- + +## catch + +`catch(func: () -> any) -> str` + +Executes the provided function and catches any potential runtime errors. Returns undefined if execution is successful, otherwise returns an error message in case of a runtime panic. + +```python3 +import runtime + +schema Person: + name: str + age: int + + check: + 0 <= age <= 120, "age must be in [1, 120], got ${age}" + +test_person_check_error = lambda { + assert runtime.catch(lambda { + p = Person {name = "Alice", age: -1} + }) == "age must be in [1, 120], got -1" +} +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/model/overview.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/model/overview.md index 4f4dd7bc0..129802224 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/model/overview.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/model/overview.md @@ -57,4 +57,7 @@ KCL 通过内置模块、系统库模块和插件模块提供工程化的扩展

提供了与模版相关的函数。

+ +

提供了与运行时相关的函数。

+
diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/model/runtime.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/model/runtime.md new file mode 100644 index 000000000..e24bcc30b --- /dev/null +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/model/runtime.md @@ -0,0 +1,30 @@ +--- +title: "runtime" +linkTitle: "runtime" +type: "docs" +description: 运行时函数 +weight: 100 +--- + +## catch + +`catch(func: () -> any) -> str` + +`catch` 函数可以执行代码块并捕获任何潜在的运行时错误。如果代码块中没有发生异常,`catch` 函数返回 `Undefined`,否则返回异常信息。 + +```python3 +import runtime + +schema Person: + name: str + age: int + + check: + 0 <= age <= 120, "age must be in [1, 120], got ${age}" + +test_person_check_error = lambda { + assert runtime.catch(lambda { + p = Person {name = "Alice", age: -1} + }) == "age must be in [1, 120], got -1" +} +```