-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add
ApiErrorArrayHandler
to handler response of custom er…
…rors
- Loading branch information
1 parent
f04fd9b
commit acb470f
Showing
11 changed files
with
135 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const { getInternalServerErrorMessage, getNotFoundErrorMessage } = require("@root/utils/errorResponse/errorResponseMessage"); | ||
const { ApiLogger } = require("@root/utils/logs/api-logger"); | ||
|
||
|
||
class DicomWebServiceErrorHandler { | ||
|
||
static doErrorResponse(response, e) { | ||
return response.status(e.code).json({ | ||
status: e.status, | ||
message: e.message | ||
}); | ||
} | ||
} | ||
|
||
class NotFoundInstanceErrorHandler { | ||
static doErrorResponse(response, e) { | ||
response.writeHead(404, { | ||
"Content-Type": "application/dicom+json" | ||
}); | ||
return response.end(JSON.stringify(getNotFoundErrorMessage(e.message))); | ||
} | ||
} | ||
|
||
class InstanceGoneErrorHandler { | ||
static doErrorResponse(response, e) { | ||
response.writeHead(410, { | ||
"Content-Type": "application/dicom+json" | ||
}); | ||
return response.end(JSON.stringify({ | ||
Details: e.message, | ||
HttpStatus: 410, | ||
Message: "Image Gone", | ||
Method: "GET" | ||
})); | ||
} | ||
} | ||
|
||
class InvalidFrameNumberErrorHandler { | ||
static doErrorResponse(response, e) { | ||
response.writeHead(400, { | ||
"Content-Type": "application/dicom+json" | ||
}); | ||
|
||
return response.end(JSON.stringify({ | ||
Details: e.message, | ||
HttpStatus: 400, | ||
Message: "Bad request", | ||
Method: "GET" | ||
})); | ||
} | ||
} | ||
|
||
const ApiErrorHandlerMapping = { | ||
"DicomWebServiceError": DicomWebServiceErrorHandler, | ||
"NotFoundInstanceError": NotFoundInstanceErrorHandler, | ||
"InstanceGoneError": InstanceGoneErrorHandler, | ||
"InvalidFrameNumberError": InvalidFrameNumberErrorHandler | ||
}; | ||
|
||
class ApiErrorArrayHandler { | ||
/** | ||
* @param {import("express").Response} res | ||
* @param {ApiLogger} apiLogger | ||
* @param {Error} e | ||
*/ | ||
constructor(res, apiLogger, e) { | ||
this.response = res; | ||
/** @type {ApiLogger} */ | ||
this.apiLogger = apiLogger; | ||
/** @type {Error} */ | ||
this.error = e; | ||
} | ||
|
||
doErrorResponse() { | ||
if (this.isCustomError(this.error)) { | ||
this.apiLogger.logger.error(this.error); | ||
return ApiErrorHandlerMapping[this.error.name].doErrorResponse(this.response, this.error); | ||
} else { | ||
ApiErrorArrayHandler.raiseInternalServerError(this.error, this.response, this.apiLogger); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {import("express").Response} response | ||
* @param {ApiLogger} apiLogger | ||
* @param {Error} e | ||
*/ | ||
static raiseInternalServerError(response, apiLogger, e) { | ||
apiLogger.logger.error(e); | ||
|
||
if (!response.headersSent) { | ||
response.writeHead(500, { | ||
"Content-Type": "application/dicom+json" | ||
}); | ||
return response.json(getInternalServerErrorMessage("An exception occurred")); | ||
} | ||
return response.end(); | ||
} | ||
|
||
isCustomError(e) { | ||
return Object.keys(ApiErrorHandlerMapping).find(key => e.name === key); | ||
} | ||
} | ||
|
||
module.exports.ApiErrorArrayHandler = ApiErrorArrayHandler; |
This file was deleted.
Oops, something went wrong.