Javascript 如何避免NodeJS中切换情况下的重复try/catch

Javascript 如何避免NodeJS中切换情况下的重复try/catch,javascript,node.js,Javascript,Node.js,现在我的代码是这样的: const androidChannel = "android"; const iosChannel = "ios"; const smsChannel = "sms"; const emailChannel = "email"; switch(channel) { case iosChannel: try{ output = await ap

现在我的代码是这样的:

const androidChannel = "android";
const iosChannel = "ios";
const smsChannel = "sms";
const emailChannel = "email";

switch(channel) {
    case iosChannel:
        try{
            output = await apnsAdaptor.processRequest(notificationRequest)
            console.log(output);
        } catch(err) {
            console.log("IOS with err: " + err);
        } 
        break;
        
    case androidChannel:
        try{
            output = await androidAdaptor.processRequest(notificationRequest)
            console.log(output);
        } catch(err) {
            console.log("Android with err: " + err);
        }
        break;
    
    case smsChannel:
        try{
            output = await smsAdaptor.processRequest(notificationRequest)
            console.log(output);
        } catch(err) {
            console.log("Sms with err: " + err);
        }
        break;
    
    default:
        console.log("This is the defualt guy");
        break;
        
}
很明显,每种情况的结构都非常相似,包括捕获错误的处理。由于将添加更多的案例,我希望避免多次重复try/catch结构。我想知道是否有更简洁的方式来写这个


注意:当捕捉到错误时,我仍然希望得到此错误来自哪种情况的通知。

将适配器改为按通道索引的对象,并在对象上查找通道属性:

const adapters = {
  android: <theAndroidAdapter>,
  ios: <theIosAdapter>,
  // ...
};
// ...

const adapter = adapters[channelName];
if (adapter) {
  try {
    console.log(await adapter.processRequest(notificationRequest));
  } catch (err) {
    console.log(channelName + ' with err: ', err);
  }
} else {
  // no matching adapter
}

将适配器改为按通道索引的对象,并在对象上查找通道属性:

const adapters = {
  android: <theAndroidAdapter>,
  ios: <theIosAdapter>,
  // ...
};
// ...

const adapter = adapters[channelName];
if (adapter) {
  try {
    console.log(await adapter.processRequest(notificationRequest));
  } catch (err) {
    console.log(channelName + ' with err: ', err);
  }
} else {
  // no matching adapter
}
试试这个:

试一试{ 开关通道{ 案例频道: 输出=等待apnsAdaptor.processRequestnotificationRequest console.logoutput; 打破 案例渠道: 输出=等待androidAdaptor.processRequestnotificationRequest console.logoutput; 打破 案例smsChannel: 输出=等待smsAdaptor.processRequestnotificationRequest console.logoutput; 打破 违约: console.Log这就是那个被解职的家伙; } }捕手{ console.log`${channel}带有错误:${err}`; } 是的,错误消息略有不同,但是,嘿,你可以做你能做的。

试试这个:

试一试{ 开关通道{ 案例频道: 输出=等待apnsAdaptor.processRequestnotificationRequest console.logoutput; 打破 案例渠道: 输出=等待androidAdaptor.processRequestnotificationRequest console.logoutput; 打破 案例smsChannel: 输出=等待smsAdaptor.processRequestnotificationRequest console.logoutput; 打破 违约: console.Log这就是那个被解职的家伙; } }捕手{ console.log`${channel}带有错误:${err}`; }
是的,错误消息略有不同,但是,嘿,你可以做你能做的。

你可以将switch语句包装在try-catch中:

try {
  switch(channel) {
    case iosChannel:
        output = await apnsAdaptor.processRequest(notificationRequest)
        console.log(output);
        break;
        
    case androidChannel:
        output = await androidAdaptor.processRequest(notificationRequest)
        console.log(output);
        break;
    
    case smsChannel:
        output = await smsAdaptor.processRequest(notificationRequest)
        console.log(output);
        break;
    
    default:
        console.log("This is the defualt guy");
        break;
        
  }
} catch(err) {
    console.log(channel, " with error ", err);
}

您可以将switch语句包装在try-catch中:

try {
  switch(channel) {
    case iosChannel:
        output = await apnsAdaptor.processRequest(notificationRequest)
        console.log(output);
        break;
        
    case androidChannel:
        output = await androidAdaptor.processRequest(notificationRequest)
        console.log(output);
        break;
    
    case smsChannel:
        output = await smsAdaptor.processRequest(notificationRequest)
        console.log(output);
        break;
    
    default:
        console.log("This is the defualt guy");
        break;
        
  }
} catch(err) {
    console.log(channel, " with error ", err);
}

你能不能不把开关放在try-catch,try{…switch-stuff}catcher里{…@Spangle是的,这可能是一种方式,但我仍然希望错误消息告诉我错误来自何处。if-else或switch case的长、重复链的解决方案几乎总是一个映射、对象或其他可设置关键帧的数据结构。有一种错误的感觉,认为switch case比if-else更干净。它几乎总是遇到同样的问题,只是语法更难看。@isakyodo看到我的答案了,开关在try中,但它仍然说错误是从哪里来的,当它第一次发布时,我忘了更改它,但它现在更新了。你能不能不把开关放在try-catch中,try{…switch-stuff}catcher{…@Spangle是的,这可能是一种方式,但我仍然希望错误消息告诉我错误来自何处。if-else或switch case的长、重复链的解决方案几乎总是一个映射、对象或其他可设置关键帧的数据结构。有一种错误的感觉,认为switch case比if-else更干净。它几乎总是遇到同样的问题,只是语法更难看。@isakyodo看到我的答案了,开关在try中,但它仍然说错误是从哪里来的,当它第一次发布时,我忘了更改它,但它现在更新了。等等,console.log可以接受多个参数?我怎么不知道?等等,console.log可以接受多个参数e参数?我怎么不知道?