mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Use more async funcs in runtime/lib/api to reduce Promise creation
This commit is contained in:
@@ -36,32 +36,30 @@ var api = module.exports = {
|
||||
* @return {Promise<String|Object>} - resolves when complete
|
||||
* @memberof @node-red/runtime_library
|
||||
*/
|
||||
getEntry: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.library.getEntry(opts.library,opts.type,opts.path).then(function(result) {
|
||||
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path}, opts.req);
|
||||
return resolve(result);
|
||||
}).catch(function(err) {
|
||||
if (err) {
|
||||
runtime.log.warn(runtime.log._("api.library.error-load-entry",{library:opts.library,type:opts.type,path:opts.path,message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
err.status = 403;
|
||||
return reject(err);
|
||||
} else if (err.code === "not_found") {
|
||||
err.status = 404;
|
||||
} else {
|
||||
err.status = 400;
|
||||
}
|
||||
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path,error:err.code}, opts.req);
|
||||
return reject(err);
|
||||
getEntry: async function(opts) {
|
||||
return runtime.library.getEntry(opts.library,opts.type,opts.path).then(function(result) {
|
||||
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path}, opts.req);
|
||||
return result;
|
||||
}).catch(function(err) {
|
||||
if (err) {
|
||||
runtime.log.warn(runtime.log._("api.library.error-load-entry",{library:opts.library,type:opts.type,path:opts.path,message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
err.status = 403;
|
||||
throw err;
|
||||
} else if (err.code === "not_found") {
|
||||
err.status = 404;
|
||||
} else {
|
||||
err.status = 400;
|
||||
}
|
||||
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,error:"not_found"}, opts.req);
|
||||
var error = new Error();
|
||||
error.code = "not_found";
|
||||
error.status = 404;
|
||||
return reject(error);
|
||||
});
|
||||
})
|
||||
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,path:opts.path,error:err.code}, opts.req);
|
||||
throw err;
|
||||
}
|
||||
runtime.log.audit({event: "library.get",library:opts.library,type:opts.type,error:"not_found"}, opts.req);
|
||||
var error = new Error();
|
||||
error.code = "not_found";
|
||||
error.status = 404;
|
||||
throw error;
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -77,23 +75,20 @@ var api = module.exports = {
|
||||
* @return {Promise} - resolves when complete
|
||||
* @memberof @node-red/runtime_library
|
||||
*/
|
||||
saveEntry: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
runtime.library.saveEntry(opts.library,opts.type,opts.path,opts.meta,opts.body).then(function() {
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path}, opts.req);
|
||||
return resolve();
|
||||
}).catch(function(err) {
|
||||
runtime.log.warn(runtime.log._("api.library.error-save-entry",{path:opts.path,message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"forbidden"}, opts.req);
|
||||
err.status = 403;
|
||||
return reject(err);
|
||||
}
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"unexpected_error",message:err.toString()}, opts.req);
|
||||
var error = new Error();
|
||||
error.status = 400;
|
||||
return reject(error);
|
||||
});
|
||||
})
|
||||
saveEntry: async function(opts) {
|
||||
return runtime.library.saveEntry(opts.library,opts.type,opts.path,opts.meta,opts.body).then(function() {
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path}, opts.req);
|
||||
}).catch(function(err) {
|
||||
runtime.log.warn(runtime.log._("api.library.error-save-entry",{path:opts.path,message:err.toString()}));
|
||||
if (err.code === 'forbidden') {
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"forbidden"}, opts.req);
|
||||
err.status = 403;
|
||||
throw err;
|
||||
}
|
||||
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"unexpected_error",message:err.toString()}, opts.req);
|
||||
var error = new Error();
|
||||
error.status = 400;
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user