964-CreateHapiPluginToValidateErrorCodesOnPreHandler - #72
Conversation
| let incomingErrorCode | ||
| if (response instanceof ErrorFactory.FSPIOPError || response.isBoom) { | ||
| try { | ||
| incomingErrorCode = response.toApiErrorObject.ErrorInformation.errorCode |
There was a problem hiding this comment.
This is unnecessary overhead as you can just get the errorCode directly from the error object. Use instead response.apiErrorCode.code (ref: https://github.com/mojaloop/sdk-standard-components/blob/master/src/lib/errors/index.js#L120)
Also, I do not believe that you will be receiving an FSPIOPError here. You are going to be receiving an errorInformation object in the payload. So the above recommendation is not really applicable. You should be instead checking the request.payload.errorInformation.errorCode.
There was a problem hiding this comment.
Addressed with last commit
| // TODO: validateFSPIOPErrorCode throws an exception if code is not valid at which point | ||
| // we nee to call response.takeover() | ||
| // response.takeover(ErrorFactory.createFSPIOPError(Errors.VALIDATION_ERROR, `The incoming error code: ${incomingErrorCode} is not a valid mojaloop specification error code`)) | ||
| return reply.response(ErrorFactory.createFSPIOPError(Errors.VALIDATION_ERROR, `The incoming error code: ${incomingErrorCode} is not a valid mojaloop specification error code`)).code(400) |
There was a problem hiding this comment.
Two things here:
- I believe you need to add the
.takeover()at the end of the calling stack - You will need to ensure that you
toApiErrorObjectthe error is also called on the FSPIOPError assuming that the prehandler is skipped (which would have done this for you). Please verify if this is required.
There was a problem hiding this comment.
Addressed with last commit
| // TODO: validateFSPIOPErrorCode throws an exception if code is not valid at which point | ||
| // we nee to call response.takeover() | ||
| // response.takeover(ErrorFactory.createFSPIOPError(Errors.VALIDATION_ERROR, `The incoming error code: ${incomingErrorCode} is not a valid mojaloop specification error code`)) | ||
| return reply.response(ErrorFactory.createFSPIOPError(Errors.VALIDATION_ERROR, `The incoming error code: ${incomingErrorCode} is not a valid mojaloop specification error code`)).code(400) |
There was a problem hiding this comment.
Point 2 above applies here as well.
There was a problem hiding this comment.
Addressed with last commit
-add takeover() to onPreHandler plugin response -add toApiErrorObject() -instead of obtaining the response from the request object, obtain the payload and specifically its error code -change boolean if validateFspiopErrorCode to null check
rmothilal
left a comment
There was a problem hiding this comment.
please rename to appropriate name
| * @param h | ||
| * @returns {boolean|h.continue|continue|((key?: IDBValidKey) => void)} | ||
| */ | ||
| exports.onPreHandler = function (request, h) { |
There was a problem hiding this comment.
Please rename function to more appropriate name
-let code execution continue if valid mojaloop error code received -add dependency check and update scripts -add unit tests to cover onPreHandler function
-add unit test to meet code coverage requirement
-update unit test to cover 2 invalid error codes, for invalid category and invalid specific error
-add .ncurc.yml -add prune run command
-remove istanbul -run update dep check -rename pretest to standard

Create a hapijs plugin to validate error codes onPreHandler.
NOTE: Currently only used at the ml-api-adapter at PUT /transfers/{id}/error route