Skip to content

Commit

Permalink
Added the ability to add a logging function that is called when error…
Browse files Browse the repository at this point in the history
…s occur in mutations or queries
  • Loading branch information
Sérgio Coimbra committed Aug 16, 2016
1 parent ea045ff commit f49bc19
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/actions/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ export default function (options, callback = false) {
mutates: options.mutates,
headers: options.headers || headers,
body: options.body || body,
endpoint: options.endpoint || endpoint
endpoint: options.endpoint || endpoint,
logging: options.logging
}).then((result) => {
if (callback !== false) {
callback(result, dispatch);
// result = { data: {...}, errors: {...} }
callback({...result, dispatch});
}
});
};
Expand Down
3 changes: 2 additions & 1 deletion lib/actions/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export default function graphql (
fragments,
headers: config && config.headers || headers,
body: config && config.body || body,
endpoint: config && config.endpoint || endpoint
endpoint: config && config.endpoint || endpoint,
logging: config && config.logging
});
}

Expand Down
3 changes: 2 additions & 1 deletion lib/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export default {
removeHeader: 'RELATE_REMOVE_HEADER',
setEndpoint: 'RELATE_SET_ENDPOINT',
setBody: 'RELATE_SET_BODY',
removeBody: 'RELATE_REMOVE_BODY'
removeBody: 'RELATE_REMOVE_BODY',
setLoggingFn: 'RELATE_SET_LOGGING_FUNCTION'
};
2 changes: 1 addition & 1 deletion lib/decorators/root-data-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default function rootDataConnect (config) {
config,
relate_ssr
)
).then((data) => {
).then(({data}) => {
this.deferred.resolve(data);
}).catch((err) => {
warning(false, err);
Expand Down
8 changes: 8 additions & 0 deletions lib/helpers/errorParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function errorParser (error) {
if (error && error.status === 400) {
return (error.response && error.response.body && error.response.body.errors)
|| error.message
|| [{message: 'Unknown error'}];
}
return error && [error] || [{message: 'Unknown error'}];
}
19 changes: 17 additions & 2 deletions lib/helpers/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import request from 'superagent';
import errorParser from '../helpers/errorParser';
import Q from 'q';

export default function doRequest ({dispatch, query, variables, type, endpoint, headers, body, ...params}) {
export default function doRequest (
{
dispatch,
query,
variables,
type,
endpoint,
headers,
body,
logging,
...params
}
) {
return new Q()
.then(() => {
const deferred = Q.defer();
Expand All @@ -20,6 +33,7 @@ export default function doRequest ({dispatch, query, variables, type, endpoint,
req
.end((error, res) => {
if (error) {
logging && logging({errors: errorParser(error)}, dispatch);
deferred.reject(error);
} else {
deferred.resolve(res.body);
Expand All @@ -28,8 +42,9 @@ export default function doRequest ({dispatch, query, variables, type, endpoint,

if (dispatch) {
promise = promise.then(({data, errors}) => {
logging && logging({data, errors}, dispatch);
dispatch({type, data, errors, ...params});
return data;
return {data, errors};
});
}

Expand Down
6 changes: 6 additions & 0 deletions lib/reducer/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ export function relateReducer (_state = defaultState, action = {}) {
});
}

if (action.type === actionTypes.setLoggingFn) {
return Object.assign({}, state, {
logging: action.logging
});
}

return state;
}

Expand Down
1 change: 1 addition & 0 deletions test/reducer/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import expect from 'expect';

import {relateReducer, relateReducerInit, actionTypes} from '../../lib';


describe('Reducer', () => {
it('Does not alter state if not a Relate action', () => {
const state = {something: 1};
Expand Down

0 comments on commit f49bc19

Please sign in to comment.