Skip to content

Commit

Permalink
Return apiError even when response is something elese
Browse files Browse the repository at this point in the history
  • Loading branch information
knor-el-snor committed Jan 15, 2020
1 parent b2ee438 commit 1fe9a69
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -175,7 +175,7 @@ serializer.serialize([parsedError]);
### parseJsonErrors(object)

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.
Parse json object containing errors into javascript `ApiError` instances. Will return an array with all non-errors filtered out or default InternalServerError if no errors were found.

```javascript
try {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/parser.ts
@@ -1,7 +1,7 @@
import * as ev from 'express-validation';
import * as _ from 'lodash';

import { ApiError, ValidationError } from './errors';
import { ApiError, ValidationError, InternalServerError } from './errors';
import { errors } from '../config/errors.config';
import { errorDefaults } from '../config/defaults.config';
import { getTranslator } from './translator';
Expand Down Expand Up @@ -102,7 +102,8 @@ export function parseJsonErrors(response: any): ApiError[] {
}, []);
}

return [];
// Make sure to always return ApiError
return [new InternalServerError(errors.INTERNAL_ERROR, { detail: response })];
}

// Interfaces
Expand Down
10 changes: 5 additions & 5 deletions tests/parser.test.ts
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, parseJsonErrors, isApiError } from '../src';
import { ApiError, errors, parseErrors, parseJsonErrors, isApiError, InternalServerError } from '../src';
import { errorDefaults } from '../src/config/defaults.config';

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

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

it('Should return empty array when not all properties were found', () => {
it('Should return empty error when not all properties were found', () => {
const result = parseJsonErrors({
errors: [{
status: httpStatus.BAD_REQUEST,
Expand Down

0 comments on commit 1fe9a69

Please sign in to comment.