How I check network error type on same request?
My nodejs code:
try {
const response= await fetch("https://app.local")
} catch (err) {
console.log(err);
}
I get on console:
TypeError: fetch failed
at node:internal/deps/undici/undici:12502:13
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at StatusController.health (/app/src/src/controllers/status.controller.ts:40:19)
at /app/src/node_modules/@nestjs/core/router/router-execution-context.js:46:28
at /app/src/node_modules/@nestjs/core/router/router-proxy.js:9:17 {
[cause]: Error: getaddrinfo ENOTFOUND app.local
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'app.local'
}
}
I want to check code: 'ENOTFOUND'
;
... catch (err) {
if(err.code=='ENOTFOUND') bla bla;
}
But err.code
always undefined.
err.message
output: fetch failed
You can get it with: err.cause.code
.
The error type should be checked to be TypeError (see more info: https://developer.mozilla.org/en-US/docs/Web/API/fetch).