Add more robust error handling to networkService
This commit is contained in:
1 parent
7639dd2e80
commit
b2c43955f8
1 file changed
+105
-66
@@ -64,28 +64,34 @@ class NetworkService {
|
|||||||
* @property {string} message - The message indicating the result of the ping request.
|
* @property {string} message - The message indicating the result of the ping request.
|
||||||
*/
|
*/
|
||||||
async requestPing(job) {
|
async requestPing(job) {
|
||||||
const url = job.data.url;
|
try {
|
||||||
const { response, responseTime, error } = await this.timeRequest(() =>
|
const url = job.data.url;
|
||||||
this.ping.promise.probe(url)
|
const { response, responseTime, error } = await this.timeRequest(() =>
|
||||||
);
|
this.ping.promise.probe(url)
|
||||||
|
);
|
||||||
|
|
||||||
const pingResponse = {
|
const pingResponse = {
|
||||||
monitorId: job.data._id,
|
monitorId: job.data._id,
|
||||||
type: "ping",
|
type: "ping",
|
||||||
responseTime,
|
responseTime,
|
||||||
payload: response,
|
payload: response,
|
||||||
};
|
};
|
||||||
if (error) {
|
if (error) {
|
||||||
pingResponse.status = false;
|
pingResponse.status = false;
|
||||||
pingResponse.code = this.PING_ERROR;
|
pingResponse.code = this.PING_ERROR;
|
||||||
pingResponse.message = errorMessages.PING_CANNOT_RESOLVE;
|
pingResponse.message = errorMessages.PING_CANNOT_RESOLVE;
|
||||||
|
return pingResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
pingResponse.code = 200;
|
||||||
|
pingResponse.status = response.alive;
|
||||||
|
pingResponse.message = successMessages.PING_SUCCESS;
|
||||||
return pingResponse;
|
return pingResponse;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = this.SERVICE_NAME;
|
||||||
|
error.method = "requestPing";
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
pingResponse.code = 200;
|
|
||||||
pingResponse.status = response.alive;
|
|
||||||
pingResponse.message = successMessages.PING_SUCCESS;
|
|
||||||
return pingResponse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,34 +112,40 @@ class NetworkService {
|
|||||||
* @property {string} message - The message indicating the result of the HTTP request.
|
* @property {string} message - The message indicating the result of the HTTP request.
|
||||||
*/
|
*/
|
||||||
async requestHttp(job) {
|
async requestHttp(job) {
|
||||||
const url = job.data.url;
|
try {
|
||||||
const config = {};
|
const url = job.data.url;
|
||||||
|
const config = {};
|
||||||
|
|
||||||
job.data.secret !== undefined &&
|
job.data.secret !== undefined &&
|
||||||
(config.headers = { Authorization: `Bearer ${job.data.secret}` });
|
(config.headers = { Authorization: `Bearer ${job.data.secret}` });
|
||||||
|
|
||||||
const { response, responseTime, error } = await this.timeRequest(() =>
|
const { response, responseTime, error } = await this.timeRequest(() =>
|
||||||
this.axios.get(url, config)
|
this.axios.get(url, config)
|
||||||
);
|
);
|
||||||
|
|
||||||
const httpResponse = {
|
const httpResponse = {
|
||||||
monitorId: job.data._id,
|
monitorId: job.data._id,
|
||||||
type: job.data.type,
|
type: job.data.type,
|
||||||
responseTime,
|
responseTime,
|
||||||
payload: response?.data,
|
payload: response?.data,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
const code = error.response?.status || this.NETWORK_ERROR;
|
const code = error.response?.status || this.NETWORK_ERROR;
|
||||||
httpResponse.code = code;
|
httpResponse.code = code;
|
||||||
httpResponse.status = false;
|
httpResponse.status = false;
|
||||||
httpResponse.message = this.http.STATUS_CODES[code] || "Network Error";
|
httpResponse.message = this.http.STATUS_CODES[code] || "Network Error";
|
||||||
|
return httpResponse;
|
||||||
|
}
|
||||||
|
httpResponse.status = true;
|
||||||
|
httpResponse.code = response.status;
|
||||||
|
httpResponse.message = this.http.STATUS_CODES[response.status];
|
||||||
return httpResponse;
|
return httpResponse;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = this.SERVICE_NAME;
|
||||||
|
error.method = "requestHttp";
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
httpResponse.status = true;
|
|
||||||
httpResponse.code = response.status;
|
|
||||||
httpResponse.message = this.http.STATUS_CODES[response.status];
|
|
||||||
return httpResponse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -153,11 +165,17 @@ class NetworkService {
|
|||||||
* @property {string} message - The message indicating the result of the PageSpeed request.
|
* @property {string} message - The message indicating the result of the PageSpeed request.
|
||||||
*/
|
*/
|
||||||
async requestPagespeed(job) {
|
async requestPagespeed(job) {
|
||||||
const url = job.data.url;
|
try {
|
||||||
const updatedJob = { ...job };
|
const url = job.data.url;
|
||||||
const pagespeedUrl = `https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance`;
|
const updatedJob = { ...job };
|
||||||
updatedJob.data.url = pagespeedUrl;
|
const pagespeedUrl = `https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance`;
|
||||||
return this.requestHttp(updatedJob);
|
updatedJob.data.url = pagespeedUrl;
|
||||||
|
return await this.requestHttp(updatedJob);
|
||||||
|
} catch (error) {
|
||||||
|
error.service = this.SERVICE_NAME;
|
||||||
|
error.method = "requestPagespeed";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -178,7 +196,13 @@ class NetworkService {
|
|||||||
* @property {string} message - The message indicating the result of the hardware status request.
|
* @property {string} message - The message indicating the result of the hardware status request.
|
||||||
*/
|
*/
|
||||||
async requestHardware(job) {
|
async requestHardware(job) {
|
||||||
return this.requestHttp(job);
|
try {
|
||||||
|
return await this.requestHttp(job);
|
||||||
|
} catch (error) {
|
||||||
|
error.service = this.SERVICE_NAME;
|
||||||
|
error.method = "requestHardware";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -198,29 +222,44 @@ class NetworkService {
|
|||||||
* @property {string} message - The message indicating the result of the Docker inspection.
|
* @property {string} message - The message indicating the result of the Docker inspection.
|
||||||
*/
|
*/
|
||||||
async requestDocker(job) {
|
async requestDocker(job) {
|
||||||
const docker = new this.Docker({ socketPath: "/var/run/docker.sock" });
|
try {
|
||||||
const container = docker.getContainer(job.data.url);
|
const docker = new this.Docker({
|
||||||
|
socketPath: "/var/run/docker.sock",
|
||||||
|
handleError: true, // Enable error handling
|
||||||
|
});
|
||||||
|
|
||||||
const { response, responseTime, error } = await this.timeRequest(() =>
|
const containers = await docker.listContainers({ all: true });
|
||||||
container.inspect()
|
const containerExists = containers.some((c) => c.Id === job.data.url);
|
||||||
);
|
if (!containerExists) {
|
||||||
|
throw new Error(errorMessages.DOCKER_NOT_FOUND);
|
||||||
|
}
|
||||||
|
const container = docker.getContainer(job.data.url);
|
||||||
|
|
||||||
const dockerResponse = {
|
const { response, responseTime, error } = await this.timeRequest(() =>
|
||||||
monitorId: job.data._id,
|
container.inspect()
|
||||||
type: job.data.type,
|
);
|
||||||
responseTime,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (error) {
|
const dockerResponse = {
|
||||||
dockerResponse.status = false;
|
monitorId: job.data._id,
|
||||||
dockerResponse.code = error.statusCode || this.NETWORK_ERROR;
|
type: job.data.type,
|
||||||
dockerResponse.message = error.reason || errorMessages.DOCKER_FAIL;
|
responseTime,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
dockerResponse.status = false;
|
||||||
|
dockerResponse.code = error.statusCode || this.NETWORK_ERROR;
|
||||||
|
dockerResponse.message = error.reason || errorMessages.DOCKER_FAIL;
|
||||||
|
return dockerResponse;
|
||||||
|
}
|
||||||
|
dockerResponse.status = response?.State?.Status === "running" ? true : false;
|
||||||
|
dockerResponse.code = 200;
|
||||||
|
dockerResponse.message = successMessages.DOCKER_SUCCESS;
|
||||||
return dockerResponse;
|
return dockerResponse;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = this.SERVICE_NAME;
|
||||||
|
error.method = "requestDocker";
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
dockerResponse.status = response?.State?.Status === "running" ? true : false;
|
|
||||||
dockerResponse.code = 200;
|
|
||||||
dockerResponse.message = successMessages.DOCKER_SUCCESS;
|
|
||||||
return dockerResponse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -246,7 +285,7 @@ class NetworkService {
|
|||||||
* @throws {Error} Throws an error if the job type is unsupported.
|
* @throws {Error} Throws an error if the job type is unsupported.
|
||||||
*/
|
*/
|
||||||
async getStatus(job) {
|
async getStatus(job) {
|
||||||
const type = job.data?.type ?? "unknown";
|
const type = job?.data?.type ?? "unknown";
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case this.TYPE_PING:
|
case this.TYPE_PING:
|
||||||
return await this.requestPing(job);
|
return await this.requestPing(job);
|
||||||
|
|||||||
Reference in new issue
Block a user