refactor(marshal): DRY out encodeToSmallcaps
MODIFY
packages/marshal/src/encodeToSmallcaps.js
packages/marshal/src/encodeToSmallcaps.js
@@ -122,6 +122,18 @@ export const makeEncodeToSmallcaps = ({
encodePromiseToSmallcaps = dontEncodePromiseToSmallcaps,
encodeErrorToSmallcaps = dontEncodeErrorToSmallcaps,
} = {}) => {
+ const assertEncodedError = encoding => {
+ (typeof encoding === 'object' && hasOwnPropertyOf(encoding, '#error')) ||
+ Fail`internal: Error encoding must have "#error" property: ${q(
+ encoding,
+ )}`;
+ // Assert that the #error property decodes to a string.
+ const message = encoding['#error'];
+ (typeof message === 'string' &&
+ (!startsSpecial(message) || message.startsWith('!'))) ||
+ Fail`internal: Error encoding must string message: ${q(message)}`;
+ };
+
/**
* Must encode `val` into plain JSON data *canonically*, such that
* `JSON.stringify(encode(v1)) === JSON.stringify(encode(v1))`. For most
@@ -245,23 +257,9 @@ export const makeEncodeToSmallcaps = ({
}
case 'error': {
const result = encodeErrorToSmallcaps(passable, encodeToSmallcapsRecur);
- if (typeof result === 'object' && hasOwnPropertyOf(result, '#error')) {
- const message = result['#error'];
- if (
- typeof message === 'string' &&
- // check that it decodes to a string
- (!startsSpecial(message) || message.startsWith('!'))
- ) {
+ assertEncodedError(result);
return result;
}
- Fail`internal: Error encoding must have string message: ${q(
- message,
- )}`;
- }
- throw Fail`internal: Error encoding must have "#error" property: ${q(
- result,
- )}`;
- }
default: {
assert.fail(
X`internal: Unrecognized passStyle ${q(passStyle)}`,
@@ -285,19 +283,9 @@ export const makeEncodeToSmallcaps = ({
const result = harden(
encodeErrorToSmallcaps(passable, encodeToSmallcapsRecur),
);
- if (typeof result === 'object' && hasOwnPropertyOf(result, '#error')) {
- const message = result['#error'];
- if (
- typeof message === 'string' &&
- // check that it decodes to a string
- (!startsSpecial(message) || message.startsWith('!'))
- ) {
+ assertEncodedError(result);
return result;
}
- Fail`internal: Error encoding must string message: ${q(message)}`;
- }
- Fail`internal: Error encoding must have "#error" property: ${q(result)}`;
- }
return harden(encodeToSmallcapsRecur(passable));
};
return harden(encodeToSmallcaps);

refactor(marshal): DRY out encodeToSmallcaps