提交 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
```js
import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (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
```js
import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}).then(
(val) => {
// do something here.
}
).catch(
(err) => {
// do something here.
}
)
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}).then(
(val) => {
// do something here.
}
).catch(
(err) => {
// do something here.
}
)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
}
```
## RuleType
......@@ -177,32 +185,30 @@ Adds a 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**
```js
import hiSysEvent from '@ohos.hiSysEvent';
let watcher = {
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// do something here.
}
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// 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
......@@ -221,33 +227,31 @@ Removes a watcher used 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**
```js
import hiSysEvent from '@ohos.hiSysEvent';
let watcher = {
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// do something here.
}
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// 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
......@@ -281,7 +285,7 @@ Defines an event query instance.
| 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|
## hiSysEvent.query
......@@ -302,44 +306,42 @@ Queries system events.
| rules | [QueryRule](#queryrule)[] | Yes | Array of event query rules.|
| 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**
```js
import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
hiSysEvent.query({
beginTime: -1,
endTime: -1,
maxEvents: 5,
}, [{
domain: "RELIABILITY",
names: ["STACK"],
}], {
onQuery: function (infos, seqs) {
// do something here.
},
onComplete: function(reason, total) {
// do something here.
}
})
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
hiSysEvent.query({
beginTime: -1,
endTime: -1,
maxEvents: 5,
}, [{
domain: "RELIABILITY",
names: ["STACK"],
}], {
onQuery: function (infos) {
// 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
```js
import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (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;
```js
import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}).then(
(val) => {
// do something here.
}
).catch(
(err) => {
// do something here.
}
)
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}).then(
(val) => {
// do something here.
}
).catch(
(err) => {
// do something here.
}
)
} catch (error) {
console.error(`error code: ${error.code}, error msg: ${error.message}`);
}
```
## RuleType
......@@ -177,32 +185,30 @@ addWatcher(watcher: Watcher): number
| ------ | ----------------------------- | ---- | ------------------------ |
| watcher | [Watcher](#watcher) | 是 | 系统事件订阅者对象。 |
**返回值:**
| 类型 | 说明 |
| ------- | -------------------------------------------------- |
| number | 系统事件订阅结果。<br>- 0表示订阅成功。<br>- 负值表示订阅失败。 |
**示例:**
```js
import hiSysEvent from '@ohos.hiSysEvent';
let watcher = {
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// do something here.
}
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// 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
......@@ -221,33 +227,31 @@ removeWatcher(watcher: Watcher): number
| ------ | ------------- | ---- | ------------------------ |
| watcher | [Watcher](#watcher) | 是 | 系统事件订阅者对象。 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------------------------------------------------------- |
| number | 取消订阅系统事件的结果。<br>- 0表示取消订阅成功。<br>- 负值表示取消订阅失败。 |
**示例:**
```js
import hiSysEvent from '@ohos.hiSysEvent';
let watcher = {
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// do something here.
}
rules: [{
domain: "RELIABILITY",
name: "STACK",
tag: "STABILITY",
ruleType: hiSysEvent.RuleType.WHOLE_WORD,
}],
onEvent: (info) => {
// do something here.
},
onServiceDied: () => {
// 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
......@@ -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。 |
## hiSysEvent.query
......@@ -302,44 +306,42 @@ query(queryArg: QueryArg, rules: QueryRule[], querier: Querier): number
| rules | [QueryRule](#queryrule)[] | 是 | 查询规则数组,每次查询可配置多个查询规则。 |
| querier | [Querier](#querier) | 是 | 查询者对象,包含查询结果及结束的相关回调。 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------------------------------------------------------- |
| number | 系统事件查询的结果。<br>- 0表示查询成功.<br>- 负值表示查询失败。 |
**示例:**
```js
import hiSysEvent from '@ohos.hiSysEvent';
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
hiSysEvent.query({
beginTime: -1,
endTime: -1,
maxEvents: 5,
}, [{
domain: "RELIABILITY",
names: ["STACK"],
}], {
onQuery: function (infos, seqs) {
// do something here.
},
onComplete: function(reason, total) {
// do something here.
}
})
try {
hiSysEvent.write({
domain: "RELIABILITY",
name: "STACK",
eventType: hiSysEvent.EventType.FAULT,
params: {
PID: 487,
UID: 103,
PACKAGE_NAME: "com.ohos.hisysevent.test",
PROCESS_NAME: "syseventservice",
MSG: "no msg."
}
}, (err, val) => {
// do something here.
})
hiSysEvent.query({
beginTime: -1,
endTime: -1,
maxEvents: 5,
}, [{
domain: "RELIABILITY",
names: ["STACK"],
}], {
onQuery: function (infos) {
// 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.
先完成此消息的编辑!
想要评论请 注册