-
Notifications
You must be signed in to change notification settings - Fork 19
964-CreateHapiPluginToValidateErrorCodesOnPreHandler #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
cd8ab00
062e03a
a3d62c3
32dbb1f
c3ff629
fc70aa5
580ad42
416ab0a
6a09752
8c6f207
27dfe15
2dc3ded
19e794e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things here:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Point 2 above applies here as well.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed with last commit |
||
| } | ||
| } | ||
| return reply.continue | ||
| } | ||
There was a problem hiding this comment.
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
errorInformationobject in the payload. So the above recommendation is not really applicable. You should be instead checking therequest.payload.errorInformation.errorCode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed with last commit