Add more robust error handling to networkService
This commit is contained in:
1 parent
7639dd2e80
commit
b2c43955f8
1 file changed
+43
-4
@@ -64,6 +64,7 @@ class NetworkService {
|
||||
* @property {string} message - The message indicating the result of the ping request.
|
||||
*/
|
||||
async requestPing(job) {
|
||||
try {
|
||||
const url = job.data.url;
|
||||
const { response, responseTime, error } = await this.timeRequest(() =>
|
||||
this.ping.promise.probe(url)
|
||||
@@ -86,6 +87,11 @@ class NetworkService {
|
||||
pingResponse.status = response.alive;
|
||||
pingResponse.message = successMessages.PING_SUCCESS;
|
||||
return pingResponse;
|
||||
} catch (error) {
|
||||
error.service = this.SERVICE_NAME;
|
||||
error.method = "requestPing";
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,6 +112,7 @@ class NetworkService {
|
||||
* @property {string} message - The message indicating the result of the HTTP request.
|
||||
*/
|
||||
async requestHttp(job) {
|
||||
try {
|
||||
const url = job.data.url;
|
||||
const config = {};
|
||||
|
||||
@@ -134,6 +141,11 @@ class NetworkService {
|
||||
httpResponse.code = response.status;
|
||||
httpResponse.message = this.http.STATUS_CODES[response.status];
|
||||
return httpResponse;
|
||||
} catch (error) {
|
||||
error.service = this.SERVICE_NAME;
|
||||
error.method = "requestHttp";
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,11 +165,17 @@ class NetworkService {
|
||||
* @property {string} message - The message indicating the result of the PageSpeed request.
|
||||
*/
|
||||
async requestPagespeed(job) {
|
||||
try {
|
||||
const url = job.data.url;
|
||||
const updatedJob = { ...job };
|
||||
const pagespeedUrl = `https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance`;
|
||||
updatedJob.data.url = pagespeedUrl;
|
||||
return this.requestHttp(updatedJob);
|
||||
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.
|
||||
*/
|
||||
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,7 +222,17 @@ class NetworkService {
|
||||
* @property {string} message - The message indicating the result of the Docker inspection.
|
||||
*/
|
||||
async requestDocker(job) {
|
||||
const docker = new this.Docker({ socketPath: "/var/run/docker.sock" });
|
||||
try {
|
||||
const docker = new this.Docker({
|
||||
socketPath: "/var/run/docker.sock",
|
||||
handleError: true, // Enable error handling
|
||||
});
|
||||
|
||||
const containers = await docker.listContainers({ all: true });
|
||||
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 { response, responseTime, error } = await this.timeRequest(() =>
|
||||
@@ -221,6 +255,11 @@ class NetworkService {
|
||||
dockerResponse.code = 200;
|
||||
dockerResponse.message = successMessages.DOCKER_SUCCESS;
|
||||
return dockerResponse;
|
||||
} catch (error) {
|
||||
error.service = this.SERVICE_NAME;
|
||||
error.method = "requestDocker";
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,7 +285,7 @@ class NetworkService {
|
||||
* @throws {Error} Throws an error if the job type is unsupported.
|
||||
*/
|
||||
async getStatus(job) {
|
||||
const type = job.data?.type ?? "unknown";
|
||||
const type = job?.data?.type ?? "unknown";
|
||||
switch (type) {
|
||||
case this.TYPE_PING:
|
||||
return await this.requestPing(job);
|
||||
|
||||
Reference in new issue
Block a user