nodejs尝试捕获并重新使用状态

时间:2018-09-23 14:48:21

标签: node.js express

对于try / catch和res.status的结构,我对代码仍然不满意。 我猜想当请求中的错误作为响应返回时,catch块将永远不会到达吗? 构造此错误的最佳方法是什么,我只想就发生的错误??退回正确的res.status?

setPublicLink: async (req, res, next) => {

    try{
        console.log('Entering setPublicLink');

        var mytoken = "Bearer "+process.env.MY_TOKEN;

        request({
                url: ' https://content.dropboxapi.com/2/files/upload',
                headers: {
                  'content-type' : 'application/octet-stream',
                  'authorization' : mytoken
                },
                encoding: null,
                method: 'POST',
                body: fs.createReadStream(fileItem.path),
                encoding: null
               }, (error, response, body) => {
                    if (error) {
                        console.log(error);
                        console.log(response);
                        //return res.status(401).json({error: 'Upload of file was not successful.'});
                    } else if ( response.statusCode == 200) {
                        //nothing to do
                    } else {
                        console.log(response);
                        return res.status(401).json({error: 'Upload of file was not successful.'});
                    }
               });

        request({
            url: 'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings',
            headers: {
                'content-type' : 'application/json',
                'authorization' : mytoken
            },
            method: 'POST',
            body: JSON.stringify(reqBodyJson)
            }, (error, response, body) => {
                if (error) {
                    console.log(error);
                    console.log(response);
                    return res.status(401).json({error: 'Error when trying to set public link'});
                } else if ( response.statusCode == 200) {
                    return res.status(200).json({success: 'Public Link has been set successfully.'});  
                } else if ( response.statusCode == 409) {
                    return res.status(409).json({error: 'Nothing to set. Public link already exists.'});  
                } else {
                    return res.status(401).json({error: 'Could not set public link'});
                }
            });

    }
    catch (err) {
        console.log('Error Occured : '+ err);
        return res.status(401).json({error: 'Error occured trying to set publicLink'});
    }

},

1 个答案:

答案 0 :(得分:0)

由于您已处理所有错误,因此您的代码将永远不会到达catch块。但是您的代码风格难以维护,因此最好使用promises,例如,您可以使用request-promise模块,那么您的代码会像是节点8+一样:

setPublicLink: async (req, res, next) => {
try{
    console.log('Entering setPublicLink');
    var mytoken = "Bearer "+process.env.MY_TOKEN;
    var response = await requestPromise({
            url: ' https://content.dropboxapi.com/2/files/upload',
            headers: {
              'content-type' : 'application/octet-stream',
              'authorization' : mytoken
            },
            encoding: null,
            method: 'POST',
            body: fs.createReadStream(fileItem.path),
            encoding: null
           });
var response1 = await requestPromise({

url: 
'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings',
            headers: {
                'content-type' : 'application/json',
                'authorization' : mytoken
            },
            method: 'POST',
            body: JSON.stringify(reqBodyJson)
          });
res.send({success: 'Public Link has been set successfully.'});// it sends 200 automatically
    }
catch(ex){
res.sendStatus(401);
}
    }

您还可以获得响应头。