-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yitan.lgh
committed
Dec 23, 2020
1 parent
7838bc7
commit ed589b0
Showing
54 changed files
with
1,001 additions
and
573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
group: | ||
title: application | ||
title: errorEvent | ||
--- | ||
|
||
# universal-error-event | ||
[![npm](https://img.shields.io/npm/v/universal-error-event.svg)](https://www.npmjs.com/package/universal-error-event) | ||
|
||
Monitoring and cancellation of error events | ||
|
||
## Support | ||
<img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> <img alt="bytedanceMicroApp" src="https://gw.alicdn.com/tfs/TB1jFtVzO_1gK0jSZFqXXcpaXXa-200-200.svg" width="25px" height="25px"> | ||
|
||
## Install | ||
|
||
```bash | ||
$ npm install universal-error-event --save | ||
``` | ||
|
||
## Function | ||
|
||
### `onError(callback): void` | ||
|
||
Listens on the Mini Program error event. This event is triggered as a result of script error or failed API call. | ||
|
||
#### Parameters | ||
function callback | ||
The callback function for the Mini Program error event | ||
|
||
##### Parameters | ||
string error | ||
Error message, including stacks. | ||
|
||
### `offError(callback): void` | ||
|
||
Un-listens on Mini Program error event. | ||
|
||
#### Parameters | ||
function callback | ||
The callback function for the Mini Program error event | ||
|
||
### `onUnhandledRejection(callback): void` | ||
|
||
Listen for unhandled Promise rejection events. | ||
|
||
#### Parameters | ||
function callback | ||
The callback function for the Mini Program error event | ||
|
||
##### Parameters | ||
Object res | ||
|Property |Type |Description| | ||
|:--|:--|:--| | ||
|reason |string or Error| Reason for rejection, usually an Error object| | ||
|promise| Promise.<any>|Rejected Promise object| | ||
|
||
### `offUnhandledRejection(callback): void` | ||
|
||
Cancel listening for unhandled Promise rejection events. | ||
|
||
#### Parameters | ||
function callback | ||
The callback function for the Mini Program error event | ||
|
||
## Example | ||
|
||
```js | ||
import errorEvent from 'universal-error-event'; | ||
|
||
errorEvent.onError(e => { | ||
console.log(e); | ||
}); | ||
|
||
errorEvent.offError(); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
group: | ||
title: 应用级事件 | ||
title: errorEvent | ||
--- | ||
|
||
# universal-error-event | ||
[![npm](https://img.shields.io/npm/v/universal-error-event.svg)](https://www.npmjs.com/package/universal-error-event) | ||
|
||
错误事件的监听与取消 | ||
|
||
## 支持 | ||
<img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> <img alt="bytedanceMicroApp" src="https://gw.alicdn.com/tfs/TB1jFtVzO_1gK0jSZFqXXcpaXXa-200-200.svg" width="25px" height="25px"> | ||
|
||
## 安装 | ||
|
||
```bash | ||
$ npm install universal-error-event --save | ||
``` | ||
|
||
## 方法 | ||
|
||
### `onError(callback): void` | ||
|
||
监听小程序错误事件。如脚本错误或 API 调用报错等。 | ||
|
||
#### 参数 | ||
function callback | ||
小程序错误事件的回调函数 | ||
|
||
##### 参数 | ||
string error | ||
错误信息,包含堆栈 | ||
|
||
### `offError(callback): void` | ||
|
||
取消监听小程序错误事件。 | ||
|
||
#### 参数 | ||
function callback | ||
小程序错误事件的回调函数 | ||
|
||
### `onUnhandledRejection(callback): void` | ||
|
||
监听未处理的 Promise 拒绝事件。 | ||
|
||
#### 参数 | ||
function callback | ||
小程序错误事件的回调函数 | ||
|
||
##### 参数 | ||
Object res | ||
|属性 |类型 |说明| | ||
|:--|:--|:--| | ||
|reason |string or Error| 拒绝原因,一般是一个 Error 对象| | ||
|promise| Promise.<any>| 被拒绝的 Promise 对象| | ||
|
||
### `offUnhandledRejection(callback): void` | ||
|
||
取消监听未处理的 Promise 拒绝事件。 | ||
|
||
#### 参数 | ||
function callback | ||
小程序错误事件的回调函数 | ||
|
||
## 示例 | ||
|
||
```js | ||
import errorEvent from 'universal-error-event'; | ||
|
||
errorEvent.onError(e => { | ||
console.log(e); | ||
}); | ||
|
||
errorEvent.offError(); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createElement, useEffect, useState, useRef } from 'rax'; | ||
import View from 'rax-view'; | ||
import Text from 'rax-text'; | ||
import errorEvent from 'universal-error-event'; | ||
import {showToast} from 'universal-toast'; | ||
|
||
export default function() { | ||
const ref = useRef((res) => { | ||
showToast('捕获到一个错误'); | ||
}); | ||
return ( | ||
<View> | ||
<Text>errorEvent</Text> | ||
<View onClick={() => { | ||
errorEvent.onError(ref.current); | ||
}}>监听错误</View> | ||
<View onClick={() => { | ||
throw new Error('我是一个错误'); | ||
}}>触发错误</View> | ||
<View onClick={() => { | ||
errorEvent.offError(ref.current); | ||
}}>取消监听</View> | ||
</View> | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/application/error-handler/src/ali-miniapp/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { isDingdingMiniapp } from 'universal-env'; | ||
import {Callback, RejectCallback} from '../types'; | ||
|
||
export const onError = (cb: Callback) => { | ||
return isDingdingMiniapp ? dd.onError(cb) : my.onError(cb); | ||
}; | ||
export const offError = (cb?: Callback) => { | ||
return isDingdingMiniapp ? dd.offError(cb) : my.offError(cb); | ||
}; | ||
export const onUnhandledRejection = (cb: RejectCallback) => { | ||
return isDingdingMiniapp ? dd.onUnhandledRejection(cb) : my.onUnhandledRejection(cb); | ||
}; | ||
export const offUnhandledRejection = (cb?: RejectCallback) => { | ||
return isDingdingMiniapp ? dd.offUnhandledRejection(cb) : my.offUnhandledRejection(cb); | ||
}; | ||
export default { | ||
onError, | ||
offError, | ||
onUnhandledRejection, | ||
offUnhandledRejection, | ||
}; |
20 changes: 20 additions & 0 deletions
20
packages/application/error-handler/src/byte-miniapp/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {Callback, RejectCallback} from '../types'; | ||
|
||
export const onError = (cb: Callback) => { | ||
return tt.onError(cb); | ||
}; | ||
export const offError = (cb?: Callback) => { | ||
return tt.offError(cb); | ||
}; | ||
export const onUnhandledRejection = (cb: RejectCallback) => { | ||
return tt.onUnhandledRejection(cb); | ||
}; | ||
export const offUnhandledRejection = (cb?: RejectCallback) => { | ||
return tt.offUnhandledRejection(cb); | ||
}; | ||
export default { | ||
onError, | ||
offError, | ||
onUnhandledRejection, | ||
offUnhandledRejection, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { isMiniApp, isWeChatMiniProgram, isWeb, isByteDanceMicroApp } from 'universal-env'; | ||
import aliMiniAppModule from './ali-miniapp/index'; | ||
import weChatModule from './wechat-miniapp/index'; | ||
import bytedanceModule from './byte-miniapp/index'; | ||
import webModule from './web/index'; | ||
import {Callback, RejectCallback, ErrorEvent} from './types'; | ||
|
||
export const onError = (cb: Callback) => { | ||
if (isWeChatMiniProgram) { | ||
return weChatModule.onError(cb); | ||
} else if (isByteDanceMicroApp) { | ||
return bytedanceModule.onError(cb); | ||
} else if (isMiniApp) { | ||
return aliMiniAppModule.onError(cb); | ||
} else if (isWeb) { | ||
return webModule.onError(cb); | ||
} else { | ||
throw new Error('universal-api:ErrorEvent暂不支持'); | ||
} | ||
}; | ||
export const offError = (cb: () => void) => { | ||
if (isWeChatMiniProgram) { | ||
return weChatModule.offError(cb); | ||
} else if (isByteDanceMicroApp) { | ||
return bytedanceModule.offError(cb); | ||
} else if (isMiniApp) { | ||
return aliMiniAppModule.offError(cb); | ||
} else if (isWeb) { | ||
return webModule.offError(cb); | ||
} else { | ||
throw new Error('universal-api:ErrorEvent暂不支持'); | ||
} | ||
}; | ||
export const onUnhandledRejection = (cb: RejectCallback) => { | ||
if (isWeChatMiniProgram) { | ||
return weChatModule.onUnhandledRejection(cb); | ||
} else if (isByteDanceMicroApp) { | ||
return bytedanceModule.onUnhandledRejection(cb); | ||
} else if (isMiniApp) { | ||
return aliMiniAppModule.onUnhandledRejection(cb); | ||
} else if (isWeb) { | ||
return webModule.onUnhandledRejection(cb); | ||
} else { | ||
throw new Error('universal-api:ErrorEvent暂不支持'); | ||
} | ||
}; | ||
export const offUnhandledRejection = (cb?: RejectCallback) => { | ||
if (isWeChatMiniProgram) { | ||
return weChatModule.offUnhandledRejection(cb); | ||
} else if (isByteDanceMicroApp) { | ||
return bytedanceModule.offUnhandledRejection(cb); | ||
} else if (isMiniApp) { | ||
return aliMiniAppModule.offUnhandledRejection(cb); | ||
} else if (isWeb) { | ||
return webModule.offUnhandledRejection(cb); | ||
} else { | ||
throw new Error('universal-api:ErrorEvent暂不支持'); | ||
} | ||
}; | ||
let res: ErrorEvent; | ||
if (isWeChatMiniProgram) { | ||
res = weChatModule; | ||
} else if (isByteDanceMicroApp) { | ||
res = bytedanceModule; | ||
} else if (isMiniApp) { | ||
res = aliMiniAppModule; | ||
} else { | ||
throw new Error('universal-api:ErrorEvent暂不支持'); | ||
} | ||
export default res; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface RejectCallbackArgs { | ||
reason: string | Error | Event; | ||
promise: Promise<any>; | ||
} | ||
export interface Callback { | ||
(res: string | Error | Event): void; | ||
} | ||
export interface RejectCallback { | ||
(res: RejectCallbackArgs): void; | ||
} | ||
|
||
export interface ErrorEvent{ | ||
onError(callback: Callback): void; | ||
offError(callback?: Callback): void; | ||
onUnhandledRejection(callback: RejectCallback): void; | ||
offUnhandledRejection(callback?: RejectCallback): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {Callback, RejectCallback} from '../types'; | ||
|
||
export const onError = (cb: Callback) => { | ||
return window.addEventListener('error', cb); | ||
}; | ||
export const offError = (cb?: Callback) => { | ||
return window.removeEventListener('error', cb); | ||
}; | ||
export const onUnhandledRejection = (cb: RejectCallback) => { | ||
return window.addEventListener('unhandledrejection', cb); | ||
}; | ||
export const offUnhandledRejection = (cb?: RejectCallback) => { | ||
return window.removeEventListener('unhandledrejection', cb); | ||
}; | ||
export default { | ||
onError, | ||
offError, | ||
onUnhandledRejection, | ||
offUnhandledRejection, | ||
}; |
20 changes: 20 additions & 0 deletions
20
packages/application/error-handler/src/wechat-miniapp/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {Callback, RejectCallback} from '../types'; | ||
|
||
export const onError = (cb: Callback) => { | ||
return wx.onError(cb); | ||
}; | ||
export const offError = (cb?: Callback) => { | ||
return wx.offError(cb); | ||
}; | ||
export const onUnhandledRejection = (cb: RejectCallback) => { | ||
return wx.onUnhandledRejection(cb); | ||
}; | ||
export const offUnhandledRejection = (cb?: RejectCallback) => { | ||
return wx.offUnhandledRejection(cb); | ||
}; | ||
export default { | ||
onError, | ||
offError, | ||
onUnhandledRejection, | ||
offUnhandledRejection, | ||
}; |
Oops, something went wrong.