Skip to content
32 changes: 32 additions & 0 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

const Factory = require('./factory')
const Errors = require('./enums').FSPIOPErrorCodes
const ErrorFactory = require('./factory')

const getReplyToFromRequestHeaders = (request) => {
return (request.headers && request.headers['fspiop-source']) ? request.headers['fspiop-source'] : null
Expand Down Expand Up @@ -99,3 +100,34 @@ exports.onPreResponse = function (request, reply) {

return reply.continue
}

/**
* Function to be used to handle the 'onPreHandler' Hapi server extension.
* This validates the error is a FSPIOPError and that it contains a valid error code
* format as per section 7.6 of "API Definition v1.0.docx".
*
* @param request the http request
* @param reply
* @returns {boolean|reply.continue|continue|((key?: IDBValidKey) => void)}
*/
exports.onPreHandler = function (request, reply) {
const response = request.response
let incomingErrorCode
if (response instanceof ErrorFactory.FSPIOPError || response.isBoom) {
try {
incomingErrorCode = response.toApiErrorObject.ErrorInformation.errorCode

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed with last commit

if (!ErrorFactory.validateFSPIOPErrorCode(incomingErrorCode)) {
// 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things here:

  1. I believe you need to add the .takeover() at the end of the calling stack
  2. You will need to ensure that you toApiErrorObject the 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed with last commit

}
} catch (err) {
// 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point 2 above applies here as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed with last commit

}
}
return reply.continue
}
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ const plugin = {
}
}

const validateFspioperrorCodeByCategoryPlugin = {
name: 'validate-fspioperror-code-by-category',
register: function (server) {
server.ext('onPreHandler', Handler.onPreHandler)
}
}

const validateRoutes = (options = {}) => {
options.abortEarly = false
const language = options.language || {}
Expand All @@ -54,6 +61,7 @@ const validateRoutes = (options = {}) => {

module.exports = {
plugin,
validateFspioperrorCodeByCategoryPlugin,
validateRoutes,
Factory: Factory,
Enums: Enums,
Expand Down