Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add safe effect helper #552

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/sagas/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,23 @@ export function* retryHandler(retryAction, dismissAction) {

return retry != null;
}

/**
* A wrapper effect to catch unexpected error and provide a binding
* for the desired handling behavior.
*
* @param {Object} effect The targeted effect.
* @param {(error) => void} onError The error handling effect,
* which receives the error object as first argument.
*
* @returns An anonymous effect.
*/
export function safeEffect(effect, onError) {
return function* _safeEffect(payload) {
try {
yield call(effect, payload);
} catch (error) {
yield call(onError, error);
}
}
}
17 changes: 15 additions & 2 deletions src/sagas/nanoContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
import { logger } from '../logger';
import { NANO_CONTRACT_TX_HISTORY_SIZE } from '../constants';
import { consumeGenerator, getNanoContractFeatureToggle } from '../utils';
import { getRegisteredNanoContracts } from './helpers';
import { getRegisteredNanoContracts, safeEffect } from './helpers';
import { isWalletServiceEnabled } from './wallet';

const log = logger('nano-contract-saga');
Expand Down Expand Up @@ -158,6 +158,16 @@ export function* registerNanoContract({ payload }) {
// emit action NANOCONTRACT_REGISTER_SUCCESS with feedback to user
yield put(nanoContractRegisterSuccess({ entryKey: ncId, entryValue: nc, hasFeedback: true }));
}
/**
* Effect invoked by safeEffect if an unexpected error occurs.
*
* @param {Object} error The error captured.
*/
function* registerNanoContractOnError(error) {
log.error('Unexpected error while registering Nano Contract.', error);
yield put(nanoContractRegisterFailure(failureMessage.nanoContractFailure));
yield put(onExceptionCaptured(error, false));
}

/**
* @typedef {Object} RawNcTxHistory
Expand Down Expand Up @@ -440,7 +450,10 @@ export function* requestBlueprintInfo({ payload }) {
export function* saga() {
yield all([
debounce(500, [[types.START_WALLET_SUCCESS, types.NANOCONTRACT_INIT]], init),
takeEvery(types.NANOCONTRACT_REGISTER_REQUEST, registerNanoContract),
takeEvery(
types.NANOCONTRACT_REGISTER_REQUEST,
safeEffect(registerNanoContract, registerNanoContractOnError)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⭐ Nice solution! For the future, I'm thinking of a way to make it more generic, and not require two handler functions for each effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be awesome!

),
takeEvery(types.NANOCONTRACT_HISTORY_REQUEST, requestHistoryNanoContract),
takeEvery(types.NANOCONTRACT_UNREGISTER_REQUEST, unregisterNanoContract),
takeEvery(types.NANOCONTRACT_ADDRESS_CHANGE_REQUEST, requestNanoContractAddressChange),
Expand Down
Loading