自定义Error类

通过继承原生Error类,实现定制的error

class HTTPError extends Error{
    constructor(status, statusText, url){
        super(`${status} ${statusText}: ${url}`)
        this.status =status
        this.statusText = statusText
        this.url = url
    }

    get name(){
        return "HTTPError"
    }
}

let error = new HTTPError(404, "Not Found", "http://www.baidu.com")
error.status    // 404
error.statusText    // Not Found
error.url   // http://www.baidu.com

 

posted @ 2021-12-15 22:44  邢韬  阅读(34)  评论(0编辑  收藏  举报