so, my problem is the following. I'm returning a promise which resolves when there are no problems while storing some data, but if (1.) The directory where I want to to save the file doesn't exists or (2.) In that folder already exists a file with that name; the promise rejects an specific error.
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 );
});
So far so good, but, the problem is that the following code is not handling the rejection.
$.post('/api/save', levelJSON)
.catch ( err => {
//Do something...
});
I'm currently receiving the error that I expected (error: 102). But, I haven't been able to handle the error.
The debbuger is throwing this: UnhandledPromiseRejectionWarning: {"error":102} UnhandledPromiseRejectionWarning: Unhandled promise rejection.