Skip to content

Commit

Permalink
fix: kickOut takes a reason error rather than a message string (#…
Browse files Browse the repository at this point in the history
…1567)

* fix: kickOut takes a reason error rather than a message string
  • Loading branch information
erights authored Aug 20, 2020
1 parent 634046c commit c3cd536
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 24 deletions.
5 changes: 3 additions & 2 deletions packages/notifier/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
* @property {(finalState: T) => void} finish sets the final state, sends a
* final update, and freezes the
* updater
* @property {(reason: T) => void} fail the stream becomes erroneously
* terminated, allegedly for the stated reason.
* @property {(reason: any) => void} fail the stream becomes erroneously
* terminated, allegedly for the stated reason, which is normally an
* instanceof `Error`.
*/

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/zoe/src/contractFacet/contractFacet.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ export function buildRootObject() {
seatToZCFSeatAdmin.init(zcfSeat, zcfSeatAdmin);
const offerHandler = invitationHandleToHandler.get(invitationHandle);
// @ts-ignore
const offerResultP = E(offerHandler)(zcfSeat).catch(err => {
console.error(err);
const offerResultP = E(offerHandler)(zcfSeat).catch(reason => {
console.error(reason);
if (!zcfSeat.hasExited()) {
throw zcfSeat.kickOut(err.message);
throw zcfSeat.kickOut(reason);
} else {
throw err;
throw reason;
}
});
const exitObj = makeExitObj(
Expand Down
9 changes: 6 additions & 3 deletions packages/zoe/src/contractFacet/seat.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ export const makeZcfSeatAdminKit = (
E(zoeSeatAdmin).exit();
},
kickOut: (
msg = 'Kicked out of seat. Please check the log for more information.',
reason = new Error(
'Kicked out of seat. Please check the log for more information.',
),
) => {
assertExitedFalse();
zcfSeatAdmin.updateHasExited();
E(zoeSeatAdmin).kickOut(msg);
assert.fail(msg);
E(zoeSeatAdmin).kickOut(harden(reason));
console.error(reason);
throw reason;
},
getNotifier: () => {
return notifier;
Expand Down
10 changes: 7 additions & 3 deletions packages/zoe/src/contractSupport/zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ export const trade = (zcf, keepLeft, tryRight) => {
} catch (err) {
console.log(err);
throw tryRight.seat.kickOut(
`The trade between left ${keepLeft} and right ${tryRight} failed. Please check the log for more information`,
new Error(
`The trade between left ${keepLeft} and right ${tryRight} failed. Please check the log for more information`,
),
);
}

Expand All @@ -164,7 +166,9 @@ export const trade = (zcf, keepLeft, tryRight) => {
console.log(`offer not safe for right`);
}
return tryRight.seat.kickOut(
`The trade between left ${keepLeft} and right ${tryRight} failed offer safety. Please check the log for more information`,
new Error(
`The trade between left ${keepLeft} and right ${tryRight} failed offer safety. Please check the log for more information`,
),
);
}

Expand Down Expand Up @@ -202,7 +206,7 @@ export const swap = (
keepHandleInactiveMsg = 'prior offer is unavailable',
) => {
if (keepSeat.hasExited()) {
throw trySeat.kickOut(keepHandleInactiveMsg);
throw trySeat.kickOut(new Error(keepHandleInactiveMsg));
}

trade(
Expand Down
6 changes: 3 additions & 3 deletions packages/zoe/src/contracts/publicAuction.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ const start = zcf => {
// Check that the item is still up for auction
if (sellSeat.hasExited()) {
const rejectMsg = `The item up for auction is not available or the auction has completed`;
throw bidSeat.kickOut(rejectMsg);
throw bidSeat.kickOut(new Error(rejectMsg));
}
if (bidSeats.length >= numBidsAllowed) {
throw bidSeat.kickOut(`No further bids allowed.`);
throw bidSeat.kickOut(new Error(`No further bids allowed.`));
}
const sellerSatisfied = satisfies(zcf, sellSeat, {
Ask: bidSeat.getAmountAllocated('Bid', minimumBid.brand),
Expand All @@ -68,7 +68,7 @@ const start = zcf => {
});
if (!(sellerSatisfied && bidderSatisfied)) {
const rejectMsg = `Bid was under minimum bid or for the wrong assets`;
throw bidSeat.kickOut(rejectMsg);
throw bidSeat.kickOut(new Error(rejectMsg));
}

// Save valid bid and try to close.
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/contracts/sellItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const start = zcf => {
// Check that the wanted items are still for sale.
if (!itemsMath.isGTE(currentItemsForSale, wantedItems)) {
const rejectMsg = `Some of the wanted items were not available for sale`;
throw buyerSeat.kickOut(rejectMsg);
throw buyerSeat.kickOut(new Error(rejectMsg));
}

const totalCost = moneyMath.make(
Expand All @@ -67,7 +67,7 @@ const start = zcf => {
// Check that the money provided to pay for the items is greater than the totalCost.
if (!moneyMath.isGTE(providedMoney, totalCost)) {
const rejectMsg = `More money (${totalCost}) is required to buy these items`;
throw buyerSeat.kickOut(rejectMsg);
throw buyerSeat.kickOut(new Error(rejectMsg));
}

// Reallocate. We are able to trade by only defining the gains
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/contracts/simpleExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const start = zcf => {
}
// Eject because the offer must be invalid
throw seat.kickOut(
`The proposal did not match either a buy or sell order.`,
new Error(`The proposal did not match either a buy or sell order.`),
);
};

Expand Down
3 changes: 2 additions & 1 deletion packages/zoe/src/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
* @typedef {Object} ZoeSeatAdmin
* @property {(allocation: Allocation) => void} replaceAllocation
* @property {() => void} exit
* @property {(msg: String) => never} kickOut
* @property {(reason: any) => never} kickOut called with the reason this seat
* is being kicked out, where reason is normally an instanceof Error.
* @property {() => Allocation} getCurrentAllocation
*/

Expand Down
5 changes: 3 additions & 2 deletions packages/zoe/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@
* keys, brand values
* @property {AmountMathKeywordRecord} maths - record with keywords
* keys, amountMath values
*
*
* @typedef {StandardTerms & Record<string, any>} Terms
*
* @typedef {object} InstanceRecord
Expand All @@ -377,7 +377,8 @@
/**
* @typedef {Object} ZCFSeat
* @property {() => void} exit
* @property {(msg?: string) => never} kickOut
* @property {(reason?: any) => never} kickOut called with the reason this
* seat is being kicked out, where reason is normally an instanceof Error.
* @property {() => Notifier<Allocation>} getNotifier
* @property {() => boolean} hasExited
* @property {() => ProposalRecord} getProposal
Expand Down
7 changes: 4 additions & 3 deletions packages/zoe/src/zoeService/zoeSeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ export const makeZoeSeatAdminKit = (
updater.finish(undefined);
doExit(zoeSeatAdmin);
},
kickOut: msg => {
kickOut: reason => {
assert(
instanceAdmin.hasZoeSeatAdmin(zoeSeatAdmin),
`Cannot kick out of seat. Seat has already exited`,
);
updater.fail(msg);
updater.fail(reason);
doExit(zoeSeatAdmin);
assert.fail(msg);
console.log(reason);
throw reason;
},
getCurrentAllocation: () => currentAllocation,
});
Expand Down

0 comments on commit c3cd536

Please sign in to comment.