Catch 没有处理拒绝承诺
Catch is not handling rejection of promise
所以,我的问题如下。我正在返回一个承诺,当存储一些数据时没有问题时解决,但是如果(1.)我要保存文件的目录不存在或(2.)该文件夹中已经存在一个文件用那个名字;承诺拒绝特定错误。
Router.post('/save/:userid?/:name?/:type?/:payload?', ( request, response ) => {
let promise = new Promise( async ( resolve, reject ) => {
let params = { ...request.params, ...request.query, ...request.body };
let path = `./GameContent/Data/${ params.userid }`;
let fileStream = new FileStream();
let exists = await fileStream.directoryExists( path );
if ( exists ) {
let fileDirectory = `${ path }/${ params.name }.json`;
exists = await fileStream.directoryExists( fileDirectory );
if ( !exists ) {
let bytesWritten = await fileStream.writeFileAt ( fileDirectory, params.payload );
let result = { name: params.name, bytes: bytesWritten, error: 0 };
resolve( JSON.stringify( result ) );
}
else {
let result = { error: 102 };
reject( JSON.stringify( result ) );
}
}
else {
let result = { error: 101 };
reject( JSON.stringify( result ) );
}
});
response.send( promise );
});
到目前为止一切顺利,但是,问题是以下代码没有处理拒绝。
$.post('/api/save', levelJSON)
.catch ( err => {
//Do something...
});
我目前收到了我预期的错误(错误:102)。但是,我一直无法处理错误。
调试器正在抛出这个:
UnhandledPromiseRejectionWarning: {"error":102}
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。
jQuery $.post
has no .catch()
method for error catching. You can try .fail()
方法改为:
$.post('/api/save', levelJSON)
.done(function (response) {
// success logic here
})
.fail(function (jqXHR, exception) {
// Error logic here
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
console.log(msg)
});
您可以更新路由器代码以捕获如下错误:
Router.post('/save/:userid?/:name?/:type?/:payload?', (request, response, next) => {
let promise = new Promise(async (resolve, reject) => {
// Your current code here...
});
promise
.then(function(result) {
response.send(result);
})
.catch(next);
});
然后将 error handling middleware 添加到您的 Express 应用中,例如:
app.use(function(err,req,res,next) {
console.log(err.stack);
res.status(500).send({"Error" : err.stack});
});
所以,我的问题如下。我正在返回一个承诺,当存储一些数据时没有问题时解决,但是如果(1.)我要保存文件的目录不存在或(2.)该文件夹中已经存在一个文件用那个名字;承诺拒绝特定错误。
Router.post('/save/:userid?/:name?/:type?/:payload?', ( request, response ) => {
let promise = new Promise( async ( resolve, reject ) => {
let params = { ...request.params, ...request.query, ...request.body };
let path = `./GameContent/Data/${ params.userid }`;
let fileStream = new FileStream();
let exists = await fileStream.directoryExists( path );
if ( exists ) {
let fileDirectory = `${ path }/${ params.name }.json`;
exists = await fileStream.directoryExists( fileDirectory );
if ( !exists ) {
let bytesWritten = await fileStream.writeFileAt ( fileDirectory, params.payload );
let result = { name: params.name, bytes: bytesWritten, error: 0 };
resolve( JSON.stringify( result ) );
}
else {
let result = { error: 102 };
reject( JSON.stringify( result ) );
}
}
else {
let result = { error: 101 };
reject( JSON.stringify( result ) );
}
});
response.send( promise );
});
到目前为止一切顺利,但是,问题是以下代码没有处理拒绝。
$.post('/api/save', levelJSON)
.catch ( err => {
//Do something...
});
我目前收到了我预期的错误(错误:102)。但是,我一直无法处理错误。
调试器正在抛出这个: UnhandledPromiseRejectionWarning: {"error":102} UnhandledPromiseRejectionWarning:未处理的承诺拒绝。
jQuery $.post
has no .catch()
method for error catching. You can try .fail()
方法改为:
$.post('/api/save', levelJSON)
.done(function (response) {
// success logic here
})
.fail(function (jqXHR, exception) {
// Error logic here
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
console.log(msg)
});
您可以更新路由器代码以捕获如下错误:
Router.post('/save/:userid?/:name?/:type?/:payload?', (request, response, next) => {
let promise = new Promise(async (resolve, reject) => {
// Your current code here...
});
promise
.then(function(result) {
response.send(result);
})
.catch(next);
});
然后将 error handling middleware 添加到您的 Express 应用中,例如:
app.use(function(err,req,res,next) {
console.log(err.stack);
res.status(500).send({"Error" : err.stack});
});