funny-catch-error
“正确”的异常处理方式,纯属娱乐,生产环境请勿使用,否则后果自负
出现异常时将自动跳转到搜索引擎,可自定义处理方式
🏠 主页
项目简介
- 出现异常时将自动跳转到搜索引擎
- 中文环境跳转百度,英文环境跳转 Google
- 可自定义搜索引擎。
- 纯属娱乐,生产环境请勿使用,否则后果自负
- 以前在网上看到过类似的处理方法,现在闲的无聊就把它实现和优化了一下
核心代码
export async function funnyCatchError(cb: () => any, config: CatchConfig = {}) {
try {
await cb()
} catch (error) {
window.open(`https://www.baidu.com/s?wd=${error.message}`)
}
}
安装
npm i -D funny-catch-error
🚀 使用
import { funnyCatchError } from 'funny-catch-error'
// 直接使用
funnyCatchError(() => {
throw new Error('xxxx is undefined') // 抛出的异常将会自动跳转页面到搜索引擎,例如 `https://www.baidu.com/s?wd=xxxx is undefined`
}
// 也支持异步函数
funnyCatchError(async () => {
throw new Error('async 异步异常处理')
})
// 还可以传入第二个参数作为配置项,具体说明见下文
funnyCatchError(() => {
throw new Error('xxxx is undefined')
}, {
searchEngines: 'https://cn.bing.com/search?q=[key]',
})
浏览器端也可以直接引入 index.js
文件,现代浏览器可以使用 index.esm.js
版本
<script src="https://cdn.jsdelivr.net/npm/funny-catch-error@latest/dist/index.js"></script>
<script>
const { funnyCatchError } = FunnyCatchError
funnyCatchError(() => {
throw new Error('xxxx is undefined')
})
</script>
配置项
interface CatchConfig {
/**
* 自定义搜索引擎。若配置该项将覆盖默认配置。[key] 会被替换为具体 errorMessage
* 例如:https://cn.bing.com/search?q=[key]
*
* @type {string}
*/
searchEngines?: string
/**
* 是否打开新的窗口。默认为中文环境 true ,英文环境 false
*
* @type {boolean}
*/
isOpenNewWindow?: boolean
}
作者
- Website: https://blog.cmyr.ltd
- GitHub: @CaoMeiYouRen
🤝 贡献
欢迎Contributions, issues and feature!
如有问题请查看 issues page.
支持
如果觉得这个项目有用的话请给一颗
📝 License
Copyright © 2020 CaoMeiYouRen.
This project is MIT licensed.
_This README was generated with