Skip to content

docs: add runtime module functions #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/reference/model/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ For relatively complex general logic, it is provided through the system modules.
<DocsCard header="template" href="template">
<p>Provides template functions.</p>
</DocsCard>
<DocsCard header="runtime" href="runtime">
<p>Provides runtime functions.</p>
</DocsCard>
</DocsCards>
30 changes: 30 additions & 0 deletions docs/reference/model/runtime.md
Original file line number Diff line number Diff line change
@@ -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"
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ KCL 通过内置模块、系统库模块和插件模块提供工程化的扩展
<DocsCard header="template" href="template">
<p>提供了与模版相关的函数。</p>
</DocsCard>
<DocsCard header="runtime" href="runtime">
<p>提供了与运行时相关的函数。</p>
</DocsCard>
</DocsCards>
Original file line number Diff line number Diff line change
@@ -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"
}
```
Loading