Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
fix(errors): clarify messaging for collection fetch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
markbello committed Apr 28, 2020
1 parent 629b465 commit e8627cf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ To use the functions that operate on collections, your API and config needs to m
* The URL specified on the `fetch` config should return the full collection when the `/:id` is omitted.
* URLs can have optional path parameters by including `?`, such as `${process.env.HOST_URL}/users/:userId?/books`.
* The API must return an array of resource objects. If it returns an object (common in paged APIs), you can use the `transformData` config function to return the array field of the object instead.
* Each object in the array must have a unique ID field, normally named `id`. If it is not named `id`, then the field name must be set in the `idKey` prop on the resource config.
* Each object in the array must have a unique ID field, normally named `id`. If it is not named `id`, then the field name must be set in the `idKey` prop on the resource config. Iguazu will throw an error on successful network responses if it cannot find a unique ID for each item in the array.

### Reducer
```javascript
Expand Down
13 changes: 7 additions & 6 deletions __tests__/helpers/hash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { getResourceIdHash, getCollectionIdHash, getQueryHash } from '../../src/helpers/hash';
import { ID_TYPE_ERROR } from '../../src/errors';

describe('hash helpers', () => {
describe('getResourceIdHash', () => {
Expand All @@ -39,22 +40,22 @@ describe('hash helpers', () => {
describe('when type of id is invalid', () => {
it('should throw an error when the id is null', () => {
expect(() => getResourceIdHash(null)).toThrowError(
new Error('ID must be an object, number, or string')
new Error(ID_TYPE_ERROR)
);
});
it('should throw an error when the id is undefined', () => {
expect(() => getResourceIdHash(undefined)).toThrowError(
new Error('ID must be an object, number, or string')
new Error(ID_TYPE_ERROR)
);
});
it('should throw an error when the id is a boolean', () => {
expect(() => getResourceIdHash(false)).toThrowError(
new Error('ID must be an object, number, or string')
new Error(ID_TYPE_ERROR)
);
});
it('should throw an error when the id is a function', () => {
expect(() => getResourceIdHash(() => '42')).toThrowError(
new Error('ID must be an object, number, or string')
new Error(ID_TYPE_ERROR)
);
});
});
Expand All @@ -79,12 +80,12 @@ describe('hash helpers', () => {
describe('when type of id is invalid', () => {
it('should throw an error when the id is a boolean and true', () => {
expect(() => getCollectionIdHash(true)).toThrowError(
new Error('ID must be an object, number, or string')
new Error(ID_TYPE_ERROR)
);
});
it('should throw an error when the id is a function', () => {
expect(() => getCollectionIdHash(() => '42')).toThrowError(
new Error('ID must be an object, number, or string')
new Error(ID_TYPE_ERROR)
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
* permissions and limitations under the License.
*/

export const ID_TYPE_ERROR = 'ID must be an object, number, or string';
export const ID_TYPE_ERROR = 'Collection response must be an array of objects containing a unique id key (either "id" by default, or a custom "idKey" of your choice). The ID must be an object, number, or string. For non-compliant API responses, you can transform the data using a custom "transformData" function.';
export const ARRAY_RESPONSE_ERROR = 'Resource call must return an object, not an array';

0 comments on commit e8627cf

Please sign in to comment.