在ES6 Node.js中,如何在任何地方提供类?

时间:2018-08-09 13:58:12

标签: javascript node.js error-handling ecmascript-6 custom-error-handling

在我的ES6 Node.js应用程序中,我有一个带有一堆自定义错误类的文件,即

class FirstCustomError extends Error {
  constructor(message) {
    super(message);
    this.name = "FirstCustomError";
  }
}

class SecondCustomError extends Error {
  constructor(message) {
    super(message);
    this.name = "SecondCustomError";
  }
}

// ...

如何使这些类在应用程序中的任何地方可用?理想情况下,我不必将它们导入需要的每个文件中,也不必求助于throw new global.errors.FirstCustomError('error message')

也就是说,理想情况下,我可以做throw new FirstCustomError('error message')

4 个答案:

答案 0 :(得分:1)

您确实可以将它们放在the global object上:

global.FirstCustomError = class extends Error {
  constructor(message) {
    super(message);
    this.name = "FirstCustomError";
  }
};

global.SecondCustomError = class extends Error {
  constructor(message) {
    super(message);
    this.name = "SecondCustomError";
  }
};

您需要确保在其他任何模块使用它们之前就发生这种情况,即您将其放在主脚本开始处需要的模块中。更好(更可重用)的方法是从错误模块中正常导出它们,然后执行

Object.assign(global, require('./errors.js'));

在您的主脚本中将所有导出安装为全局变量。


免责声明:当然,这是一种可怕的做法!请不要这样做!只需将它们明确导入到您需要的任何地方即可。 ES6模块语法轻而易举,使一切变得更加清晰。同样,您的应用程序中不可能有太多自定义错误,这些错误确实是跨领域的,并且与特定功能无关,在这种情况下,您无论如何都不应将它们全部放在同一个模块中。

答案 1 :(得分:0)

您可以将它们添加到运行的第一个js文件中(例如app.js),并确保它们在全局范围内,以便它们可以在任何地方重用,但是必须首先运行它们,以便在其他情况下可以存在代码尝试调用它们

 Sip Mesajları 
sendRegister : REGISTER sip:801@kdogutas.com SIP/2.0
Call-ID: 49d0e36414c1d1abaa33a256b318b171@192.168.2.86
CSeq: 1 REGISTER
From: "Zeynep Eğilmez" <sip:801@kdogutas.com:5060>;tag=724913436
To: "801" <sip:801@kdogutas.com>
Via: SIP/2.0/UDP 192.168.2.86:5060;rport;branch=z9hG4bK-373730-3cf966a6a3c0177a572d2a23fe506287
Max-Forwards: 70
Contact: <sip:801@192.168.2.86:5060;udp>
Expires: 60
Content-Length: 0

Received processResponse: SIP/2.0 401 Unauthorized
Call-ID: 49d0e36414c1d1abaa33a256b318b171@192.168.2.86
CSeq: 1 REGISTER
From: "Zeynep Eğilmez" <sip:801@kdogutas.com:5060>;tag=724913436
To: "801" <sip:801@kdogutas.com>;tag=b059b3424eb911ed130009d1469e6b45.a968
Via: SIP/2.0/UDP 192.168.2.86:5060;received=78.188.3.82;rport=1030;branch=z9hG4bK-373730-3cf966a6a3c0177a572d2a23fe506287
WWW-Authenticate: Digest realm="kdogutas.com",nonce="5b6c4dd100013d145714177985de24ad385d4f548e9c207c"
Server: NORMSWITCH V3.14 KDOGUTAS
Content-Length: 0

Received processResponse: SIP/2.0 200 OK
Call-ID: 49d0e36414c1d1abaa33a256b318b171@192.168.2.86
CSeq: 2 REGISTER
From: "Zeynep Eğilmez" <sip:801@kdogutas.com:5060>;tag=724913436
To: "801" <sip:801@kdogutas.com>;tag=b059b3424eb911ed130009d1469e6b45.1432
Via: SIP/2.0/UDP 192.168.2.86:5060;received=78.188.3.82;rport=1030;branch=z9hG4bK-373730-f7b309520389533925035dfe3bc703cb
Contact: <sip:801@78.188.3.82:1030;udp>;expires=60
Server: NORMSWITCH V3.14 KDOGUTAS
Content-Length: 0
sendRequest SdpInfo Send ringbacktone.wav  : INVITE sip:802@kdogutas.com SIP/2.0
Call-ID: 39768268317c68212845c5ac32316c33@192.168.2.86
CSeq: 2 INVITE
From: "Kadriye Doğutaş" <sip:801@kdogutas.com>;tag=724913436
To: <sip:802@kdogutas.com>
Via: SIP/2.0/UDP 192.168.2.86:5060;branch=z9hG4bK-373730-1b367e5c8080590ed71e86bd3de92fb6
Max-Forwards: 20
Contact: <sip:801@192.168.2.86:5060;udp>
Route: <sip:802@kdogutas.com>
Content-Type: application/sdp
Content-Length: 235

v=0
o=- 3742813239 3742813239 IN IP4 192.168.2.86
s=-
c=IN IP4 192.168.2.86
t=0 0
m=video 0 RTP/AVP 0
m=audio 40000 RTP/AVP 8 101
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=sendrecv
a=ptime:20
Received processResponse: SIP/2.0 407 Proxy Authentication Required
Call-ID: 39768268317c68212845c5ac32316c33@192.168.2.86
CSeq: 2 INVITE
From: "Kadriye Doğutaş" <sip:801@kdogutas.com>;tag=724913436
To: <sip:802@kdogutas.com>;tag=b059b3424eb911ed130009d1469e6b45.90d1
Via: SIP/2.0/UDP 192.168.2.86:5060;received=78.188.3.82;rport=1030;branch=z9hG4bK-373730-1b367e5c8080590ed71e86bd3de92fb6
Proxy-Authenticate: Digest realm="kdogutas.com",nonce="5b6c4dd400013d17d5ce3e2f006d64f76f721c482d32c154"
Server: NORMSWITCH V3.14 KDOGUTAS
Content-Length: 0

Received processResponse: SIP/2.0 100 Giving a try
Call-ID: 39768268317c68212845c5ac32316c33@192.168.2.86
CSeq: 3 INVITE
From: "Kadriye Doğutaş" <sip:801@kdogutas.com>;tag=724913436
To: <sip:802@kdogutas.com>
Via: SIP/2.0/UDP 192.168.2.86:5060;received=78.188.3.82;rport=1030;branch=z9hG4bK-373730-133c6c506e767d97418dba14fc5725b8
Server: NORMSWITCH V3.14 KDOGUTAS
Content-Length: 0

Received processResponse: SIP/2.0 183 Session Progress
Via: SIP/2.0/UDP 192.168.2.86:5060;rport=1030;received=78.188.3.82;branch=z9hG4bK-373730-133c6c506e767d97418dba14fc5725b8
Record-Route: <sip:185.124.84.100;lr;ftag=724913436;did=423.b4fe0cf3>
From: "Kadriye Doğutaş" <sip:801@kdogutas.com>;tag=724913436
To: <sip:802@kdogutas.com>;tag=FZv3gvvt39BXg
Call-ID: 39768268317c68212845c5ac32316c33@192.168.2.86
CSeq: 3 INVITE
Contact: <sip:802@192.168.10.110:5060;transport=udp>
User-Agent: NORM MEDIA SERVER V3.14 © KDOGUTAS
Accept: application/sdp
Allow: INVITE,ACK,BYE,CANCEL,OPTIONS,MESSAGE,INFO,UPDATE,REGISTER,REFER,NOTIFY
Supported: timer,path,replaces
Allow-Events: talk,hold,conference,refer
Content-Type: application/sdp
Remote-Party-ID: "#RR001#RA0002#SNA#802" <sip:#RR001#RA0002#SNA#802@kdogutas.com>;party=calling;privacy=off;screen=no
Content-Length: 268

v=0
o=NORMMEDIASERVER 1533797619 1533797620 IN IP4 185.124.84.110
s=NORMMEDIASERVER
c=IN IP4 185.124.84.110
t=0 0
m=audio 26824 RTP/AVP 8 101
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20
a=sendrecv
m=video 0 RTP/AVP 19
Received processResponse: SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.2.86:5060;rport=1030;received=78.188.3.82;branch=z9hG4bK-373730-133c6c506e767d97418dba14fc5725b8
Record-Route: <sip:185.124.84.100;lr;ftag=724913436;did=423.b4fe0cf3>
From: "Kadriye Doğutaş" <sip:801@kdogutas.com>;tag=724913436
To: <sip:802@kdogutas.com>;tag=FZv3gvvt39BXg
Call-ID: 39768268317c68212845c5ac32316c33@192.168.2.86
CSeq: 3 INVITE
Contact: <sip:802@192.168.10.110:5060;transport=udp>
User-Agent: NORM MEDIA SERVER V3.14 © KDOGUTAS
Allow: INVITE,ACK,BYE,CANCEL,OPTIONS,MESSAGE,INFO,UPDATE,REGISTER,REFER,NOTIFY
Supported: timer,path,replaces
Allow-Events: talk,hold,conference,refer
Content-Type: application/sdp
Remote-Party-ID: "Outbound Call" <sip:802@kdogutas.com>;party=calling;privacy=off;screen=no
Content-Length: 246

v=0
o=NORMMEDIASERVER 1533797619 1533797621 IN IP4 185.124.84.110
s=NORMMEDIASERVER
c=IN IP4 185.124.84.110
t=0 0
m=audio 26824 RTP/AVP 8 101
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20
a=sendrecv

INVITE 200 processResponse Send : ACK sip:802@192.168.10.110:5060;transport=udp SIP/2.0
Call-ID: 39768268317c68212845c5ac32316c33@192.168.2.86
CSeq: 3 ACK
Via: SIP/2.0/UDP 192.168.2.86:5060;branch=z9hG4bK-373730-57d38f452333c14c7e1cc8b65dafde0a
From: "Kadriye Doğutaş" <sip:801@kdogutas.com>;tag=724913436
To: <sip:802@kdogutas.com>;tag=FZv3gvvt39BXg
Max-Forwards: 70
Route: <sip:185.124.84.100;lr;ftag=724913436;did=423.b4fe0cf3>
Contact: <sip:801@192.168.2.86:5060;udp>
Content-Length: 0

Received processRequest: BYE sip:801@78.188.3.82:1030;udp SIP/2.0
Via: SIP/2.0/UDP 185.124.84.100:5060;branch=z9hG4bKf00c.420d8c85.0,SIP/2.0/UDP 192.168.10.110;received=192.168.10.110;rport=5060;branch=z9hG4bK281K63Z85g3vN
Max-Forwards: 70
From: <sip:802@kdogutas.com>;tag=FZv3gvvt39BXg
To: "Kadriye Doğutaş" <sip:801@kdogutas.com>;tag=724913436
Call-ID: 39768268317c68212845c5ac32316c33@192.168.2.86
CSeq: 126576169 BYE
User-Agent: NORM MEDIA SERVER V3.14 © KDOGUTAS
Allow: INVITE,ACK,BYE,CANCEL,OPTIONS,MESSAGE,INFO,UPDATE,REGISTER,REFER,NOTIFY
Supported: timer,path,replaces
Reason: Q.850;cause=16;text="NORMAL_CLEARING"
Content-Length: 0

答案 2 :(得分:0)

通常,要访问一个类,您需要像这样导出它:

exports.NameOfFunction = async (req, resp) =>{..... your code}; 

不需要异步!就算您需要等待Promise的完整回应 我希望这会有所帮助!

答案 3 :(得分:-1)

您要实现的目标是其他海报建议的 可能 ,但不是 推荐 。一种理想的方法是将它们导出到模块中,然后将其导入到您需要的任何位置。

customErrors.js

export.modules.FirstCustomError = FirstCustomError
export.modules.SecondCustomError = SecondCustomError

我知道这不是您要寻找的东西,但是有思想的发展有时是好的。在模块化环境中工作时,global的分配在实践中是很头疼的。您可以通过导入在代码中使用它们。

import { FirstCustomError } from './customErrors.js'