Add more robust error handling to networkService

This commit is contained in:
Alex Holliday committed 2024-11-13 14:56:15 +08:00
1 parent 7639dd2e80
commit b2c43955f8
1 file changed
+43 -4
+43 -4
View File
@@ -64,6 +64,7 @@ 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) {
try {
const url = job.data.url; const url = job.data.url;
const { response, responseTime, error } = await this.timeRequest(() => const { response, responseTime, error } = await this.timeRequest(() =>
this.ping.promise.probe(url) this.ping.promise.probe(url)
@@ -86,6 +87,11 @@ class NetworkService {
pingResponse.status = response.alive; pingResponse.status = response.alive;
pingResponse.message = successMessages.PING_SUCCESS; pingResponse.message = successMessages.PING_SUCCESS;
return pingResponse; 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. * @property {string} message - The message indicating the result of the HTTP request.
*/ */
async requestHttp(job) { async requestHttp(job) {
try {
const url = job.data.url; const url = job.data.url;
const config = {}; const config = {};
@@ -134,6 +141,11 @@ class NetworkService {
httpResponse.code = response.status; httpResponse.code = response.status;
httpResponse.message = this.http.STATUS_CODES[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;
}
} }
/** /**
@@ -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) {
try {
const url = job.data.url; const url = job.data.url;
const updatedJob = { ...job }; const updatedJob = { ...job };
const pagespeedUrl = `https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance`; const pagespeedUrl = `https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance`;
updatedJob.data.url = pagespeedUrl; 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. * @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,7 +222,17 @@ 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 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 container = docker.getContainer(job.data.url);
const { response, responseTime, error } = await this.timeRequest(() => const { response, responseTime, error } = await this.timeRequest(() =>
@@ -221,6 +255,11 @@ class NetworkService {
dockerResponse.code = 200; dockerResponse.code = 200;
dockerResponse.message = successMessages.DOCKER_SUCCESS; dockerResponse.message = successMessages.DOCKER_SUCCESS;
return dockerResponse; 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. * @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);