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 🙏

© 2025 – Pkg Stats / Ryan Hefner

class-error-catch

v1.0.31

Published

catch class error

Readme

基于 decorator 的错误捕捉
支持方法,getter,class

Installation

Node

npm install class-error-catch --save

Options

createCatchError supports the following options:

  • errorHandler: 错误处理函数
    默认处理函数如下
/**
 * 默认错误处理函数
 * @param {Error} err  错误信息
 * @param {Class} target 错误发生所在的Class
 * @param {Class Instance} context  错误发生所在的Class实例对象
 * @param {String} methodName   错误发生的方法名
 * @param  {...any} params  其他自定义参数
 */
function defaultErrorHanlder(err, target, context, methodName, ...params) {
    console.log(
        "error catched by catchError decotator",
        err,
        target,
        context,
        methodName,
        ...params
    );
}
  • filter: 当装饰 class 时有用, 过滤哪些方法需要捕捉错误

Demo

Usage

基本使用

const createCatchError = require("class-error-catch");

const config = {
    errorHandler: function(
        err,
        target,
        context,
        methodName,
        message,
    ) {
        console.log("err:", err); //错误信息
        console.log("target:", target); //错误发生所在的Class
        console.log("context:", context); // 错误发生所在的Class实例对象
        console.log("methodName:", methodName); // 错误发生的方法名
        console.log("message:", message); // 自定义的错误消息
    }
};

const catchError = createCatchError(config);

function getPromise() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            reject(new Error("超时错误"));
        }, 1000);
    });
}

class DemoClass {

    @catchError("Error from method sayHi")
    sayHi() {
        console.log(this.xxx.xxx);
    }

    @catchError("Error from async method sayHi2")
    async sayHi2() {
        const r = await getPromise();
        console.log("result", r);
    }
}
const demoClass = new DemoClass();
demoClass.sayHi();
demoClass.sayHi2();

//输出
//sayHi in  DemoClass
//自定义消息:Error from method sayHi

//sayHi2 in  DemoClass
//自定义消息:Error from async method sayHi2

react 中使用

import React, { Component } from "react";

import createCatchError from "class-error-catch";

const BUILTIN_METHODS = [
    "constructor",
    "render",
    "replaceState",
    "setState",
    "isMounted",
    "replaceState"
];

const PREFIX = ["component", "unsafe_"];
function customFilter(propertyName, descriptor) {
    if (typeof name !== "string" || name.trim() === "") {
        return false;
    }
    // 以component或者unsafe_开头
    if (PREFIX.some(prefix => name.startsWith(prefix))) {
        return true;
    }
    // 其他内置方法
    if (BUILTIN_METHODS.includes(name)) {
        return true;
    }
    return false;
}
const config = {
    errorHandler: function(
        err,
        target,
        context,
        methodName,
        message
    ) {
        console.log("err:", err); //错误信息
        console.log("target:", target); //错误发生所在的Class
        console.log("context:", context); // 错误发生所在的Class实例对象
        console.log("methodName:", methodName); // 错误发生的方法名
        console.log("message:", message); // 自定义的错误消息
    },
    filter: function(propertyName, descriptor) {
        return !customFilter(propertyName);
    }
};

const catchError = createCatchError(config);

class App extends Component {

    state = {
        xxx: "xxx"
    };

    @catchError("发生异常了啊 onClick_Initializer")
    onClick_Initializer = e => {
        console.log(window.onClick_Initializer.xxx);
    };

    @catchError("发生异常了啊 onClick_Fun")
    onClick_Fun() {
        console.log("xxx");
        console.log(window.onClick_Fun.xxxxx);
        this.setState({ xxx: "yyyy" }, () => {
            console.log(this.state);
        });
    }

    render() {
        const { onClick_Initializer, onClick_Fun } = this;
        return (
            <div className="App">
                <input
                    type="button"
                    value="initializer catch"
                    onClick={onClick_Initializer}
                />
                <input
                    type="button"
                    value="fun catch"
                    onClick={() => onClick_Fun.bind(this)()}
                />
            </div>
        );
    }
}

export default App;

Typings

none

License

MIT