npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

funny-catch-error

v1.1.0

Published

“正确”的异常处理方式,纯属娱乐,生产环境请勿使用,否则后果自负

Downloads

5

Readme

“正确”的异常处理方式,纯属娱乐,生产环境请勿使用,否则后果自负

出现异常时将自动跳转到搜索引擎,可自定义处理方式

🏠 主页

项目简介

  • 出现异常时将自动跳转到搜索引擎
    • 中文环境跳转百度,英文环境跳转 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
}

作者

👤 CaoMeiYouRen

🤝贡献

欢迎Contributions, issues and feature!

如有问题请查看 issues page.

支持

如果觉得这个项目有用的话请给一颗⭐️,非常感谢

📝 License

Copyright © 2020 CaoMeiYouRen. This project is MIT licensed.


_This README was generated with ❤️ by readme-md-generator