Skip to content

Commit

Permalink
Merge b2ee438 into 4d67281
Browse files Browse the repository at this point in the history
  • Loading branch information
knor-el-snor committed Jan 15, 2020
2 parents 4d67281 + b2ee438 commit 1721eb2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ serializer.serialize([parsedError]);

> The `parseErrors` function will load the i18n configuration once, and reuse the same instance afterwards. It is not possible to overwrite the configuration after the first call. This has to do with performance and caching of translations.
### parseJsonResponse(object)
### parseJsonErrors(object)

Parse json object containing errors into javascript `ApiError` instances.
Parse json object containing errors into javascript `ApiError` instances. Will return an array with all non-errors filtered out or empty array if no errors were found.

```javascript
try {
Expand All @@ -191,8 +191,8 @@ Parse json object containing errors into javascript `ApiError` instances.
## Tests

- You can run `yarn test` to run all tests
- You can run `yarn test:coverage` to run all tests with coverage report
- You can run `npm run test` to run all tests
- You can run `npm run test:coverage` to run all tests with coverage report

## Bugs

Expand Down
12 changes: 6 additions & 6 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ export function parseErrors(error: any = {}, translatorOptions?: TranslatorOptio
* Parse json response containing errors into actual ApiError objects
* @param {Object} response
*/
export function parseJsonResponse<T>(response: any): ApiError[] | T {
export function parseJsonErrors(response: any): ApiError[] {
if ((response || {}).hasOwnProperty('errors') && Array.isArray(response.errors)) {
return response.errors.map((error: any) => {
return response.errors.reduce((acc: ApiError[], error: any) => {
if (isApiError(error)) {
const { status, code, title, detail, meta = {} } = error;
return new ApiError(status, { code, message: title }, { detail, stack: (meta || {}).stack });
return [...acc, new ApiError(status, { code, message: title }, { detail, stack: (meta || {}).stack })];
}

return error;
});
return acc;
}, []);
}

return response;
return [];
}

// Interfaces
Expand Down
36 changes: 14 additions & 22 deletions tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as httpStatus from 'http-status';
import { ValidationError } from 'express-validation';

import * as translator from '../src/lib/translator';
import { ApiError, errors, parseErrors, parseJsonResponse, isApiError } from '../src';
import { ApiError, errors, parseErrors, parseJsonErrors, isApiError } from '../src';
import { errorDefaults } from '../src/config/defaults.config';

describe('errorParser', () => {
Expand Down Expand Up @@ -220,9 +220,9 @@ describe('errorParser', () => {
});
});

describe('parseJsonResponse', () => {
describe('parseJsonErrors', () => {
it('Should succesfully return parsed errors', () => {
const result = parseJsonResponse({
const result = parseJsonErrors({
errors: [{
status: httpStatus.BAD_REQUEST,
code: 'MY_CODE',
Expand All @@ -231,7 +231,8 @@ describe('errorParser', () => {
meta: {
stack: 'Something wrong',
},
}],
},
{}], // Should be filtered out
});

expect(result).toBeInstanceOf(Array);
Expand All @@ -249,7 +250,7 @@ describe('errorParser', () => {
});

it('Should succesfully return parsed errors with empty meta', () => {
const result = parseJsonResponse({
const result = parseJsonErrors({
errors: [{
status: httpStatus.BAD_REQUEST,
code: 'MY_CODE',
Expand All @@ -274,7 +275,7 @@ describe('errorParser', () => {
});

it('Should succesfully return parsed errors without meta', () => {
const result = parseJsonResponse({
const result = parseJsonErrors({
errors: [{
status: httpStatus.BAD_REQUEST,
code: 'MY_CODE',
Expand All @@ -297,14 +298,14 @@ describe('errorParser', () => {
});
});

it('Should return response object when contains no errors', () => {
expect(parseJsonResponse(null)).toEqual(null);
expect(parseJsonResponse([])).toEqual([]);
expect(parseJsonResponse({ errors: [] })).toEqual([]);
it('Should return empty array when contains no errors', () => {
expect(parseJsonErrors(null)).toEqual([]);
expect(parseJsonErrors([])).toEqual([]);
expect(parseJsonErrors({ errors: [] })).toEqual([]);
});

it('Should return same error when not all properties were found', () => {
const result = parseJsonResponse({
it('Should return empty array when not all properties were found', () => {
const result = parseJsonErrors({
errors: [{
status: httpStatus.BAD_REQUEST,
detail: { key: 'Value Mister' },
Expand All @@ -315,16 +316,7 @@ describe('errorParser', () => {
});

expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(1);

expect(result[0]).not.toBeInstanceOf(ApiError);
expect(result[0]).toMatchObject({
status: httpStatus.BAD_REQUEST,
detail: { key: 'Value Mister' },
meta: {
stack: 'Something wrong',
},
});
expect(result).toHaveLength(0);
});
});
});

0 comments on commit 1721eb2

Please sign in to comment.