提交 042a8b67 编写于 作者: X xuyong

HiSysEvent napi接口文档增加异常相关的try-catch指导说明

Signed-off-by: Nxuyong <xuyong59@huawei.com>
上级 fc77c051
...@@ -60,20 +60,24 @@ Writes event information to the event file. This API uses an asynchronous callba ...@@ -60,20 +60,24 @@ Writes event information to the event file. This API uses an asynchronous callba
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({ try {
domain: "RELIABILITY", hiSysEvent.write({
name: "STACK", domain: "RELIABILITY",
eventType: hiSysEvent.EventType.FAULT, name: "STACK",
params: { eventType: hiSysEvent.EventType.FAULT,
PID: 487, params: {
UID: 103, PID: 487,
PACKAGE_NAME: "com.ohos.hisysevent.test", UID: 103,
PROCESS_NAME: "syseventservice", PACKAGE_NAME: "com.ohos.hisysevent.test",
MSG: "no msg." PROCESS_NAME: "syseventservice",
} MSG: "no msg."
}, (err, val) => { }
// do something here. }, (err, val) => {
}) // do something here.
})
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
}
``` ```
...@@ -102,26 +106,30 @@ Writes event information to the event file. This API uses a promise to return th ...@@ -102,26 +106,30 @@ Writes event information to the event file. This API uses a promise to return th
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({ try {
domain: "RELIABILITY", hiSysEvent.write({
name: "STACK", domain: "RELIABILITY",
eventType: hiSysEvent.EventType.FAULT, name: "STACK",
params: { eventType: hiSysEvent.EventType.FAULT,
PID: 487, params: {
UID: 103, PID: 487,
PACKAGE_NAME: "com.ohos.hisysevent.test", UID: 103,
PROCESS_NAME: "syseventservice", PACKAGE_NAME: "com.ohos.hisysevent.test",
MSG: "no msg." PROCESS_NAME: "syseventservice",
} MSG: "no msg."
}).then( }
(val) => { }).then(
// do something here. (val) => {
} // do something here.
).catch( }
(err) => { ).catch(
// do something here. (err) => {
} // do something here.
) }
)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
}
``` ```
## RuleType ## RuleType
...@@ -177,32 +185,30 @@ Adds a watcher for event subscription. ...@@ -177,32 +185,30 @@ Adds a watcher for event subscription.
| ------ | ----------------------------- | ---- | ------------------------ | | ------ | ----------------------------- | ---- | ------------------------ |
| watcher | [Watcher](#watcher) | Yes| Watcher for event subscription.| | watcher | [Watcher](#watcher) | Yes| Watcher for event subscription.|
**Return value**
| Type | Description|
| ------- | -------------------------------------------------- |
| number | Event subscription result.<br>- **0**: Event subscription is successful.<br>- A value smaller than **0**: Event subscription has failed.|
**Example** **Example**
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
let watcher = { let watcher = {
rules: [{ rules: [{
domain: "RELIABILITY", domain: "RELIABILITY",
name: "STACK", name: "STACK",
tag: "STABILITY", tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD, ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}], }],
onEvent: (info) => { onEvent: (info) => {
// do something here. // do something here.
}, },
onServiceDied: () => { onServiceDied: () => {
// do something here. // do something here.
} }
}
try {
hiSysEvent.addWatcher(watcher)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
} }
let ret = hiSysEvent.addWatcher(watcher)
``` ```
## hiSysEvent.removeWatcher ## hiSysEvent.removeWatcher
...@@ -221,33 +227,31 @@ Removes a watcher used for event subscription. ...@@ -221,33 +227,31 @@ Removes a watcher used for event subscription.
| ------ | ------------- | ---- | ------------------------ | | ------ | ------------- | ---- | ------------------------ |
| watcher | [Watcher](#watcher) | Yes| Watcher for event subscription.| | watcher | [Watcher](#watcher) | Yes| Watcher for event subscription.|
**Return value**
| Type | Description|
| ------- | ----------------------------------------------------------- |
| number | Result of removing the watcher.<br>- **0**: Removing the watcher is successful.<br>- A value smaller than **0**: Removing the watcher has failed.|
**Example** **Example**
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
let watcher = { let watcher = {
rules: [{ rules: [{
domain: "RELIABILITY", domain: "RELIABILITY",
name: "STACK", name: "STACK",
tag: "STABILITY", tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD, ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}], }],
onEvent: (info) => { onEvent: (info) => {
// do something here. // do something here.
}, },
onServiceDied: () => { onServiceDied: () => {
// do something here. // do something here.
} }
}
try {
hiSysEvent.addWatcher(watcher)
hiSysEvent.removeWatcher(watcher)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
} }
let ret = hiSysEvent.addWatcher(watcher)
hiSysEvent.removeWatcher(watcher)
``` ```
## QueryArg ## QueryArg
...@@ -281,7 +285,7 @@ Defines an event query instance. ...@@ -281,7 +285,7 @@ Defines an event query instance.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| onQuery | function | Yes| Callback of queried events: (infos: [SysEventInfo](#syseventinfo)[], seqs: number[]) => void| | onQuery | function | Yes| Callback of queried events: (infos: [SysEventInfo](#syseventinfo)[]) => void|
| onComplete | function | Yes| Callback of query result statistics: (reason: number, total: number) => void| | onComplete | function | Yes| Callback of query result statistics: (reason: number, total: number) => void|
## hiSysEvent.query ## hiSysEvent.query
...@@ -302,44 +306,42 @@ Queries system events. ...@@ -302,44 +306,42 @@ Queries system events.
| rules | [QueryRule](#queryrule)[] | Yes | Array of event query rules.| | rules | [QueryRule](#queryrule)[] | Yes | Array of event query rules.|
| querier | [Querier](#querier) | Yes | Event query instance.| | querier | [Querier](#querier) | Yes | Event query instance.|
**Return value**
| Type | Description |
| ------- | ----------------------------------------------------------- |
| number | Event query result.<br>- **0**: Event query is successful.<br>- A value smaller than **0**: Event query has failed.|
**Example** **Example**
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({ try {
domain: "RELIABILITY", hiSysEvent.write({
name: "STACK", domain: "RELIABILITY",
eventType: hiSysEvent.EventType.FAULT, name: "STACK",
params: { eventType: hiSysEvent.EventType.FAULT,
PID: 487, params: {
UID: 103, PID: 487,
PACKAGE_NAME: "com.ohos.hisysevent.test", UID: 103,
PROCESS_NAME: "syseventservice", PACKAGE_NAME: "com.ohos.hisysevent.test",
MSG: "no msg." PROCESS_NAME: "syseventservice",
} MSG: "no msg."
}, (err, val) => { }
// do something here. }, (err, val) => {
}) // do something here.
hiSysEvent.query({ })
beginTime: -1, hiSysEvent.query({
endTime: -1, beginTime: -1,
maxEvents: 5, endTime: -1,
}, [{ maxEvents: 5,
domain: "RELIABILITY", }, [{
names: ["STACK"], domain: "RELIABILITY",
}], { names: ["STACK"],
onQuery: function (infos, seqs) { }], {
// do something here. onQuery: function (infos) {
}, // do something here.
onComplete: function(reason, total) { },
// do something here. onComplete: function(reason, total) {
} // do something here.
}) }
})
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
}
``` ```
...@@ -60,20 +60,24 @@ write(info: SysEventInfo, callback: AsyncCallback&lt;void&gt;): void ...@@ -60,20 +60,24 @@ write(info: SysEventInfo, callback: AsyncCallback&lt;void&gt;): void
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({ try {
domain: "RELIABILITY", hiSysEvent.write({
name: "STACK", domain: "RELIABILITY",
eventType: hiSysEvent.EventType.FAULT, name: "STACK",
params: { eventType: hiSysEvent.EventType.FAULT,
PID: 487, params: {
UID: 103, PID: 487,
PACKAGE_NAME: "com.ohos.hisysevent.test", UID: 103,
PROCESS_NAME: "syseventservice", PACKAGE_NAME: "com.ohos.hisysevent.test",
MSG: "no msg." PROCESS_NAME: "syseventservice",
} MSG: "no msg."
}, (err, val) => { }
// do something here. }, (err, val) => {
}) // do something here.
})
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
}
``` ```
...@@ -102,26 +106,30 @@ write(info: SysEventInfo): Promise&lt;void&gt; ...@@ -102,26 +106,30 @@ write(info: SysEventInfo): Promise&lt;void&gt;
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({ try {
domain: "RELIABILITY", hiSysEvent.write({
name: "STACK", domain: "RELIABILITY",
eventType: hiSysEvent.EventType.FAULT, name: "STACK",
params: { eventType: hiSysEvent.EventType.FAULT,
PID: 487, params: {
UID: 103, PID: 487,
PACKAGE_NAME: "com.ohos.hisysevent.test", UID: 103,
PROCESS_NAME: "syseventservice", PACKAGE_NAME: "com.ohos.hisysevent.test",
MSG: "no msg." PROCESS_NAME: "syseventservice",
} MSG: "no msg."
}).then( }
(val) => { }).then(
// do something here. (val) => {
} // do something here.
).catch( }
(err) => { ).catch(
// do something here. (err) => {
} // do something here.
) }
)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
}
``` ```
## RuleType ## RuleType
...@@ -177,32 +185,30 @@ addWatcher(watcher: Watcher): number ...@@ -177,32 +185,30 @@ addWatcher(watcher: Watcher): number
| ------ | ----------------------------- | ---- | ------------------------ | | ------ | ----------------------------- | ---- | ------------------------ |
| watcher | [Watcher](#watcher) | 是 | 系统事件订阅者对象。 | | watcher | [Watcher](#watcher) | 是 | 系统事件订阅者对象。 |
**返回值:**
| 类型 | 说明 |
| ------- | -------------------------------------------------- |
| number | 系统事件订阅结果。<br>- 0表示订阅成功。<br>- 负值表示订阅失败。 |
**示例:** **示例:**
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
let watcher = { let watcher = {
rules: [{ rules: [{
domain: "RELIABILITY", domain: "RELIABILITY",
name: "STACK", name: "STACK",
tag: "STABILITY", tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD, ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}], }],
onEvent: (info) => { onEvent: (info) => {
// do something here. // do something here.
}, },
onServiceDied: () => { onServiceDied: () => {
// do something here. // do something here.
} }
}
try {
hiSysEvent.addWatcher(watcher)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
} }
let ret = hiSysEvent.addWatcher(watcher)
``` ```
## hiSysEvent.removeWatcher ## hiSysEvent.removeWatcher
...@@ -221,33 +227,31 @@ removeWatcher(watcher: Watcher): number ...@@ -221,33 +227,31 @@ removeWatcher(watcher: Watcher): number
| ------ | ------------- | ---- | ------------------------ | | ------ | ------------- | ---- | ------------------------ |
| watcher | [Watcher](#watcher) | 是 | 系统事件订阅者对象。 | | watcher | [Watcher](#watcher) | 是 | 系统事件订阅者对象。 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------------------------------------------------------- |
| number | 取消订阅系统事件的结果。<br>- 0表示取消订阅成功。<br>- 负值表示取消订阅失败。 |
**示例:** **示例:**
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
let watcher = { let watcher = {
rules: [{ rules: [{
domain: "RELIABILITY", domain: "RELIABILITY",
name: "STACK", name: "STACK",
tag: "STABILITY", tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD, ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}], }],
onEvent: (info) => { onEvent: (info) => {
// do something here. // do something here.
}, },
onServiceDied: () => { onServiceDied: () => {
// do something here. // do something here.
} }
}
try {
let ret = hiSysEvent.addWatcher(watcher)
hiSysEvent.removeWatcher(watcher)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
} }
let ret = hiSysEvent.addWatcher(watcher)
hiSysEvent.removeWatcher(watcher)
``` ```
## QueryArg ## QueryArg
...@@ -281,7 +285,7 @@ hiSysEvent.removeWatcher(watcher) ...@@ -281,7 +285,7 @@ hiSysEvent.removeWatcher(watcher)
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| onQuery | function | 是 | 返回查询到的系统事件的回调方法(infos: [SysEventInfo](#syseventinfo)[], seqs: number[]) => void。 | | onQuery | function | 是 | 返回查询到的系统事件的回调方法(infos: [SysEventInfo](#syseventinfo)[]) => void。 |
| onComplete | function | 是 | 查询结果统计的回调方法(reason: number, total: number) => void。 | | onComplete | function | 是 | 查询结果统计的回调方法(reason: number, total: number) => void。 |
## hiSysEvent.query ## hiSysEvent.query
...@@ -302,44 +306,42 @@ query(queryArg: QueryArg, rules: QueryRule[], querier: Querier): number ...@@ -302,44 +306,42 @@ query(queryArg: QueryArg, rules: QueryRule[], querier: Querier): number
| rules | [QueryRule](#queryrule)[] | 是 | 查询规则数组,每次查询可配置多个查询规则。 | | rules | [QueryRule](#queryrule)[] | 是 | 查询规则数组,每次查询可配置多个查询规则。 |
| querier | [Querier](#querier) | 是 | 查询者对象,包含查询结果及结束的相关回调。 | | querier | [Querier](#querier) | 是 | 查询者对象,包含查询结果及结束的相关回调。 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------------------------------------------------------- |
| number | 系统事件查询的结果。<br>- 0表示查询成功.<br>- 负值表示查询失败。 |
**示例:** **示例:**
```js ```js
import hiSysEvent from '@ohos.hiSysEvent'; import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({ try {
domain: "RELIABILITY", hiSysEvent.write({
name: "STACK", domain: "RELIABILITY",
eventType: hiSysEvent.EventType.FAULT, name: "STACK",
params: { eventType: hiSysEvent.EventType.FAULT,
PID: 487, params: {
UID: 103, PID: 487,
PACKAGE_NAME: "com.ohos.hisysevent.test", UID: 103,
PROCESS_NAME: "syseventservice", PACKAGE_NAME: "com.ohos.hisysevent.test",
MSG: "no msg." PROCESS_NAME: "syseventservice",
} MSG: "no msg."
}, (err, val) => { }
// do something here. }, (err, val) => {
}) // do something here.
hiSysEvent.query({ })
beginTime: -1, hiSysEvent.query({
endTime: -1, beginTime: -1,
maxEvents: 5, endTime: -1,
}, [{ maxEvents: 5,
domain: "RELIABILITY", }, [{
names: ["STACK"], domain: "RELIABILITY",
}], { names: ["STACK"],
onQuery: function (infos, seqs) { }], {
// do something here. onQuery: function (infos) {
}, // do something here.
onComplete: function(reason, total) { },
// do something here. onComplete: function(reason, total) {
} // do something here.
}) }
})
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
}
``` ```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册