diff --git a/package.json b/package.json index 83c5737..ac424c5 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "prepare": "npm run build && husky install", "prettier": "prettier --config .prettierrc 'src/**/*.ts' 'test/**/*.spec.ts'", "test": "npm run format:ci && npm run lint && npm run coverage", - "unit": "jest test/clientUnit.spec.ts --config=jest.json --runInBand", + "unit": "jest test/clientUnit.spec.ts test/Client/bitPayResponseUnit.spec.ts --config=jest.json --runInBand", "functional": "jest test/clientFunctional.spec.ts --config=jest.json --runInBand" }, "author": "Antonio Buedo ", diff --git a/src/util/BitPayResponseParser.ts b/src/util/BitPayResponseParser.ts index 7e4fc7f..4d7bf08 100644 --- a/src/util/BitPayResponseParser.ts +++ b/src/util/BitPayResponseParser.ts @@ -28,6 +28,25 @@ export class BitPayResponseParser { BitPayExceptionProvider.throwApiExceptionWithMessage(responseObj['error'] ?? null, responseObj['code'] ?? null); } + if (Object.prototype.hasOwnProperty.call(responseObj, 'errors')) { + let result = ''; + responseObj['errors'].forEach((error, index) => { + if (index !== 0) { + result += ' '; + } + + const errorText = error['error'].endsWith('.') ? error['error'].slice(0, -1) : error['error']; + const param = Object.prototype.hasOwnProperty.call(error, 'param') ? error['param'] : ''; + result += `${errorText} ${param}.`; + + if (!result.endsWith('.')) { + result += '.'; + } + }); + + BitPayExceptionProvider.throwApiExceptionWithMessage(result, responseObj['code'] ?? null); + } + if (Object.prototype.hasOwnProperty.call(responseObj, 'success')) { return JSON.stringify(responseObj['success']); } diff --git a/test/Client/bitPayResponseUnit.spec.ts b/test/Client/bitPayResponseUnit.spec.ts new file mode 100644 index 0000000..e10d86d --- /dev/null +++ b/test/Client/bitPayResponseUnit.spec.ts @@ -0,0 +1,18 @@ +import { BitPayResponseParser } from '../../src/util/BitPayResponseParser'; + +describe('BitPayResponse', function () { + it('should handle multiple errors', async () => { + const bitPayResponseParser = new BitPayResponseParser(); + + try { + await bitPayResponseParser.getJsonDataFromJsonResponse({ + errors: [ + { error: 'Missing required parameter.', param: 'price' }, + { error: 'Missing required parameter.', param: 'currency' } + ] + }); + } catch (e: any) { + expect(e.message).toBe('Missing required parameter price. Missing required parameter currency.'); + } + }); +});