Skip to content

Commit 25109c6

Browse files
committed
docs: add runtime module functions
Signed-off-by: peefy <[email protected]>
1 parent 5d3919f commit 25109c6

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

docs/reference/model/overview.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ For relatively complex general logic, it is provided through the system modules.
5757
<DocsCard header="template" href="template">
5858
<p>Provides template functions.</p>
5959
</DocsCard>
60+
<DocsCard header="runtime" href="runtime">
61+
<p>Provides runtime functions.</p>
62+
</DocsCard>
6063
</DocsCards>

docs/reference/model/runtime.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: "runtime"
3+
linkTitle: "runtime"
4+
type: "docs"
5+
description: runtime functions
6+
weight: 100
7+
---
8+
9+
## catch
10+
11+
`catch(func: () -> any) -> str`
12+
13+
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.
14+
15+
```python3
16+
import runtime
17+
18+
schema Person:
19+
name: str
20+
age: int
21+
22+
check:
23+
0 <= age <= 120, "age must be in [1, 120], got ${age}"
24+
25+
test_person_check_error = lambda {
26+
assert runtime.catch(lambda {
27+
p = Person {name = "Alice", age: -1}
28+
}) == "age must be in [1, 120], got -1"
29+
}
30+
```

i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/model/overview.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ KCL 通过内置模块、系统库模块和插件模块提供工程化的扩展
5757
<DocsCard header="template" href="template">
5858
<p>提供了与模版相关的函数。</p>
5959
</DocsCard>
60+
<DocsCard header="runtime" href="runtime">
61+
<p>提供了与运行时相关的函数。</p>
62+
</DocsCard>
6063
</DocsCards>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: "runtime"
3+
linkTitle: "runtime"
4+
type: "docs"
5+
description: 运行时函数
6+
weight: 100
7+
---
8+
9+
## catch
10+
11+
`catch(func: () -> any) -> str`
12+
13+
`catch` 函数可以执行代码块并捕获任何潜在的运行时错误。如果代码块中没有发生异常,`catch` 函数返回 `Undefined`,否则返回异常信息。
14+
15+
```python3
16+
import runtime
17+
18+
schema Person:
19+
name: str
20+
age: int
21+
22+
check:
23+
0 <= age <= 120, "age must be in [1, 120], got ${age}"
24+
25+
test_person_check_error = lambda {
26+
assert runtime.catch(lambda {
27+
p = Person {name = "Alice", age: -1}
28+
}) == "age must be in [1, 120], got -1"
29+
}
30+
```

0 commit comments

Comments
 (0)