From d99f68a8994a52a173faa37ede6442af9111cd0a Mon Sep 17 00:00:00 2001 From: chinlinlee Date: Mon, 1 Jan 2024 20:44:03 +0800 Subject: [PATCH] refactor: split `getThumbnailAndResponse` into two responsibility things - remove `getThumbnailAndResponse` method in wado uri service - for right responsibility: - response should in controller, not in service - so, move `response.end` into controller - in service, throw error instead of response --- .../WADO-RS/service/thumbnail.service.js | 25 ++++++------------- .../WADO-RS/thumbnail/base.controller.js | 16 ++++-------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/api/dicom-web/controller/WADO-RS/service/thumbnail.service.js b/api/dicom-web/controller/WADO-RS/service/thumbnail.service.js index 539f877a..d18ec8a1 100644 --- a/api/dicom-web/controller/WADO-RS/service/thumbnail.service.js +++ b/api/dicom-web/controller/WADO-RS/service/thumbnail.service.js @@ -3,6 +3,7 @@ const errorResponse = require("../../../../../utils/errorResponse/errorResponseM const renderedService = require("@api/dicom-web/controller/WADO-RS/service/rendered.service"); const _ = require("lodash"); const { getUidsString } = require("./WADO-RS.service"); +const { NotFoundInstanceError } = require("@error/dicom-instance"); class ThumbnailService { /** @@ -18,21 +19,19 @@ class ThumbnailService { this.apiLogger = apiLogger; } - async getThumbnailAndResponse() { + async getThumbnail() { if (!_.get(this.request, "query.viewport")) { _.set(this.request, "query.viewport", "100,100"); } let instanceFramesObj = await this.thumbnailFactory.getThumbnailInstance(); - if (this.checkInstanceExists(instanceFramesObj)) { - return; - } + this.checkInstanceExists(instanceFramesObj); let thumbnail = await this.getThumbnailByInstance(instanceFramesObj); - if (thumbnail) { - return this.response.end(thumbnail, "binary"); + if (!thumbnail) { + throw new Error(`Can not process this image, instanceUID: ${instanceFramesObj.instanceUID}`); } - throw new Error(`Can not process this image, instanceUID: ${instanceFramesObj.instanceUID}`); + return thumbnail; } async getThumbnailByInstance(instanceFramesObj) { @@ -57,18 +56,8 @@ class ThumbnailService { checkInstanceExists(instanceFramesObj) { if (!instanceFramesObj) { - this.response.writeHead(404, { - "Content-Type": "application/dicom+json" - }); - let notFoundMessage = errorResponse.getNotFoundErrorMessage(`Not Found, ${getUidsString(this.thumbnailFactory.uids)}`); - - let notFoundMessageStr = JSON.stringify(notFoundMessage); - - this.apiLogger.logger.warn(`[${notFoundMessageStr}]`); - - return this.response.end(notFoundMessageStr); + throw new NotFoundInstanceError(`Not Found, ${getUidsString(this.thumbnailFactory.uids)}`); } - return undefined; } } diff --git a/api/dicom-web/controller/WADO-RS/thumbnail/base.controller.js b/api/dicom-web/controller/WADO-RS/thumbnail/base.controller.js index c657a3ba..c96cb0df 100644 --- a/api/dicom-web/controller/WADO-RS/thumbnail/base.controller.js +++ b/api/dicom-web/controller/WADO-RS/thumbnail/base.controller.js @@ -2,6 +2,7 @@ const { Controller } = require("@root/api/controller.class"); const { StudyImagePathFactory } = require("@api/dicom-web/controller/WADO-RS/service/WADO-RS.service"); const { ThumbnailService } = require("../service/thumbnail.service"); const { ApiLogger } = require("@root/utils/logs/api-logger"); +const { ApiErrorArrayHandler } = require("@error/api-errors.handler"); class BaseThumbnailController extends Controller { constructor(req, res) { @@ -19,18 +20,11 @@ class BaseThumbnailController extends Controller { try { this.logAction(); let thumbnailService = new ThumbnailService(this.request, this.response, this.apiLogger, this.factory); - return thumbnailService.getThumbnailAndResponse(); + let thumbnail = await thumbnailService.getThumbnail(); + return this.response.end(thumbnail, "binary"); } catch (e) { - let errorStr = JSON.stringify(e, Object.getOwnPropertyNames(e)); - this.apiLogger.logger.error(errorStr); - - this.response.writeHead(500, { - "Content-Type": "application/dicom+json" - }); - return this.response.end({ - code: 500, - message: "An exception occurred" - }); + let apiErrorArrayHandler = new ApiErrorArrayHandler(this.response, this.apiLogger, e); + return apiErrorArrayHandler.doErrorResponse(); } } }