Skip to content

Commit

Permalink
Merge pull request #41 from shopgate/error-handling
Browse files Browse the repository at this point in the history
PWA-299: Refactored / Implemented error handling.
  • Loading branch information
richardgorman authored May 9, 2018
2 parents 5fbec83 + 2f72825 commit ee7af25
Show file tree
Hide file tree
Showing 39 changed files with 1,803 additions and 287 deletions.
13 changes: 5 additions & 8 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
"transform-class-properties",
"transform-export-extensions",
"dynamic-import-node",
[
"transform-runtime",
{
"helpers": false,
"polyfill": false,
"regenerator": true
}
]
["transform-runtime", {
"helpers": false,
"polyfill": false,
"regenerator": true
}]
],
"env": {
"development": {
Expand Down
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ ifneq ($(REPO_VERSION), '')
$(call merge-develop, $(SUBSTR))
endif

coverage:
$(foreach package, $(NPM_PACKAGES), $(call run-libraries-coverage, $(package)))


# DEFINITIONS

Expand Down Expand Up @@ -185,4 +182,4 @@ endef
define run-libraries-coverage
cd ./libraries/$(strip $(1))/ && yarn run cover

endef
endef
2 changes: 2 additions & 0 deletions libraries/commerce/cart/actions/addCouponsToCart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PipelineRequest from '@shopgate/pwa-core/classes/PipelineRequest';
import { PROCESS_SEQUENTIAL } from '@shopgate/pwa-core/constants/ProcessTypes';
import { logger } from '@shopgate/pwa-core/helpers';
import * as pipelines from '../constants/Pipelines';
import addCoupons from '../action-creators/addCouponsToCart';
Expand All @@ -15,6 +16,7 @@ const addCouponsToCart = couponIds => dispatch => new Promise((resolve, reject)

const request = new PipelineRequest(pipelines.SHOPGATE_CART_ADD_COUPONS);
request.setInput({ couponCodes: couponIds })
.setResponseProcessed(PROCESS_SEQUENTIAL)
.dispatch()
.then(({ messages }) => {
const requestsPending = request.hasPendingRequests();
Expand Down
2 changes: 2 additions & 0 deletions libraries/commerce/cart/actions/addProductsToCart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PipelineRequest from '@shopgate/pwa-core/classes/PipelineRequest';
import { PROCESS_SEQUENTIAL } from '@shopgate/pwa-core/constants/ProcessTypes';
import { logger } from '@shopgate/pwa-core/helpers';
import * as pipelines from '../constants/Pipelines';
import addProductsToCart from '../action-creators/addProductsToCart';
Expand All @@ -21,6 +22,7 @@ const addToCart = productData => (dispatch, getState) => {

const request = new PipelineRequest(pipelines.SHOPGATE_CART_ADD_PRODUCTS);
request.setInput({ products: productData })
.setResponseProcessed(PROCESS_SEQUENTIAL)
.dispatch()
.then(({ messages }) => {
const requestsPending = request.hasPendingRequests();
Expand Down
2 changes: 2 additions & 0 deletions libraries/commerce/cart/actions/deleteCouponsFromCart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PipelineRequest from '@shopgate/pwa-core/classes/PipelineRequest';
import { PROCESS_SEQUENTIAL } from '@shopgate/pwa-core/constants/ProcessTypes';
import { logger } from '@shopgate/pwa-core/helpers';
import * as pipelines from '../constants/Pipelines';
import deleteCoupons from '../action-creators/deleteCouponsFromCart';
Expand All @@ -16,6 +17,7 @@ const deleteCouponsFromCart = couponIds => (dispatch) => {

const request = new PipelineRequest(pipelines.SHOPGATE_CART_DELETE_COUPONS);
request.setInput({ couponCodes: couponIds })
.setResponseProcessed(PROCESS_SEQUENTIAL)
.dispatch()
.then(({ messages }) => {
const requestsPending = request.hasPendingRequests();
Expand Down
2 changes: 2 additions & 0 deletions libraries/commerce/cart/actions/deleteProductsFromCart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PipelineRequest from '@shopgate/pwa-core/classes/PipelineRequest';
import { PROCESS_SEQUENTIAL } from '@shopgate/pwa-core/constants/ProcessTypes';
import { logger } from '@shopgate/pwa-core/helpers';
import * as pipelines from '../constants/Pipelines';
import deleteProducts from '../action-creators/deleteProductsFromCart';
Expand All @@ -16,6 +17,7 @@ const deleteProductsFromCart = cartItemIds => (dispatch) => {

const request = new PipelineRequest(pipelines.SHOPGATE_CART_DELETE_PRODUCTS);
request.setInput({ CartItemIds: cartItemIds })
.setResponseProcessed(PROCESS_SEQUENTIAL)
.dispatch()
.then(({ messages }) => {
const requestsPending = request.hasPendingRequests();
Expand Down
2 changes: 2 additions & 0 deletions libraries/commerce/cart/actions/updateProductsInCart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PipelineRequest from '@shopgate/pwa-core/classes/PipelineRequest';
import { PROCESS_SEQUENTIAL } from '@shopgate/pwa-core/constants/ProcessTypes';
import { logger } from '@shopgate/pwa-core/helpers';
import * as pipelines from '../constants/Pipelines';
import updateProducts from '../action-creators/updateProductsInCart';
Expand Down Expand Up @@ -29,6 +30,7 @@ const updateProductsInCart = updateData => (dispatch) => {

const request = new PipelineRequest(pipelines.SHOPGATE_CART_UPDATE_PRODUCTS);
request.setInput({ CartItem: convertedData })
.setResponseProcessed(PROCESS_SEQUENTIAL)
.dispatch()
.then(({ messages }) => {
const requestsPending = request.hasPendingRequests();
Expand Down
17 changes: 12 additions & 5 deletions libraries/commerce/cart/subscriptions/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import event from '@shopgate/pwa-core/classes/Event';
import registerEvents from '@shopgate/pwa-core/commands/registerEvents';
import pipelineDependencies from '@shopgate/pwa-core/classes/PipelineDependencies';
import { userDidUpdate$ } from '@shopgate/pwa-common/streams/user';
import { appDidStart$ } from '@shopgate/pwa-common/streams/app';
import { routeDidEnter } from '@shopgate/pwa-common/streams/history';
Expand All @@ -10,6 +11,7 @@ import showModal from '@shopgate/pwa-common/actions/modal/showModal';
import { getHistoryLength, getHistoryPathname } from '@shopgate/pwa-common/selectors/history';
import { INDEX_PATH } from '@shopgate/pwa-common/constants/RoutePaths';
import fetchRegisterUrl from '@shopgate/pwa-common/actions/user/fetchRegisterUrl';
import * as pipelines from '../constants/Pipelines';
import addCouponsToCart from '../actions/addCouponsToCart';
import fetchCart from '../actions/fetchCart';
import {
Expand Down Expand Up @@ -62,6 +64,14 @@ export default function cart(subscribe) {
* Gets triggered when the app starts.
*/
subscribe(appDidStart$, ({ dispatch }) => {
pipelineDependencies.set(`${pipelines.SHOPGATE_CART_GET_CART}.v1`, [
`${pipelines.SHOPGATE_CART_ADD_PRODUCTS}.v1`,
`${pipelines.SHOPGATE_CART_UPDATE_PRODUCTS}.v1`,
`${pipelines.SHOPGATE_CART_DELETE_PRODUCTS}.v1`,
`${pipelines.SHOPGATE_CART_ADD_COUPONS}.v1`,
`${pipelines.SHOPGATE_CART_DELETE_COUPONS}.v1`,
]);

// Register for the app event that is triggered when the checkout process is finished
registerEvents(['checkoutSuccess']);

Expand All @@ -83,11 +93,8 @@ export default function cart(subscribe) {
dispatch(setCartProductPendingCount(0));
});

subscribe(cartNeedsSync$, ({ dispatch, action }) => {
const { requestsPending = false } = action;
if (requestsPending !== true) {
dispatch(fetchCart());
}
subscribe(cartNeedsSync$, ({ dispatch }) => {
dispatch(fetchCart());
});

subscribe(cartBusy$, ({ dispatch }) => {
Expand Down
3 changes: 1 addition & 2 deletions libraries/commerce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
"@shopgate/pwa-unit-test": "^5.3.0-beta.7",
"@shopgate/react-hammerjs": "^0.5.3",
"@shopgate/tracking-core": "^5.3.0-beta.7",
"coveralls": "^3.0.0",
"jest": "^22.4.2",
"jest": "^22.4.3",
"lodash": "^4.17.4",
"react": "^16.3.2",
"react-dom": "^16.3.2"
Expand Down
25 changes: 25 additions & 0 deletions libraries/common/action-creators/error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
APP_ERROR,
PIPELINE_ERROR,
} from '../../constants/ActionTypes';

/**
* Creates the dispatched APP_ERROR action object.
* @param {Object} error The error object.
* @return {Object} The dispatched action object.
*/
export const appError = error => ({
type: APP_ERROR,
error,
});

/**
* Creates the dispatched PIPELINE_ERROR action object.
* @param {Object} error The error object.
* @return {Object} The dispatched action object.
*/
export const pipelineError = error => ({
type: PIPELINE_ERROR,
error,
});

6 changes: 6 additions & 0 deletions libraries/common/constants/ActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ export const ERROR_USER = 'ERROR_USER';
export const REQUEST_URL = 'REQUEST_URL';
export const RECEIVE_URL = 'RECEIVE_URL';
export const ERROR_URL = 'ERROR_URL';

/**
* ------- ERROR -------
*/
export const APP_ERROR = 'APP_ERROR';
export const PIPELINE_ERROR = 'PIPELINE_ERROR';
3 changes: 1 addition & 2 deletions libraries/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
"@shopgate/pwa-core": "^5.3.0-beta.7",
"@shopgate/pwa-unit-test": "^5.3.0-beta.7",
"@shopgate/react-hammerjs": "^0.5.3",
"coveralls": "^3.0.0",
"jest": "^22.4.2",
"jest": "^22.4.3",
"lodash": "^4.17.4",
"prop-types": "^15.6.0",
"react": "^16.3.2",
Expand Down
19 changes: 19 additions & 0 deletions libraries/common/streams/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
APP_ERROR,
PIPELINE_ERROR,
} from '../constants/ActionTypes';
import { main$ } from './main';

/**
* Gets triggered when an app error is received.
* @type {Observable}
*/
export const appError$ = main$
.filter(({ action }) => action.type === APP_ERROR);

/**
* Gets triggered when an pipeline error is received.
* @type {Observable}
*/
export const pipelineError$ = main$
.filter(({ action }) => action.type === PIPELINE_ERROR);
30 changes: 27 additions & 3 deletions libraries/common/subscriptions/app.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import event from '@shopgate/pwa-core/classes/Event';
import registerEvents from '@shopgate/pwa-core/commands/registerEvents';
import closeInAppBrowser from '@shopgate/pwa-core/commands/closeInAppBrowser';
import { emitter as errorEmitter } from '@shopgate/pwa-core/classes/ErrorManager';
import { SOURCE_APP, SOURCE_PIPELINE } from '@shopgate/pwa-core/classes/ErrorManager/constants';
import pipelineManager from '@shopgate/pwa-core/classes/PipelineManager';
import * as errorCodes from '@shopgate/pwa-core/constants/Pipeline';
import { appDidStart$, appWillStart$ } from '../streams/app';
import { pipelineError$ } from '../streams/error';
import registerLinkEvents from '../actions/app/registerLinkEvents';
import showModal from '../actions/modal/showModal';
import { isAndroid } from '../selectors/client';
import {
updateNavigationBarNone,
showPreviousTab,
pageContext,
} from '../helpers/legacy';
import ParsedLink from '../components/Router/helpers/parsed-link';
import { appError, pipelineError } from '../action-creators/error';

/**
* App subscriptions.
* @param {Function} subscribe The subscribe function.
*/
export default function app(subscribe) {
/**
* Gets triggered before the app starts.
*/
// Gets triggered before the app starts.
subscribe(appWillStart$, ({ dispatch, action }) => {
dispatch(registerLinkEvents(action.location));

pipelineManager.addSuppressedErrors([
errorCodes.EACCESS,
]);

// Map the error events into the Observable streams.
errorEmitter.addListener(SOURCE_APP, error => dispatch(appError(error)));
errorEmitter.addListener(SOURCE_PIPELINE, error => dispatch(pipelineError(error)));
});

/**
Expand Down Expand Up @@ -62,4 +75,15 @@ export default function app(subscribe) {
event.addCallback('viewDidDisappear', () => {});
event.addCallback('pageInsetsChanged', () => {});
});

subscribe(pipelineError$, ({ dispatch, action }) => {
const { error } = action;

dispatch(showModal({
confirm: 'modal.ok',
dismiss: null,
message: error.message,
title: null,
}));
});
}
3 changes: 3 additions & 0 deletions libraries/core/classes/ErrorManager/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULT_CONTEXT = '*';
export const SOURCE_APP = 'app';
export const SOURCE_PIPELINE = 'pipeline';
Loading

0 comments on commit ee7af25

Please sign in to comment.