This is not a duplicate to this question since it discusses rejecting promises. This is about errors that are thrown. It is also worth mentioning that I can’t modify the behavior of the mpv.getProperty
function. Please re-open this question.
I’m using an async function mpv.getProperty
(see documentation here)
This function sometimes throws an error and not a rejection to the promise, so I cannot simply use a .catch
block after the async call.
I’ve tried wrapping it around a try-catch block but that didn’t work. I also tried writing a wrapper for it and calling it instead of the function directly as so:
app.get('/filename', (req, res) => {
async function getFilenameProperty() {
try {
return await mpv.getProperty("filename")
} catch (error) {
console.log(error)
}
}
getFilenameProperty().then((f) => {
res.status(200).send(f)
}).catch((error) => {
console.log(error)
res.status(400).send(`Error getting filename`)
})
})
}
Why isn’t this working? What’s the best practice way to handle this error?
To reproduce this error, create an express server with the node-mpv library imported including the /filename
endpoint as shown above:
const mpv = new mpvAPI({"verbose": true}, ["--fullscreen"])
const app = express()
If you perform a GET request to the endpoint, the server crashes instead of handling the error gracefully.
Thanks!
Edit: this is the error being thrown if that gives any clue as to how the error can be handled:
node:events:495
throw er; // Unhandled 'error' event
^
Error [ERR_SOCKET_CLOSED]: Socket is closed
at new NodeError (node:internal/errors:405:5)
at Socket._writeGeneric (node:net:953:8)
at Socket._write (node:net:975:8)
at writeOrBuffer (node:internal/streams/writable:392:12)
at _write (node:internal/streams/writable:333:10)
at Writable.write (node:internal/streams/writable:337:10)
at /hot/GitHub/remotetv-fullstack-project/back/node_modules/node-mpv/lib/ipcInterface/ipcInterface.js:209:17
at new Promise (<anonymous>)
at ipcInterface.send (/hot/GitHub/remotetv-fullstack-project/back/node_modules/node-mpv/lib/ipcInterface/ipcInterface.js:190:10)
at ipcInterface.getProperty (/hot/GitHub/remotetv-fullstack-project/back/node_modules/node-mpv/lib/ipcInterface/ipcInterface.js:117:15)
Emitted 'error' event on Socket instance at:
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'ERR_SOCKET_CLOSED'
}