promise reject crash try catch everywhere - node.js

I put try catch everywhere in my code, but this error still crashes the app. What is the problem:
node:events:515
throw er; // Unhandled 'error' event
^
AbortError: The operation was aborted
at Fetch.onAborted (node:internal/deps/undici/undici:6241:53)
at Fetch.emit (node:events:537:28)
at Fetch.abort (node:internal/deps/undici/undici:5511:14)
at requestObject.signal.addEventListener.once (node:internal/deps/undici/und
ici:5542:22)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:645:20)
at AbortSignal.dispatchEvent (node:internal/event_target:587:26)
at abortSignal (node:internal/abort_controller:292:10)
at AbortController.abort (node:internal/abort_controller:322:5)
at AbortSignal.abort (node:internal/deps/undici/undici:4974:36)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:645:20)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:645:20)
at AbortSignal.dispatchEvent (node:internal/event_target:587:26)
at abortSignal (node:internal/abort_controller:292:10)
at AbortController.abort (node:internal/abort_controller:322:5)
at abort (/home/eguneys/chess/rhx/lib/stream.js:17:24)
at PlayOnTurn.fAbort (/home/eguneys/chess/openex/lib/play.js:59:21)
at PlayOnTurn.respondGameAbort (/home/eguneys/chess/openex/lib/play.js:27:68
)
Emitted 'error' event on Readable 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:2
1) {
code: 'ABORT_ERR'
}

Related

localhost not responding for nodejs project [duplicate]

This question already has answers here:
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
(5 answers)
Closed 5 months ago.
I am new to nodejs and still learning so please help me out here.
const http=require('http')
const server=http.createServer((req,res)=>{
if(req.url==='/'){
res.end('Welcome to our home page')
}
if(req.url==='/about'){
res.end("Here is our short abt page")
}
res.end(`
<h1>OOps!</h1>
<p>We can't seem to find the page you are looking for</p>
<a href='/'>Go back home</a>
`)
})
server.listen(2000)
The above mentioned code gives the following error:
node:events:505
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:372:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (c:\Users\Hp\Desktop\node\app.js:10:9)
at Server.emit (node:events:527:28)
at parserOnIncoming (node:_http_server:956:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
PS C:\Users\Hp\Desktop\node> node "c:\Users\Hp\Desktop\node\app.js"
node:events:505
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:372:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (c:\Users\Hp\Desktop\node\app.js:10:9)
at Server.emit (node:events:527:28)
at parserOnIncoming (node:_http_server:956:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
The page at localhost:2000 works at times for the main page (mostly) but gives the "site can't be reached" error for the rest. Do I need to start the xampp apache?
Here we should observe the code execution clearly.
When we visit localhost:2000, First mentioned if condition will be executed and send the response as Welcome to our home page
if(req.url==='/'){
res.end('Welcome to our home page')
}
Then after it tries to check the second if condition and as the if condition is false that block of code will not be executed
if(req.url==='/about'){
res.end("Here is our short abt page")
}
Then it tries to execute the last block of code
res.end(`
<h1>OOps!</h1>
<p>We can't seem to find the page you are looking for</p>
<a href='/'>Go back home</a>
`)
As the res is already sent with first block of if condition, Now again these last lines of code will try to send the response, which will result in an error and the app will be crashed. Because the response is already ended its process.
To overcome these issues, try to change the code in the below-mentioned way.
const server=http.createServer((req,res)=>{
if(req.url==='/'){
res.end('Welcome to our home page')
return
}
if(req.url==='/about'){
res.end("Here is our short abt page")
return;
}
res.end(`
<h1>OOps!</h1>
<p>We can't seem to find the page you are looking for</p>
<a href='/'>Go back home</a>
`)
})
The above-mentioned code snippet will work fine and fixes the issue.

Why am I getting "unexpected end of file" with zlib.js?

I'm using the unzipper Node.js package and it was working fine, processing through my files, when suddenly for seemingly no reason, I got this error:
events.js:291
throw er; // Unhandled 'error' event
^
Error: unexpected end of file
at Zlib.zlibOnError [as onerror] (zlib.js:180:17)
Emitted 'error' event on Parse instance at:
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: -5,
code: 'Z_BUF_ERROR'
}
The error gets thrown every time this particular file is processed, others seem to work fine, but I can't think of what's different about this one.
Is this potentially a user-mistake, or a bug I should report?
code:
console.log("unzipping", zipPath) // <-- error triggers after this log
fs.createReadStream(zipPath)
.pipe(unzipper.Parse())
.on('entry', function(entry) {
entry.pipe(fs.createWriteStream(csvPath)).on('finish', () => {
console.log("done unzipping", zipPath)
fd = fs.openSync(csvPath, "r+")
initializeFile()
})
})
}

events.js:288 throw er; // Unhandled 'error' event

I got the error while doing build in Angular 9.1.0 version.
npm run build
I got bellow error,
throw er; // Unhandled 'error' event
^
Error: write UNKNOWN
at ChildProcess.target._send (internal/child_process.js:806:20)
at ChildProcess.target.send (internal/child_process.js:677:19)
at ChildProcessWorker.send (D:\PublishSource\sample\node_modules\terser-webpack-plugin\node_modules\jest-worker\build\workers\ChildProcessWorker.js:330:17)
at WorkerPool.send (D:\PublishSource\sample\node_modules\terser-webpack-plugin\node_modules\jest-worker\build\WorkerPool.js:32:34)
at Farm._process (D:\PublishSource\sample\node_modules\terser-webpack-plugin\node_modules\jest-worker\build\Farm.js:129:10)
at Farm._enqueue (D:\PublishSource\sample\node_modules\terser-webpack-plugin\node_modules\jest-worker\build\Farm.js:152:10)
at Farm._push (D:\PublishSource\sample\node_modules\terser-webpack-plugin\node_modules\jest-worker\build\Farm.js:159:12)
at D:\PublishSource\sample\node_modules\terser-webpack-plugin\node_modules\jest-worker\build\Farm.js:90:14
at new Promise (<anonymous>)
at Farm.doWork (D:\PublishSource\sample\node_modules\terser-webpack-plugin\node_modules\jest-worker\build\Farm.js:56:12)
at JestWorker._callFunctionWithArgs (D:\PublishSource\sample\node_modules\terser-webpack-plugin\node_modules\jest-worker\build\index.js:178:23)
at TaskRunner.runTask (D:\PublishSource\sample\node_modules\terser-webpack-plugin\dist\TaskRunner.js:41:26)
at enqueue (D:\PublishSource\sample\node_modules\terser-webpack-plugin\dist\TaskRunner.js:80:35)
at D:\PublishSource\sample\node_modules\terser-webpack-plugin\dist\TaskRunner.js:104:86
Emitted 'error' event on ChildProcess instance at:
at internal/child_process.js:810:39
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
errno: 'UNKNOWN',
code: 'UNKNOWN',
syscall: 'write'
Had the same error, tried again without changes and it worked...

NodeJS : Error: read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27)

Using Polling like below to check if the content of the file is changed then, other two functions are called
var poll_max_date=AsyncPolling(function (end,err) { if(err) {
console.error(err); } var stmp_node_id=fs.readFileSync(path.join(__dirname,'node_id'),"utf8");
console.log("--------loaded node : "+stmp_node_id);
if(druid_stmp_node_id!=stmp_node_id) {
// MAX DATA CUT-OFF DRUID QUERY
druid_exe.max_date_query_fire();
// // DRUID QUERY FOR GLOBAL DATA
druid_exe.global_druid_query_fire();
druid_stmp_node_id=stmp_node_id; }
end(); }, 1800000).run();//30 mins
Its working fine for sometime, but then getting below error like after 4 - 5hours :
events.js:167
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27) Emitted 'error' event at:
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
tried using fs.watch to monitor the changes in the file instead of polling like below :
let md5Previous = null; let fsWait = false;
fs.watch(dataSourceLogFile, (event, filename) => { if (filename) {
if (fsWait) return;
fsWait = setTimeout(() => {
fsWait = false;
}, 1000);
const md5Current = md5(fs.readFileSync(dataSourceLogFile));
if (md5Current === md5Previous) {
return;
}
md5Previous = md5Current;
console.log(`${filename} file Changed`);
// MAX DATA CUT-OFF DRUID QUERY
druid_exe.max_date_query_fire();
// DRUID QUERY FOR GLOBAL DATA
druid_exe.global_druid_query_fire(); } });
Its is also working fine for sometime, but then getting same error like after 4 - 5hours :
events.js:167 throw er; // Unhandled 'error' event ^
Error: read ECONNRESET at TCP.onStreamRead
(internal/stream_base_commons.js:111:27) Emitted 'error' event at: at
emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT
(internal/streams/destroy.js:50:3)
But when run in Local Machine, its working fine. the error occurs only when run in remote Linux Machine.
somebody can help me how I can fix that problem?
Use fs.watchFile once , because fs.watch is not consistent across platforms,
https://nodejs.org/docs/latest/api/fs.html#fs_fs_watchfile_filename_options_listener
Change your code according to the requirement.
It has been happening since the users are closing the browser before the data request is received, leading to Connection Reset.
Used PM2 (http://pm2.keymetrics.io/) to run the application, and it is working great now .

nodejs child_process.spawn return throw er; // Unhandled 'error' event

I wanna exec another .js file with child_process.spawn. I'm using OSX.
The code is :
var server = spawn( '/Path/to/node', /.../server.js' );
And it is failed with :
events.js:141
throw er; // Unhandled 'error' event
^
Error: spawn /Users/GB/Dev/workspace_fe/www_ms/output/node/server.js ENOENT
at exports._errnoException (util.js:870:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
at onErrorNT (internal/child_process.js:344:16)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
at Function.Module.runMain (module.js:443:11)
at startup (node.js:139:18)
at node.js:968:3
It's working well if only with command in shell /Path/to/node', /.../server.js.
According to docs second argument of spawn must be array. Have you tried this:
var server = spawn( 'node', ['/path/to/server.js'] );

Resources