Skip to content

Commit

Permalink
feat: add PUT /thirdpartyRequests/transactions/{id}/error (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleyow authored Oct 22, 2020
1 parent 3ebce07 commit 7428145
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 73 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions src/interface/api-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,53 @@ paths:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/501'
503:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/503'
/thirdpartyRequests/transactions/{ID}/error:
put:
tags:
- thirdparty
- sampled
operationId: ThirdpartyTransactionRequestsError
parameters:
#Path
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/ID.yaml'
#Headers
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/Accept.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/Content-Length.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/Content-Type.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/Date.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/X-Forwarded-For.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/FSPIOP-Source.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/FSPIOP-Destination.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/FSPIOP-Encryption.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/FSPIOP-Signature.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/FSPIOP-URI.yaml'
- $ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/parameters/FSPIOP-HTTP-Method.yaml'
requestBody:
description: Details of the error returned.
required: true
content:
application/json:
schema:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/schemas/ErrorInformationObject.yaml'
responses:
200:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/200'
400:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/400'
401:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/401'
403:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/403'
404:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/404'
405:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/405'
406:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/406'
501:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/501'
503:
$ref: '../../node_modules/@mojaloop/api-snippets/v1.0/openapi3/responses/index.yaml#/503'
/thirdpartyRequests/transactions/{ID}/authorizations:
post:
tags:
Expand Down
174 changes: 113 additions & 61 deletions src/interface/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,7 @@ paths:
description: Data model for the complex type object that contains an optional element ErrorInformation used along with 4xx and 5xx responses.
properties:
errorInformation:
title: ErrorInformation
type: object
description: Data model for the complex type ErrorInformation.
properties:
errorCode:
title: ErrorCode
type: string
pattern: '^[1-9]\d{3}$'
description: |
The API data type ErrorCode is a JSON String of four characters,
consisting of digits only. Negative numbers are not allowed.
A leading zero is not allowed. Each error code in the API is a
four-digit number, for example, 1234, where the first number
(1 in the example) represents the high-level error category,
the second number (2 in the example) represents the low-level error category,
and the last two numbers (34 in the example) represent the specific error.
example: 5100
errorDescription:
title: ErrorDescription
type: string
minLength: 1
maxLength: 128
description: Error description string.
example: This is an error description.
extensionList:
title: ExtensionList
type: object
description: |
Data model for the complex type ExtensionList.
An optional list of extensions, specific to deployment.
properties:
extension:
type: array
items:
title: Extension
type: object
description: Data model for the complex type Extension.
properties:
key:
title: ExtensionKey
type: string
minLength: 1
maxLength: 32
description: Extension key.
value:
title: ExtensionValue
type: string
minLength: 1
maxLength: 128
description: Extension value.
required:
- key
- value
minItems: 1
maxItems: 16
description: Number of Extension elements.
required:
- extension
required:
- errorCode
- errorDescription
$ref: '#/paths/~1thirdpartyRequests~1transactions~1%7BID%7D~1error/put/requestBody/content/application~1json/schema/properties/errorInformation'
headers:
Content-Length:
required: false
Expand Down Expand Up @@ -323,6 +263,118 @@ paths:
$ref: '#/paths/~1health/get/responses/501'
'503':
$ref: '#/paths/~1health/get/responses/503'
'/thirdpartyRequests/transactions/{ID}/error':
put:
tags:
- thirdparty
- sampled
operationId: ThirdpartyTransactionRequestsError
parameters:
- $ref: '#/paths/~1consents~1%7BID%7D/put/parameters/0'
- $ref: '#/paths/~1consents/post/parameters/0'
- $ref: '#/paths/~1consents/post/parameters/1'
- $ref: '#/paths/~1consents/post/parameters/2'
- $ref: '#/paths/~1consents/post/parameters/3'
- $ref: '#/paths/~1consents/post/parameters/4'
- $ref: '#/paths/~1consents/post/parameters/5'
- $ref: '#/paths/~1consents/post/parameters/6'
- $ref: '#/paths/~1consents/post/parameters/7'
- $ref: '#/paths/~1consents/post/parameters/8'
- $ref: '#/paths/~1consents/post/parameters/9'
- $ref: '#/paths/~1consents/post/parameters/10'
requestBody:
description: Details of the error returned.
required: true
content:
application/json:
schema:
title: ErrorInformationObject
type: object
description: Data model for the complex type object that contains ErrorInformation.
properties:
errorInformation:
title: ErrorInformation
type: object
description: Data model for the complex type ErrorInformation.
properties:
errorCode:
title: ErrorCode
type: string
pattern: '^[1-9]\d{3}$'
description: |
The API data type ErrorCode is a JSON String of four characters,
consisting of digits only. Negative numbers are not allowed.
A leading zero is not allowed. Each error code in the API is a
four-digit number, for example, 1234, where the first number
(1 in the example) represents the high-level error category,
the second number (2 in the example) represents the low-level error category,
and the last two numbers (34 in the example) represent the specific error.
example: 5100
errorDescription:
title: ErrorDescription
type: string
minLength: 1
maxLength: 128
description: Error description string.
example: This is an error description.
extensionList:
title: ExtensionList
type: object
description: |
Data model for the complex type ExtensionList.
An optional list of extensions, specific to deployment.
properties:
extension:
type: array
items:
title: Extension
type: object
description: Data model for the complex type Extension.
properties:
key:
title: ExtensionKey
type: string
minLength: 1
maxLength: 32
description: Extension key.
value:
title: ExtensionValue
type: string
minLength: 1
maxLength: 128
description: Extension value.
required:
- key
- value
minItems: 1
maxItems: 16
description: Number of Extension elements.
required:
- extension
required:
- errorCode
- errorDescription
required:
- errorInformation
responses:
'200':
$ref: '#/paths/~1health/get/responses/200'
'400':
$ref: '#/paths/~1health/get/responses/400'
'401':
$ref: '#/paths/~1health/get/responses/401'
'403':
$ref: '#/paths/~1health/get/responses/403'
'404':
$ref: '#/paths/~1health/get/responses/404'
'405':
$ref: '#/paths/~1health/get/responses/405'
'406':
$ref: '#/paths/~1health/get/responses/406'
'501':
$ref: '#/paths/~1health/get/responses/501'
'503':
$ref: '#/paths/~1health/get/responses/503'
'/thirdpartyRequests/transactions/{ID}/authorizations':
post:
tags:
Expand Down
9 changes: 9 additions & 0 deletions src/server/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Health from './health'
import Metrics from './metrics'
import ThirdpartyTransactions from './thirdpartyRequests/transactions'
import ThirdpartyTransactionsId from './thirdpartyRequests/transactions/{ID}'
import ThirdpartyTransactionsIdError from './thirdpartyRequests/transactions/{ID}/error'
import Consents from './consents'
import ConsentsId from './consents/{ID}'
import ConsentsIdGenerateChallenge from './consents/{ID}/generateChallenge'
Expand Down Expand Up @@ -57,6 +58,14 @@ export default {
['success']
]
),
ThirdpartyTransactionRequestsError: wrapWithHistogram(
ThirdpartyTransactionsIdError.put,
[
'thirdpartyRequests_transactions_error_put',
'Put thirdpartyRequests transactions error request',
['success']
]
),
VerifyThirdPartyAuthorization: wrapWithHistogram(
Authorizations.post,
[
Expand Down
88 changes: 88 additions & 0 deletions src/server/handlers/thirdpartyRequests/transactions/{ID}/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*****
License
--------------
Copyright © 2020 Mojaloop Foundation The Mojaloop files are made available by the Mojaloop Foundation
under the Apache License, Version 2.0 (the 'License') and you may not
use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in
writing, the Mojaloop files are distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific language governing
permissions and limitations under the License. Contributors
--------------
This is the official list of the Mojaloop project contributors for this file. Names of the original
copyright holders (individuals or organizations) should be listed with a '*' in the first column.
People who have contributed from an organization can be listed under the organization that actually
holds the copyright for their contributions (see the Gates Foundation organization for an example).
Those individuals should have their names indented and be marked with a '-'. Email address can be
added optionally within square brackets <email>.
* Gates Foundation
- Name Surname <name.surname@gatesfoundation.com>
- Kevin Leyow <kevin.leyow@modusbox.com>
--------------
******/
'use strict'

import { Request, ResponseToolkit, ResponseObject } from '@hapi/hapi'
import Logger from '@mojaloop/central-services-logger'
import { ReformatFSPIOPError, APIErrorObject } from '@mojaloop/central-services-error-handling'
import { Enum } from '@mojaloop/central-services-shared'
import { AuditEventAction } from '@mojaloop/event-sdk'
import { Transactions } from '~/domain/thirdpartyRequests'
import { getSpanTags } from '~/shared/util'

/**
* summary: ThirdpartyTransactionRequestsError
* description: The HTTP request PUT /thirdpartyRequests/transactions/{ID}/error is used to inform a thirdparty
* of a transaction error.
* parameters: body, accept, content-length, content-type, date, x-forwarded-for, fspiop-source,
* fspiop-destination, fspiop-encryption,fspiop-signature, fspiop-uri fspiop-http-method
* produces: application/json
* responses: 200, 400, 401, 403, 404, 405, 406, 501, 503
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const put = async (_context: any, request: Request, h: ResponseToolkit): Promise<ResponseObject> => {
const span = (request as any).span
const transactionRequestId: string = request.params.ID
const payload = request.payload as APIErrorObject

try {
const tags: { [id: string]: string } = getSpanTags(
request,
Enum.Events.Event.Type.TRANSACTION_REQUEST,
Enum.Events.Event.Action.PUT,
{ transactionRequestId: request.params.transactionRequestId })

span?.setTags(tags)
await span?.audit({
headers: request.headers,
payload: request.payload
}, AuditEventAction.start)

// Note: calling async function without `await`
Transactions.forwardTransactionRequestError(
request.headers,
Enum.EndPoints.FspEndpointTemplates.TP_TRANSACTION_REQUEST_PUT_ERROR,
Enum.Http.RestMethods.PUT,
transactionRequestId,
payload,
span
)
.catch(err => {
// Do nothing with the error - forwardTransactionRequestError takes care of async errors
Logger.error('Transactions::put - forwardTransactionRequestError async handler threw an unhandled error')
Logger.error(ReformatFSPIOPError(err))
})

return h.response().code(Enum.Http.ReturnCodes.OK.CODE)
} catch (err) {
const fspiopError = ReformatFSPIOPError(err)
Logger.error(fspiopError)
throw fspiopError
}
}

export default {
put
}
Loading

0 comments on commit 7428145

Please sign in to comment.